Skip to content

Commit

Permalink
Apply allowInPropTypes option to class property
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheile committed Nov 12, 2018
1 parent 3008e85 commit b61ad5c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
22 changes: 22 additions & 0 deletions lib/rules/forbid-foreign-prop-types.js
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
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'
}]
}]
});

0 comments on commit b61ad5c

Please sign in to comment.