Skip to content

Commit

Permalink
Fix props that where not assigned to the right component (fixes #591)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed May 11, 2016
1 parent 6affab3 commit be2a470
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Components.prototype.list = function() {
node = this._list[i].node;
while (!component && node.parent) {
node = node.parent;
// Stop moving up if we reach a decorator
if (node.type === 'Decorator') {
break;
}
component = this.get(node);
}
if (component) {
Expand Down Expand Up @@ -270,9 +274,15 @@ function componentRule(rule, context) {
var scope = context.getScope();
while (scope) {
var node = scope.block;
var isClass = node.type === 'ClassExpression';
var isFunction = /Function/.test(node.type); // Ignore non functions
var isNotMethod = !node.parent || node.parent.type !== 'MethodDefinition'; // Ignore classes methods
var isNotArgument = !node.parent || node.parent.type !== 'CallExpression'; // Ignore arguments (callback, etc.)
// Stop moving up if we reach a class
if (isClass) {
return null;
}
// Return the node if it is a function that is not a class method or an argument (like a callback)
if (isFunction && isNotMethod && isNotArgument) {
return node;
}
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,23 @@ ruleTester.run('prop-types', rule, {
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
// Should stop at the class when searching for a parent component
code: [
'export default (ComposedComponent) => class Something extends SomeOtherComponent {',
' someMethod = ({width}) => {}',
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
// Should stop at the decorator when searching for a parent component
code: [
'@asyncConnect([{',
' promise: ({dispatch}) => {}',
'}])',
'class Something extends Component {}'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit be2a470

Please sign in to comment.