Skip to content

Commit

Permalink
[Fix] prop-types: avoid further crashes from nonexistent nodes in u…
Browse files Browse the repository at this point in the history
…nusedPropTypes

Fixes #2127
  • Loading branch information
ljharb committed Jan 9, 2019
1 parent 7f7b96d commit 58ed9e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/rules/prop-types.js
Expand Up @@ -163,17 +163,18 @@ module.exports = {
* @param {Object} component The component to process
*/
function reportUndeclaredPropTypes(component) {
let allNames;
for (let i = 0, j = component.usedPropTypes.length; i < j; i++) {
allNames = component.usedPropTypes[i].allNames;
const allNames = component.usedPropTypes[i].allNames;
const node = component.usedPropTypes[i].node;
if (
isIgnored(allNames[0]) ||
isDeclaredInComponent(component.node, allNames)
isDeclaredInComponent(component.node, allNames) ||
!node
) {
continue;
}
context.report(
component.usedPropTypes[i].node,
node,
MISSING_MESSAGE, {
name: allNames.join('.').replace(/\.__COMPUTED_PROP__/g, '[]')
}
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -4379,6 +4379,21 @@ ruleTester.run('prop-types', rule, {
errors: [{
message: '\'usedProp\' is missing in props validation'
}]
},
{
code: `
export default class Controller extends React.Component {
handleAdd = id => {
this.setState((state, { name }) => {
const item = this.buildItem(id);
});
};
}
`,
parser: 'babel-eslint',
errors: [{
message: '\'name\' is missing in props validation'
}]
}
]
});

0 comments on commit 58ed9e9

Please sign in to comment.