Skip to content

Commit

Permalink
Merge pull request #1218 from jseminck/no-unused-prop-types-bug
Browse files Browse the repository at this point in the history
Fix false positive in no-unused-proptype
  • Loading branch information
ljharb authored May 24, 2017
2 parents b74a693 + bf69820 commit 089beec
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/util/Components.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,12 @@ Components.prototype.list = function() {
component = this.get(node);
}
if (component) {
usedPropTypes[this._getId(component.node)] = (this._list[i].usedPropTypes || []).filter(function(propType) {
const newUsedProps = (this._list[i].usedPropTypes || []).filter(function(propType) {
return !propType.node || propType.node.kind !== 'init';
});

const componentId = this._getId(component.node);
usedPropTypes[componentId] = (usedPropTypes[componentId] || []).concat(newUsedProps);
}
}
// Assign used props in not confident components to the parent component
Expand Down
45 changes: 45 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,51 @@ ruleTester.run('no-unused-prop-types', rule, {
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
// The next two test cases are related to: https://github.com/yannickcr/eslint-plugin-react/issues/1183
code: [
'export default function SomeComponent(props) {',
' const callback = () => {',
' props.a(props.b);',
' };',
'',
' const anotherCallback = () => {};',
'',
' return (',
' <SomeOtherComponent',
' name={props.c}',
' callback={callback}',
' />',
' );',
'}',
'',
'SomeComponent.propTypes = {',
' a: React.PropTypes.func.isRequired,',
' b: React.PropTypes.string.isRequired,',
' c: React.PropTypes.string.isRequired,',
'};'
].join('\n')
}, {
code: [
'export default function SomeComponent(props) {',
' const callback = () => {',
' props.a(props.b);',
' };',
'',
' return (',
' <SomeOtherComponent',
' name={props.c}',
' callback={callback}',
' />',
' );',
'}',
'',
'SomeComponent.propTypes = {',
' a: React.PropTypes.func.isRequired,',
' b: React.PropTypes.string.isRequired,',
' c: React.PropTypes.string.isRequired,',
'};'
].join('\n')
}
],

Expand Down

0 comments on commit 089beec

Please sign in to comment.