Skip to content

Commit

Permalink
check named imports for "PropTypes"
Browse files Browse the repository at this point in the history
  • Loading branch information
TildaDares committed Jul 23, 2022
1 parent 5118610 commit 2433116
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 11 deletions.
35 changes: 32 additions & 3 deletions lib/rules/forbid-prop-types.js
Expand Up @@ -62,15 +62,24 @@ module.exports = {
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)
&& (
node.name === null
|| node.name === propTypesPackageName
|| !isForeignPropTypesPackage
)
) || (
node.type === 'MemberExpression'
&& node.property.name === 'PropTypes'
&& (node.object.name === null || node.object.name === reactPackageName)
&& node.object.name === 'PropTypes'
&& (
node.object.name === null
|| node.object.name === reactPackageName
|| !isForeignPropTypesPackage
)
);
}

Expand Down Expand Up @@ -126,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 @@ -187,12 +202,23 @@ module.exports = {
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 @@ -204,6 +230,7 @@ module.exports = {
MemberExpression(node) {
if (
!propsUtil.isPropTypesDeclaration(node)
&& !isPropTypesPackage(node)
&& !shouldCheckContextTypes(node)
&& !shouldCheckChildContextTypes(node)
) {
Expand Down Expand Up @@ -233,6 +260,7 @@ module.exports = {
MethodDefinition(node) {
if (
!propsUtil.isPropTypesDeclaration(node)
&& !isPropTypesPackage(node)
&& !shouldCheckContextTypes(node)
&& !shouldCheckChildContextTypes(node)
) {
Expand All @@ -254,6 +282,7 @@ module.exports = {

if (
!propsUtil.isPropTypesDeclaration(property)
&& !isPropTypesPackage(property)
&& !shouldCheckContextTypes(property)
&& !shouldCheckChildContextTypes(property)
) {
Expand Down
44 changes: 36 additions & 8 deletions tests/lib/rules/forbid-prop-types.js
Expand Up @@ -630,6 +630,7 @@ ruleTester.run('forbid-prop-types', rule, {
},
{
code: `
import yup from "yup"
const formValidation = Yup.object().shape({
name: Yup.string(),
customer_ids: Yup.array()
Expand All @@ -638,22 +639,30 @@ ruleTester.run('forbid-prop-types', rule, {
},
{
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({
Yup.object().shape({
value: Yup.number()
})
)
`,
options: [{ forbid: ['number'] }],
},
{
code: `
Expand All @@ -675,6 +684,25 @@ ruleTester.run('forbid-prop-types', rule, {
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 @@ -1772,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 @@ -1797,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 Down

0 comments on commit 2433116

Please sign in to comment.