Skip to content

Commit 41de2c2

Browse files
committed
Fix crash with destructured PropTypes
1 parent 69fe6d8 commit 41de2c2

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/rules/forbid-prop-types.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ module.exports = function(context) {
5353
*/
5454
function checkForbidden(declarations) {
5555
declarations.forEach(function(declaration) {
56+
var target;
5657
if (
5758
declaration.value.type === 'MemberExpression' &&
5859
declaration.value.property &&
@@ -67,9 +68,13 @@ module.exports = function(context) {
6768
) {
6869
declaration.value = declaration.value.callee;
6970
}
70-
71-
if (isForbidden(declaration.value.property.name)) {
72-
context.report(declaration, 'Prop type `' + declaration.value.property.name + '` is forbidden');
71+
if (declaration.value.property) {
72+
target = declaration.value.property.name;
73+
} else if (declaration.value.type === 'Identifier') {
74+
target = declaration.value.name;
75+
}
76+
if (isForbidden(target)) {
77+
context.report(declaration, 'Prop type `' + target + '` is forbidden');
7378
}
7479
});
7580
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,5 +455,24 @@ ruleTester.run('forbid-prop-types', rule, {
455455
forbid: ['instanceOf']
456456
}],
457457
errors: 1
458+
}, {
459+
code: [
460+
'var object = React.PropTypes.object;',
461+
'var Hello = React.createClass({',
462+
' propTypes: {',
463+
' retailer: object,',
464+
' },',
465+
' render: function() {',
466+
' return <div />;',
467+
' }',
468+
'});'
469+
].join('\n'),
470+
ecmaFeatures: {
471+
jsx: true
472+
},
473+
options: [{
474+
forbid: ['object']
475+
}],
476+
errors: 1
458477
}]
459478
});

0 commit comments

Comments
 (0)