Skip to content

Commit

Permalink
Merge pull request #253 from rhysd/fix-issue-249
Browse files Browse the repository at this point in the history
Fix forbid-prop-types to do not modify AST directly (fixes #249)
  • Loading branch information
yannickcr committed Oct 18, 2015
2 parents 75ca44e + 2fb23a9 commit f4cbdfc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,25 @@ module.exports = function(context) {
function checkForbidden(declarations) {
declarations.forEach(function(declaration) {
var target;
var value = declaration.value;
if (
declaration.value.type === 'MemberExpression' &&
declaration.value.property &&
declaration.value.property.name &&
declaration.value.property.name === 'isRequired'
value.type === 'MemberExpression' &&
value.property &&
value.property.name &&
value.property.name === 'isRequired'
) {
declaration.value = declaration.value.object;
value = value.object;
}
if (
declaration.value.type === 'CallExpression' &&
declaration.value.callee.type === 'MemberExpression'
value.type === 'CallExpression' &&
value.callee.type === 'MemberExpression'
) {
declaration.value = declaration.value.callee;
value = value.callee;
}
if (declaration.value.property) {
target = declaration.value.property.name;
} else if (declaration.value.type === 'Identifier') {
target = declaration.value.name;
if (value.property) {
target = value.property.name;
} else if (value.type === 'Identifier') {
target = value.name;
}
if (isForbidden(target)) {
context.report(declaration, 'Prop type `' + target + '` is forbidden');
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/rules/forbid-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,21 @@ ruleTester.run('forbid-prop-types', rule, {
classes: true,
jsx: true
}
}, {
code: [
'class First extends React.Component {',
' render() {',
' return <div />;',
' }',
'}',
'First.propTypes = {',
' elem: PropTypes.instanceOf(HTMLElement)',
'};'
].join('\n'),
ecmaFeatures: {
classes: true,
jsx: true
}
}, {
code: [
'class Hello extends React.Component {',
Expand Down

0 comments on commit f4cbdfc

Please sign in to comment.