Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/rules/forbid-foreign-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -67,6 +79,16 @@ module.exports = {
) {
return true;
}

const classProperty = findParentClassProperty(node);

if (
classProperty &&
classProperty.key &&
classProperty.key.name === 'propTypes'
) {
return true;
}
return false;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/forbid-foreign-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [{
Expand Down Expand Up @@ -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'
}]
}]
});