Skip to content

Commit 9ad0c41

Browse files
janicduplessisfkling
authored andcommitted
Component methods - Improve parameter name parsing and add missing react (#72)
1 parent b34bae6 commit 9ad0c41

File tree

6 files changed

+103
-10
lines changed

6 files changed

+103
-10
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
node_modules/
33

44
## IDE
5-
.idea/
5+
.idea/
6+
7+
## Jest
8+
coverage/
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
/*global jest, describe, beforeEach, it, expect*/
12+
13+
jest.autoMockOff();
14+
15+
describe('getParameterName', () => {
16+
let getParameterName;
17+
let expression;
18+
19+
beforeEach(() => {
20+
getParameterName = require('../getParameterName');
21+
({expression} = require('../../../tests/utils'));
22+
});
23+
24+
it('returns the name for a normal parameter', () => {
25+
const def = expression('function(a) {}');
26+
const param = def.get('params', 0);
27+
expect(getParameterName(param)).toEqual('a');
28+
});
29+
30+
it('returns the name for a rest parameter', () => {
31+
const def = expression('function(...a) {}');
32+
const param = def.get('params', 0);
33+
expect(getParameterName(param)).toEqual('...a');
34+
});
35+
36+
it('returns the name for a parameter with a default value', () => {
37+
const def = expression('function(a = 0) {}');
38+
const param = def.get('params', 0);
39+
expect(getParameterName(param)).toEqual('a');
40+
});
41+
42+
it('returns the raw object representation for a parameter with destructuring', () => {
43+
const def = expression('function({a}) {}');
44+
const param = def.get('params', 0);
45+
expect(getParameterName(param)).toEqual('{a}');
46+
});
47+
48+
it('throws when passed an invalid path', () => {
49+
const def = expression('function() {}');
50+
const param = def;
51+
expect(() => getParameterName(param)).toThrow();
52+
});
53+
});

src/utils/getMethodDocumentation.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import {getDocblock} from './docblock';
1313
import getFlowType from './getFlowType';
14+
import getParameterName from './getParameterName';
1415
import getPropertyName from './getPropertyName';
1516
import getTypeAnnotation from './getTypeAnnotation';
1617

@@ -44,7 +45,7 @@ function getMethodParamsDoc(methodPath, jsDoc) {
4445
}
4546

4647
const param = {
47-
name: paramPath.node.name,
48+
name: getParameterName(paramPath),
4849
type,
4950
};
5051

src/utils/getParameterName.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
* @flow
10+
*/
11+
12+
import recast from 'recast';
13+
14+
import printValue from './printValue';
15+
16+
const {types: {namedTypes: types}} = recast;
17+
18+
export default function getParameterName(parameterPath: NodePath): string {
19+
switch (parameterPath.node.type) {
20+
case types.Identifier.name:
21+
return parameterPath.node.name;
22+
case types.AssignmentPattern.name:
23+
return getParameterName(parameterPath.get('left'));
24+
case types.ObjectPattern.name:
25+
return printValue(parameterPath);
26+
case types.RestElement.name:
27+
return '...' + getParameterName(parameterPath.get('argument'));
28+
default:
29+
throw new TypeError(
30+
'Parameter name must be an Identifier, an AssignmentPattern an ' +
31+
`ObjectPattern or a RestElement, got ${parameterPath.node.type}`
32+
);
33+
}
34+
}

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export {default as getMembers} from './getMembers';
2424
export {default as getMemberValuePath} from './getMemberValuePath';
2525
export {default as getMethodDocumentation} from './getMethodDocumentation';
2626
export {default as getNameOrValue} from './getNameOrValue';
27+
export {default as getParameterName} from './getParameterName';
2728
export {default as getPropertyName} from './getPropertyName';
2829
export {default as getPropertyValuePath} from './getPropertyValuePath';
2930
export {default as getPropType} from './getPropType';

src/utils/isReactComponentMethod.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ import getPropertyName from './getPropertyName';
1616
const {types: {namedTypes: types}} = recast;
1717

1818
const componentMethods = [
19-
'render',
20-
'getInitialState',
21-
'getDefaultProps',
22-
'getChildContext',
23-
'componentWillMount',
2419
'componentDidMount',
25-
'componentWillReceiveProps',
26-
'shouldComponentUpdate',
27-
'componentWillUpdate',
20+
'componentDidReceiveProps',
2821
'componentDidUpdate',
22+
'componentWillMount',
23+
'componentWillReceiveProps',
2924
'componentWillUnmount',
25+
'componentWillUpdate',
26+
'getChildContext',
27+
'getDefaultProps',
28+
'getInitialState',
29+
'render',
30+
'shouldComponentUpdate',
3031
];
3132

3233
/**

0 commit comments

Comments
 (0)