Skip to content

Commit

Permalink
Fix detection for direct props in prop-types (fixes #497)
Browse files Browse the repository at this point in the history
  • Loading branch information
yannickcr committed Mar 14, 2016
1 parent e83079d commit 2884a80
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
var Components = require('../util/Components');
var variable = require('../util/variable');

// ------------------------------------------------------------------------------
// Constants
// ------------------------------------------------------------------------------

var DIRECT_PROPS_REGEX = /^props\s*(\.|\[)/;

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -461,7 +467,7 @@ module.exports = Components.detect(function(context, components, utils) {
* @return {string} the name of the property or undefined if not found
*/
function getPropertyName(node) {
var isDirectProp = /^props(\.|\[)/.test(sourceCode.getText(node));
var isDirectProp = DIRECT_PROPS_REGEX.test(sourceCode.getText(node));
var isInClassComponent = utils.getParentES6Component() || utils.getParentES5Component();
var isNotInConstructor = !inConstructor(node);
if (isDirectProp && isInClassComponent && isNotInConstructor) {
Expand Down Expand Up @@ -567,7 +573,7 @@ module.exports = Components.detect(function(context, components, utils) {
break;
}

var isDirectProp = /^props(\.|\[)/.test(sourceCode.getText(node));
var isDirectProp = DIRECT_PROPS_REGEX.test(sourceCode.getText(node));

usedPropTypes.push({
name: name,
Expand Down
13 changes: 13 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,19 @@ ruleTester.run('prop-types', rule, {
'}'
].join('\n'),
parserOptions: parserOptions
}, {
code: [
'function JobList(props) {',
' props',
' .jobs',
' .forEach(() => {});',
' return <div></div>;',
'}',
'JobList.propTypes = {',
' jobs: PropTypes.array',
'};'
].join('\n'),
parser: 'babel-eslint'
}
],

Expand Down

0 comments on commit 2884a80

Please sign in to comment.