Skip to content

Commit 2884a80

Browse files
committed
Fix detection for direct props in prop-types (fixes #497)
1 parent e83079d commit 2884a80

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/rules/prop-types.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
var Components = require('../util/Components');
1111
var variable = require('../util/variable');
1212

13+
// ------------------------------------------------------------------------------
14+
// Constants
15+
// ------------------------------------------------------------------------------
16+
17+
var DIRECT_PROPS_REGEX = /^props\s*(\.|\[)/;
18+
1319
// ------------------------------------------------------------------------------
1420
// Rule Definition
1521
// ------------------------------------------------------------------------------
@@ -461,7 +467,7 @@ module.exports = Components.detect(function(context, components, utils) {
461467
* @return {string} the name of the property or undefined if not found
462468
*/
463469
function getPropertyName(node) {
464-
var isDirectProp = /^props(\.|\[)/.test(sourceCode.getText(node));
470+
var isDirectProp = DIRECT_PROPS_REGEX.test(sourceCode.getText(node));
465471
var isInClassComponent = utils.getParentES6Component() || utils.getParentES5Component();
466472
var isNotInConstructor = !inConstructor(node);
467473
if (isDirectProp && isInClassComponent && isNotInConstructor) {
@@ -567,7 +573,7 @@ module.exports = Components.detect(function(context, components, utils) {
567573
break;
568574
}
569575

570-
var isDirectProp = /^props(\.|\[)/.test(sourceCode.getText(node));
576+
var isDirectProp = DIRECT_PROPS_REGEX.test(sourceCode.getText(node));
571577

572578
usedPropTypes.push({
573579
name: name,

tests/lib/rules/prop-types.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,19 @@ ruleTester.run('prop-types', rule, {
11591159
'}'
11601160
].join('\n'),
11611161
parserOptions: parserOptions
1162+
}, {
1163+
code: [
1164+
'function JobList(props) {',
1165+
' props',
1166+
' .jobs',
1167+
' .forEach(() => {});',
1168+
' return <div></div>;',
1169+
'}',
1170+
'JobList.propTypes = {',
1171+
' jobs: PropTypes.array',
1172+
'};'
1173+
].join('\n'),
1174+
parser: 'babel-eslint'
11621175
}
11631176
],
11641177

0 commit comments

Comments
 (0)