Skip to content

Commit

Permalink
[Fix] forbid-prop-types: avoid crash
Browse files Browse the repository at this point in the history
Fixes #2682.
  • Loading branch information
ljharb committed Jun 29, 2020
1 parent f5405f7 commit a9d2621
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
56 changes: 29 additions & 27 deletions lib/rules/forbid-prop-types.js
Expand Up @@ -88,33 +88,35 @@ module.exports = {
* @returns {void}
*/
function checkProperties(declarations) {
declarations.forEach((declaration) => {
if (declaration.type !== 'Property') {
return;
}
let target;
let value = declaration.value;
if (
value.type === 'MemberExpression'
&& value.property
&& value.property.name
&& value.property.name === 'isRequired'
) {
value = value.object;
}
if (value.type === 'CallExpression') {
value.arguments.forEach((arg) => {
reportIfForbidden(arg.name, declaration, target);
});
value = value.callee;
}
if (value.property) {
target = value.property.name;
} else if (value.type === 'Identifier') {
target = value.name;
}
reportIfForbidden(target, declaration, target);
});
if (declarations) {
declarations.forEach((declaration) => {
if (declaration.type !== 'Property') {
return;
}
let target;
let value = declaration.value;
if (
value.type === 'MemberExpression'
&& value.property
&& value.property.name
&& value.property.name === 'isRequired'
) {
value = value.object;
}
if (value.type === 'CallExpression') {
value.arguments.forEach((arg) => {
reportIfForbidden(arg.name, declaration, target);
});
value = value.callee;
}
if (value.property) {
target = value.property.name;
} else if (value.type === 'Identifier') {
target = value.name;
}
reportIfForbidden(target, declaration, target);
});
}
}

function checkNode(node) {
Expand Down
10 changes: 10 additions & 0 deletions tests/lib/rules/forbid-prop-types.js
Expand Up @@ -572,6 +572,16 @@ ruleTester.run('forbid-prop-types', rule, {
' preview: PropTypes.bool,',
'}, componentApi, teaserListProps);'
].join('\n')
}, {
code: [
'import PropTypes from "prop-types";',
'const Foo = {',
' foo: PropTypes.string,',
'};',
'const Bar = {',
' bar: PropTypes.shape(Foo),',
'};'
].join('\n')
}],

invalid: [{
Expand Down

0 comments on commit a9d2621

Please sign in to comment.