Skip to content

Commit b61ad5c

Browse files
committed
Apply allowInPropTypes option to class property
1 parent 3008e85 commit b61ad5c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/rules/forbid-foreign-prop-types.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ module.exports = {
5252
return null;
5353
}
5454

55+
function findParentClassProperty(node) {
56+
let parent = node.parent;
57+
58+
while (parent && parent.type !== 'Program') {
59+
if (parent.type === 'ClassProperty') {
60+
return parent;
61+
}
62+
parent = parent.parent;
63+
}
64+
return null;
65+
}
66+
5567
function isAllowedAssignment(node) {
5668
if (!allowInPropTypes) {
5769
return false;
@@ -67,6 +79,16 @@ module.exports = {
6779
) {
6880
return true;
6981
}
82+
83+
const classProperty = findParentClassProperty(node);
84+
85+
if (
86+
classProperty &&
87+
classProperty.key &&
88+
classProperty.key.name === 'propTypes'
89+
) {
90+
return true;
91+
}
7092
return false;
7193
}
7294

tests/lib/rules/forbid-foreign-prop-types.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ ruleTester.run('forbid-foreign-prop-types', rule, {
6262
options: [{
6363
allowInPropTypes: true
6464
}]
65+
},
66+
{
67+
code: `
68+
class MyComponent extends React.Component {
69+
static propTypes = {
70+
baz: Qux.propTypes.baz
71+
};
72+
}
73+
`,
74+
parser: 'babel-eslint',
75+
options: [{
76+
allowInPropTypes: true
77+
}]
6578
}],
6679

6780
invalid: [{
@@ -170,5 +183,22 @@ ruleTester.run('forbid-foreign-prop-types', rule, {
170183
message: ERROR_MESSAGE,
171184
type: 'Identifier'
172185
}]
186+
},
187+
{
188+
code: `
189+
class MyComponent extends React.Component {
190+
static propTypes = {
191+
baz: Qux.propTypes.baz
192+
};
193+
}
194+
`,
195+
parser: 'babel-eslint',
196+
options: [{
197+
allowInPropTypes: false
198+
}],
199+
errors: [{
200+
message: ERROR_MESSAGE,
201+
type: 'Identifier'
202+
}]
173203
}]
174204
});

0 commit comments

Comments
 (0)