Skip to content

Commit 4f6c5ee

Browse files
ZauberNerdfkling
authored andcommitted
Take the correct MemberExpression by comparing the Identifier (#111)
When searching for a MemberExpresion (i.e. "propTypes") that is defined on an object (ArrowFunctionExpression, FunctionExpression, FunctionDeclaration or VariableDeclaration), take the local Identifier into account to find the member of the correct object. Before: When using multiple components per file, it would for every component take the propTypes of the last component encountered. ```javascript function Component1() { return (<i />); } Component1.propTypes = { a: React.PropTypes.string }; function Component2() { return (<b />); } Component2.propTypes = { b: React.PropTypes.number }; ``` After: Now it checks whether the property of the MemberExpression actually belongs to the correct object.
1 parent 5af28f1 commit 4f6c5ee

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/utils/__tests__/getMemberExpressionValuePath-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ describe('getMemberExpressionValuePath', () => {
3232
.toBe(def.parent.get('body', 1, 'expression', 'right'));
3333
});
3434

35+
it('takes the correct property definitions', () => {
36+
var def = statement(`
37+
var Foo = () => {};
38+
Foo.propTypes = {};
39+
Bar.propTypes = { unrelated: true };
40+
`);
41+
42+
expect(getMemberExpressionValuePath(def, 'propTypes'))
43+
.toBe(def.parent.get('body', 1, 'expression', 'right'));
44+
});
45+
3546
it('finds computed property definitions with literal keys', () => {
3647
var def = statement(`
3748
function Foo () {}

src/utils/getMemberExpressionValuePath.js

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

1313
import getNameOrValue from './getNameOrValue';
14+
import { String as toString } from './expressionTo';
1415
import recast from 'recast';
1516

1617
var {types: {namedTypes: types}} = recast;
@@ -82,7 +83,8 @@ export default function getMemberExpressionValuePath(
8283

8384
if (
8485
(!memberPath.node.computed || types.Literal.check(memberPath.node.property)) &&
85-
getNameOrValue(memberPath.get('property')) === memberName
86+
getNameOrValue(memberPath.get('property')) === memberName &&
87+
toString(memberPath.get('object')) === localName
8688
) {
8789
result = path.get('right');
8890
return false;

0 commit comments

Comments
 (0)