diff --git a/lib/rules/forbid-foreign-prop-types.js b/lib/rules/forbid-foreign-prop-types.js index ba944f94e8..b5a829f631 100644 --- a/lib/rules/forbid-foreign-prop-types.js +++ b/lib/rules/forbid-foreign-prop-types.js @@ -52,6 +52,18 @@ module.exports = { return null; } + function findParentClassProperty(node) { + let parent = node.parent; + + while (parent && parent.type !== 'Program') { + if (parent.type === 'ClassProperty') { + return parent; + } + parent = parent.parent; + } + return null; + } + function isAllowedAssignment(node) { if (!allowInPropTypes) { return false; @@ -67,6 +79,16 @@ module.exports = { ) { return true; } + + const classProperty = findParentClassProperty(node); + + if ( + classProperty && + classProperty.key && + classProperty.key.name === 'propTypes' + ) { + return true; + } return false; } diff --git a/tests/lib/rules/forbid-foreign-prop-types.js b/tests/lib/rules/forbid-foreign-prop-types.js index 2ab08f6dad..2396462d9f 100644 --- a/tests/lib/rules/forbid-foreign-prop-types.js +++ b/tests/lib/rules/forbid-foreign-prop-types.js @@ -62,6 +62,19 @@ ruleTester.run('forbid-foreign-prop-types', rule, { options: [{ allowInPropTypes: true }] + }, + { + code: ` + class MyComponent extends React.Component { + static propTypes = { + baz: Qux.propTypes.baz + }; + } + `, + parser: 'babel-eslint', + options: [{ + allowInPropTypes: true + }] }], invalid: [{ @@ -170,5 +183,22 @@ ruleTester.run('forbid-foreign-prop-types', rule, { message: ERROR_MESSAGE, type: 'Identifier' }] + }, + { + code: ` + class MyComponent extends React.Component { + static propTypes = { + baz: Qux.propTypes.baz + }; + } + `, + parser: 'babel-eslint', + options: [{ + allowInPropTypes: false + }], + errors: [{ + message: ERROR_MESSAGE, + type: 'Identifier' + }] }] });