Skip to content

Commit 55e496d

Browse files
Add support for wrapped propTypes to forbid-prop-types
1 parent b25d791 commit 55e496d

File tree

2 files changed

+69
-6
lines changed

2 files changed

+69
-6
lines changed

lib/rules/forbid-prop-types.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ module.exports = {
3636
},
3737

3838
create: function(context) {
39+
var propWrapperFunctions = new Set(context.settings.propWrapperFunctions || []);
40+
3941
function isForbidden(type) {
4042
var configuration = context.options[0] || {};
4143

@@ -108,17 +110,46 @@ module.exports = {
108110

109111
return {
110112
ClassProperty: function(node) {
111-
if (isPropTypesDeclaration(node) && node.value && node.value.type === 'ObjectExpression') {
112-
checkForbidden(node.value.properties);
113+
if (!isPropTypesDeclaration(node)) {
114+
return;
115+
}
116+
switch (node.value && node.value.type) {
117+
case 'ObjectExpression':
118+
checkForbidden(node.value.properties);
119+
break;
120+
case 'CallExpression':
121+
if (
122+
propWrapperFunctions.has(node.value.callee.name) &&
123+
node.value.arguments && node.value.arguments[0]
124+
) {
125+
checkForbidden(node.value.arguments[0].properties);
126+
}
127+
break;
128+
default:
129+
break;
113130
}
114131
},
115132

116133
MemberExpression: function(node) {
117-
if (isPropTypesDeclaration(node.property)) {
118-
var right = node.parent.right;
119-
if (right && right.type === 'ObjectExpression') {
134+
if (!isPropTypesDeclaration(node.property)) {
135+
return;
136+
}
137+
138+
var right = node.parent.right;
139+
switch (right && right.type) {
140+
case 'ObjectExpression':
120141
checkForbidden(right.properties);
121-
}
142+
break;
143+
case 'CallExpression':
144+
if (
145+
propWrapperFunctions.has(right.callee.name) &&
146+
right.arguments && right.arguments[0]
147+
) {
148+
checkForbidden(right.arguments[0].properties);
149+
}
150+
break;
151+
default:
152+
break;
122153
}
123154
},
124155

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,21 @@ ruleTester.run('forbid-prop-types', rule, {
383383
'};'
384384
].join('\n'),
385385
errors: 4
386+
}, {
387+
code: [
388+
'class First extends React.Component {',
389+
' render() {',
390+
' return <div />;',
391+
' }',
392+
'}',
393+
'First.propTypes = forbidExtraProps({',
394+
' a: PropTypes.array',
395+
'});'
396+
].join('\n'),
397+
errors: 1,
398+
settings: {
399+
propWrapperFunctions: ['forbidExtraProps']
400+
}
386401
}, {
387402
code: [
388403
'class Component extends React.Component {',
@@ -397,6 +412,23 @@ ruleTester.run('forbid-prop-types', rule, {
397412
].join('\n'),
398413
parser: 'babel-eslint',
399414
errors: 2
415+
}, {
416+
code: [
417+
'class Component extends React.Component {',
418+
' static propTypes = forbidExtraProps({',
419+
' a: PropTypes.array,',
420+
' o: PropTypes.object',
421+
' });',
422+
' render() {',
423+
' return <div />;',
424+
' }',
425+
'}'
426+
].join('\n'),
427+
parser: 'babel-eslint',
428+
errors: 2,
429+
settings: {
430+
propWrapperFunctions: ['forbidExtraProps']
431+
}
400432
}, {
401433
code: [
402434
'var Hello = createReactClass({',

0 commit comments

Comments
 (0)