Skip to content

Commit dd44ef5

Browse files
committed
Fix parsing of components that use array destructuring in parameters
`getParameterName` was not setup to handle array destructuring and threw an error instead. Fixes #197.
1 parent 2ebe06b commit dd44ef5

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/__tests__/__snapshots__/main-test.js.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,24 @@ Object {
170170
"methods": Array [],
171171
}
172172
`;
173+
174+
exports[`main fixtures processes component "component_8.js" without errors 1`] = `
175+
Object {
176+
"description": "",
177+
"methods": Array [
178+
Object {
179+
"docblock": null,
180+
"modifiers": Array [],
181+
"name": "onChangeSlider",
182+
"params": Array [
183+
Object {
184+
"name": "[min, max]",
185+
"optional": undefined,
186+
"type": null,
187+
},
188+
],
189+
"returns": null,
190+
},
191+
],
192+
}
193+
`;

src/__tests__/fixtures/component_8.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (c) 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*
9+
*/
10+
11+
/**
12+
* Testing array parameter destructuring.
13+
*/
14+
15+
import React from 'react';
16+
17+
var pt = React.PropTypes;
18+
19+
class Parent extends React.Component {
20+
onChangeSlider([min, max]) {
21+
this.setState({ min, max });
22+
}
23+
}
24+
25+
export default Parent;
26+

src/utils/__tests__/getParameterName-test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,16 @@ describe('getParameterName', () => {
3939
expect(getParameterName(param)).toEqual('a');
4040
});
4141

42-
it('returns the raw object representation for a parameter with destructuring', () => {
42+
it('returns the raw representation for a parameter with object destructuring', () => {
4343
const def = expression('function({a}) {}');
4444
const param = def.get('params', 0);
4545
expect(getParameterName(param)).toEqual('{a}');
4646
});
47+
it('returns the raw representation for a parameter with array destructuring', () => {
48+
const def = expression('function([a]) {}');
49+
const param = def.get('params', 0);
50+
expect(getParameterName(param)).toEqual('[a]');
51+
});
4752

4853
it('throws when passed an invalid path', () => {
4954
const def = expression('function() {}');

src/utils/getParameterName.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default function getParameterName(parameterPath: NodePath): string {
2222
case types.AssignmentPattern.name:
2323
return getParameterName(parameterPath.get('left'));
2424
case types.ObjectPattern.name:
25+
case types.ArrayPattern.name:
2526
return printValue(parameterPath);
2627
case types.RestElement.name:
2728
return '...' + getParameterName(parameterPath.get('argument'));

0 commit comments

Comments
 (0)