Skip to content

Commit

Permalink
Fix direct props detection (fixes #298)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Nov 7, 2015
1 parent 004baae commit d464347
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ module.exports = Components.detect(function(context, components) {
* @return {string} the name of the property or undefined if not found
*/
function getPropertyName(node) {
var isDirectProp = /^props\./.test(context.getSource(node));
var isDirectProp = /^props(\.|\[)/.test(context.getSource(node));
var isInClassComponent = context.react.getParentES6Component() || context.react.getParentES5Component();
var isNotInConstructor = !inConstructor(node);
if (isDirectProp && isInClassComponent && isNotInConstructor) {
Expand Down Expand Up @@ -418,7 +418,7 @@ module.exports = Components.detect(function(context, components) {
break;
}

var isDirectProp = /^props\./.test(context.getSource(node));
var isDirectProp = /^props(\.|\[)/.test(context.getSource(node));

usedPropTypes.push({
name: name,
Expand Down
11 changes: 11 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,17 @@ ruleTester.run('prop-types', rule, {
'};'
].join('\n'),
parser: 'babel-eslint'
}, {
// Reassigned props are ignored
code: [
'export class Hello extends Component {',
' render() {',
' const props = this.props;',
' return <div>Hello {props.name.firstname} {props[\'name\'].lastname}</div>',
' }',
'}'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit d464347

Please sign in to comment.