Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply allowInPropTypes option to class property #2040

Merged
merged 1 commit into from Nov 15, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
}]
}]
});