Skip to content

Commit 089beec

Browse files
authored
Merge pull request #1218 from jseminck/no-unused-prop-types-bug
Fix false positive in no-unused-proptype
2 parents b74a693 + bf69820 commit 089beec

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

lib/util/Components.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ Components.prototype.list = function() {
9999
component = this.get(node);
100100
}
101101
if (component) {
102-
usedPropTypes[this._getId(component.node)] = (this._list[i].usedPropTypes || []).filter(function(propType) {
102+
const newUsedProps = (this._list[i].usedPropTypes || []).filter(function(propType) {
103103
return !propType.node || propType.node.kind !== 'init';
104104
});
105+
106+
const componentId = this._getId(component.node);
107+
usedPropTypes[componentId] = (usedPropTypes[componentId] || []).concat(newUsedProps);
105108
}
106109
}
107110
// Assign used props in not confident components to the parent component

tests/lib/rules/no-unused-prop-types.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,6 +1439,51 @@ ruleTester.run('no-unused-prop-types', rule, {
14391439
'}'
14401440
].join('\n'),
14411441
parser: 'babel-eslint'
1442+
}, {
1443+
// The next two test cases are related to: https://github.com/yannickcr/eslint-plugin-react/issues/1183
1444+
code: [
1445+
'export default function SomeComponent(props) {',
1446+
' const callback = () => {',
1447+
' props.a(props.b);',
1448+
' };',
1449+
'',
1450+
' const anotherCallback = () => {};',
1451+
'',
1452+
' return (',
1453+
' <SomeOtherComponent',
1454+
' name={props.c}',
1455+
' callback={callback}',
1456+
' />',
1457+
' );',
1458+
'}',
1459+
'',
1460+
'SomeComponent.propTypes = {',
1461+
' a: React.PropTypes.func.isRequired,',
1462+
' b: React.PropTypes.string.isRequired,',
1463+
' c: React.PropTypes.string.isRequired,',
1464+
'};'
1465+
].join('\n')
1466+
}, {
1467+
code: [
1468+
'export default function SomeComponent(props) {',
1469+
' const callback = () => {',
1470+
' props.a(props.b);',
1471+
' };',
1472+
'',
1473+
' return (',
1474+
' <SomeOtherComponent',
1475+
' name={props.c}',
1476+
' callback={callback}',
1477+
' />',
1478+
' );',
1479+
'}',
1480+
'',
1481+
'SomeComponent.propTypes = {',
1482+
' a: React.PropTypes.func.isRequired,',
1483+
' b: React.PropTypes.string.isRequired,',
1484+
' c: React.PropTypes.string.isRequired,',
1485+
'};'
1486+
].join('\n')
14421487
}
14431488
],
14441489

0 commit comments

Comments
 (0)