Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] forbid-prop-types: warn on destructured values as well #2676

Merged
merged 1 commit into from Jun 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 23 additions & 10 deletions lib/rules/forbid-prop-types.js
Expand Up @@ -59,6 +59,15 @@ module.exports = {
return forbid.indexOf(type) >= 0;
}

function reportIfForbidden(type, declaration, target) {
if (isForbidden(type)) {
context.report({
node: declaration,
message: `Prop type \`${target}\` is forbidden`
});
}
}

function shouldCheckContextTypes(node) {
if (checkContextTypes && propsUtil.isContextTypesDeclaration(node)) {
return true;
Expand Down Expand Up @@ -93,23 +102,18 @@ module.exports = {
) {
value = value.object;
}
if (
value.type === 'CallExpression'
&& value.callee.type === 'MemberExpression'
) {
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;
}
if (isForbidden(target)) {
context.report({
node: declaration,
message: `Prop type \`${target}\` is forbidden`
});
}
reportIfForbidden(target, declaration, target);
});
}

Expand Down Expand Up @@ -161,6 +165,15 @@ module.exports = {
checkNode(node.parent.right);
},

CallExpression(node) {
if (
node.arguments.length > 0
&& (node.callee.name === 'shape' || astUtil.getPropertyName(node.callee) === 'shape')
) {
checkProperties(node.arguments[0].properties);
}
},

MethodDefinition(node) {
if (
!propsUtil.isPropTypesDeclaration(node)
Expand Down
95 changes: 81 additions & 14 deletions tests/lib/rules/forbid-prop-types.js
Expand Up @@ -572,20 +572,6 @@ ruleTester.run('forbid-prop-types', rule, {
' preview: PropTypes.bool,',
'}, componentApi, teaserListProps);'
].join('\n')
}, {
code: [
'var First = createReactClass({',
' propTypes: {',
' s: PropTypes.shape({',
' o: PropTypes.object',
' })',
' },',
' render: function() {',
' return <div />;',
' }',
'});'
].join('\n'),
errors: 0 // TODO: fix #1673 and move this to "invalid"
}],

invalid: [{
Expand Down Expand Up @@ -1460,5 +1446,86 @@ ruleTester.run('forbid-prop-types', rule, {
checkChildContextTypes: true
}],
errors: 1
}, {
code: [
'import { object, string } from "prop-types";',
'function C({ a, b }) { return [a, b]; }',
'C.propTypes = {',
' a: object,',
' b: string',
'};'
].join('\n'),
options: [{
forbid: ['object']
}],
errors: 1
}, {
code: [
'import { objectOf, any } from "prop-types";',
'function C({ a }) { return a; }',
'C.propTypes = {',
' a: objectOf(any)',
'};'
].join('\n'),
options: [{
forbid: ['any']
}],
errors: 1
}, {
code: [
'import { objectOf, any } from "prop-types";',
'function C({ a }) { return a; }',
'C.propTypes = {',
' a: objectOf(any)',
'};'
].join('\n'),
options: [{
forbid: ['objectOf']
}],
errors: 1
},
{
code: [
'import { shape, any } from "prop-types";',
'function C({ a }) { return a; }',
'C.propTypes = {',
' a: shape({',
' b: any',
' })',
'};'
].join('\n'),
options: [{
forbid: ['any']
}],
errors: 1
},
{
code: [
'import { any } from "prop-types";',
'function C({ a }) { return a; }',
'C.propTypes = {',
' a: PropTypes.shape({',
' b: any',
' })',
'};'
].join('\n'),
options: [{
forbid: ['any']
}],
errors: 1
}, {
code: [
'var First = createReactClass({',
' propTypes: {',
' s: PropTypes.shape({',
' o: PropTypes.object',
' })',
' },',
' render: function() {',
' return <div />;',
' }',
'});'
].join('\n'),
errors: 1
}]
});