Skip to content

Commit

Permalink
[Fix] forbid-prop-types: Ignore objects that are not of type React.…
Browse files Browse the repository at this point in the history
…PropTypes
  • Loading branch information
TildaDares authored and ljharb committed Jul 8, 2022
1 parent b3a3937 commit cecfc49
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 6 deletions.
70 changes: 70 additions & 0 deletions lib/rules/forbid-prop-types.js
Expand Up @@ -60,6 +60,28 @@ module.exports = {
const configuration = context.options[0] || {};
const checkContextTypes = configuration.checkContextTypes || false;
const checkChildContextTypes = configuration.checkChildContextTypes || false;
let propTypesPackageName = null;
let reactPackageName = null;
let isForeignPropTypesPackage = false;

function isPropTypesPackage(node) {
return (
node.type === 'Identifier'
&& (
node.name === null
|| node.name === propTypesPackageName
|| !isForeignPropTypesPackage
)
) || (
node.type === 'MemberExpression'
&& node.object.name === 'PropTypes'
&& (
node.object.name === null
|| node.object.name === reactPackageName
|| !isForeignPropTypesPackage
)
);
}

function isForbidden(type) {
const forbid = configuration.forbid || DEFAULTS;
Expand Down Expand Up @@ -113,12 +135,18 @@ module.exports = {
value = value.object;
}
if (value.type === 'CallExpression') {
if (!isPropTypesPackage(value.callee)) {
return;
}
value.arguments.forEach((arg) => {
const name = arg.type === 'MemberExpression' ? arg.property.name : arg.name;
reportIfForbidden(name, declaration, name);
});
value = value.callee;
}
if (!isPropTypesPackage(value)) {
return;
}
if (value.property) {
target = value.property.name;
} else if (value.type === 'Identifier') {
Expand Down Expand Up @@ -157,9 +185,40 @@ module.exports = {
}

return {
ImportDeclaration(node) {
if (node.source && node.source.value === 'prop-types') { // import PropType from "prop-types"
if (node.specifiers.length > 0) {
propTypesPackageName = node.specifiers[0].local.name;
}
} else if (node.source && node.source.value === 'react') { // import { PropTypes } from "react"
if (node.specifiers.length > 0) {
reactPackageName = node.specifiers[0].local.name; // guard against accidental anonymous `import "react"`
}
if (node.specifiers.length >= 1) {
const propTypesSpecifier = node.specifiers.find((specifier) => (
specifier.imported && specifier.imported.name === 'PropTypes'
));
if (propTypesSpecifier) {
propTypesPackageName = propTypesSpecifier.local.name;
}
}
} else { // package is not imported from "react" or "prop-types"
if (node.specifiers.length < 1) {
return;
}
for (const specifier of node.specifiers) {
if (specifier.local.name === 'PropTypes') {
isForeignPropTypesPackage = true;
break;
}
}
}
},

'ClassProperty, PropertyDefinition'(node) {
if (
!propsUtil.isPropTypesDeclaration(node)
&& !isPropTypesPackage(node)
&& !shouldCheckContextTypes(node)
&& !shouldCheckChildContextTypes(node)
) {
Expand All @@ -171,6 +230,7 @@ module.exports = {
MemberExpression(node) {
if (
!propsUtil.isPropTypesDeclaration(node)
&& !isPropTypesPackage(node)
&& !shouldCheckContextTypes(node)
&& !shouldCheckChildContextTypes(node)
) {
Expand All @@ -181,6 +241,14 @@ module.exports = {
},

CallExpression(node) {
if (
node.callee.object
&& !isPropTypesPackage(node.callee.object)
&& !propsUtil.isPropTypesDeclaration(node.callee)
) {
return;
}

if (
node.arguments.length > 0
&& (node.callee.name === 'shape' || astUtil.getPropertyName(node.callee) === 'shape')
Expand All @@ -192,6 +260,7 @@ module.exports = {
MethodDefinition(node) {
if (
!propsUtil.isPropTypesDeclaration(node)
&& !isPropTypesPackage(node)
&& !shouldCheckContextTypes(node)
&& !shouldCheckChildContextTypes(node)
) {
Expand All @@ -213,6 +282,7 @@ module.exports = {

if (
!propsUtil.isPropTypesDeclaration(property)
&& !isPropTypesPackage(property)
&& !shouldCheckContextTypes(property)
&& !shouldCheckChildContextTypes(property)
) {
Expand Down
121 changes: 115 additions & 6 deletions tests/lib/rules/forbid-prop-types.js
Expand Up @@ -627,6 +627,82 @@ ruleTester.run('forbid-prop-types', rule, {
bar: PropTypes.shape(Foo),
};
`,
},
{
code: `
import yup from "yup"
const formValidation = Yup.object().shape({
name: Yup.string(),
customer_ids: Yup.array()
});
`,
},
{
code: `
import yup from "Yup"
const validation = yup.object().shape({
address: yup.object({
city: yup.string(),
zip: yup.string(),
})
})
`,
options: [
{
forbid: ['string', 'object'],
},
],
},
{
code: `
import yup from "yup"
Yup.array(
Yup.object().shape({
value: Yup.number()
})
)
`,
options: [{ forbid: ['number'] }],
},
{
code: `
import CustomPropTypes from "prop-types";
class Component extends React.Component {};
Component.propTypes = {
a: CustomPropTypes.shape({
b: CustomPropTypes.String,
c: CustomPropTypes.number.isRequired,
})
}
`,
},
{
code: `
import CustomReact from "react"
class Component extends React.Component {};
Component.propTypes = {
b: CustomReact.PropTypes.string,
}
`,
},
{
code: `
import PropTypes from "yup"
class Component extends React.Component {};
Component.propTypes = {
b: PropTypes.array(),
}
`,
},
{
code: `
import { PropTypes, shape, any } from "yup"
class Component extends React.Component {};
Component.propTypes = {
b: PropTypes.any,
}
`,
options: [{ forbid: ['any'] }],
}
)),

Expand Down Expand Up @@ -1724,17 +1800,17 @@ ruleTester.run('forbid-prop-types', rule, {
import React from './React';
import { arrayOf, object } from 'prop-types';
const App = ({ foo }) => (
<div>
Hello world {foo}
</div>
);
App.propTypes = {
foo: arrayOf(object)
}
export default App;
`,
errors: [
Expand All @@ -1749,17 +1825,17 @@ ruleTester.run('forbid-prop-types', rule, {
import React from './React';
import PropTypes, { arrayOf } from 'prop-types';
const App = ({ foo }) => (
<div>
Hello world {foo}
</div>
);
App.propTypes = {
foo: arrayOf(PropTypes.object)
}
export default App;
`,
errors: [
Expand All @@ -1769,5 +1845,38 @@ ruleTester.run('forbid-prop-types', rule, {
},
],
},
{
code: `
import CustomPropTypes from "prop-types";
class Component extends React.Component {};
Component.propTypes = {
a: CustomPropTypes.shape({
b: CustomPropTypes.String,
c: CustomPropTypes.object.isRequired,
})
}
`,
errors: [
{
messageId: 'forbiddenPropType',
data: { target: 'object' },
},
],
},
{
code: `
import CustomReact from "react"
class Component extends React.Component {};
Component.propTypes = {
b: CustomReact.PropTypes.object,
}
`,
errors: [
{
messageId: 'forbiddenPropType',
data: { target: 'object' },
},
],
},
]),
});

0 comments on commit cecfc49

Please sign in to comment.