Skip to content

Commit d3dc4c8

Browse files
golopotljharb
authored andcommitted
[Fix] prop-types: Fix false positive on computed member expression
Fixes #1259
1 parent 005070d commit d3dc4c8

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

lib/util/usedPropTypes.js

100644100755
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,11 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
295295
name = getPropertyName(node);
296296
if (name) {
297297
allNames = parentNames.concat(name);
298-
if (node.parent.type === 'MemberExpression') {
298+
if (
299+
// Match props.foo.bar, don't match bar[props.foo]
300+
node.parent.type === 'MemberExpression' &&
301+
node.parent.object === node
302+
) {
299303
markPropTypesAsUsed(node.parent, allNames);
300304
}
301305
// Do not mark computed props as used.

tests/lib/rules/prop-types.js

100644100755
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,19 @@ ruleTester.run('prop-types', rule, {
23032303
const b = a::fn1();
23042304
`,
23052305
parser: 'babel-eslint'
2306+
},
2307+
{
2308+
// issue #1259
2309+
code: `
2310+
const Hello = props => {
2311+
const { notInProp } = dict[props.foo];
2312+
return <div />
2313+
}
2314+
2315+
Hello.propTypes = {
2316+
foo: PropTypes.number,
2317+
}
2318+
`
23062319
}
23072320
],
23082321

0 commit comments

Comments
 (0)