Skip to content

Commit

Permalink
Fixes a few false positive failures in the no-unused-prop-types rule
Browse files Browse the repository at this point in the history
  • Loading branch information
Evgueni Naverniouk committed Aug 29, 2016
1 parent 9f76459 commit 070b489
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 3 deletions.
49 changes: 46 additions & 3 deletions lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,43 @@ module.exports = {
);
}

/**
* Returns true if the given node is inside a React Component lifecycle
* method.
* @param {ASTNode} node The AST node being checked.
* @return {Boolean} True if the node is inside a lifecycle method
*/
function isInLifeCycleMethod(node) {
if (node.type === 'MethodDefinition' && (
node.kind === 'constructor' ||
node.key.name === 'componentWillReceiveProps' ||
node.key.name === 'shouldComponentUpdate' ||
node.key.name === 'componentWillUpdate' ||
node.key.name === 'componentDidUpdate'
)) {
return true;
}

if (node.parent) {
return isInLifeCycleMethod(node.parent);
}

return false;
}

/**
* Checks if a prop init name matches common naming patterns
* @param {ASTNode} node The AST node being checked.
* @returns {Boolean} True if the prop name matches
*/
function isPropAttributeName (node) {
return (
node.init.name === 'props' ||
node.init.name === 'nextProps' ||
node.init.name === 'prevProps'
);
}

/**
* Checks if a prop is used
* @param {ASTNode} node The AST node being checked.
Expand Down Expand Up @@ -536,11 +573,14 @@ module.exports = {
node.id.properties[i].value.type === 'ObjectPattern'
);
// let {firstname} = props
var statelessDestructuring = node.init.name === 'props' && utils.getParentStatelessComponent();
var genericDestructuring = isPropAttributeName(node) && (
utils.getParentStatelessComponent() ||
isInLifeCycleMethod(node)
);

if (thisDestructuring) {
properties = node.id.properties[i].value.properties;
} else if (statelessDestructuring) {
} else if (genericDestructuring) {
properties = node.id.properties;
} else {
continue;
Expand Down Expand Up @@ -776,7 +816,10 @@ module.exports = {
// let {props: {firstname}} = this
var thisDestructuring = destructuring && node.init.type === 'ThisExpression';
// let {firstname} = props
var statelessDestructuring = destructuring && node.init.name === 'props' && utils.getParentStatelessComponent();
var statelessDestructuring = destructuring && isPropAttributeName(node) && (
utils.getParentStatelessComponent() ||
isInLifeCycleMethod(node)
);

if (!thisDestructuring && !statelessDestructuring) {
return;
Expand Down
186 changes: 186 additions & 0 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,96 @@ ruleTester.run('no-unused-prop-types', rule, {
].join('\n'),
options: [{skipShapeProps: true}],
parser: 'babel-eslint'
}, {
// Destructured props in componentWillReceiveProps shouldn't throw errors
code: [
'class Hello extends Component {',
' static propTypes = {',
' something: PropTypes.bool',
' }',
' componentWillReceiveProps (nextProps) {',
' const {something} = nextProps;',
' doSomething(something);',
' }',
'}'
].join('\n'),
parser: 'babel-eslint'
}, {
// Destructured props in the constructor shouldn't throw errors
code: [
'class Hello extends Component {',
' static propTypes = {',
' something: PropTypes.bool',
' }',
' constructor (props) {',
' super(props);',
' const {something} = props;',
' doSomething(something);',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions
}, {
// Destructured props in the `componentWillReceiveProps` method shouldn't throw errors
code: [
'class Hello extends Component {',
' static propTypes = {',
' something: PropTypes.bool',
' }',
' componentWillReceiveProps (nextProps, nextState) {',
' const {something} = nextProps;',
' return something;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions
}, {
// Destructured props in the `shouldComponentUpdate` method shouldn't throw errors
code: [
'class Hello extends Component {',
' static propTypes = {',
' something: PropTypes.bool',
' }',
' shouldComponentUpdate (nextProps, nextState) {',
' const {something} = nextProps;',
' return something;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions
}, {
// Destructured props in the `componentWillUpdate` method shouldn't throw errors
code: [
'class Hello extends Component {',
' static propTypes = {',
' something: PropTypes.bool',
' }',
' componentWillUpdate (nextProps, nextState) {',
' const {something} = nextProps;',
' return something;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions
}, {
// Destructured props in the `componentDidUpdate` method shouldn't throw errors
code: [
'class Hello extends Component {',
' static propTypes = {',
' something: PropTypes.bool',
' }',
' componentDidUpdate (prevProps, nextState) {',
' const {something} = prevProps;',
' return something;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions
}
],

Expand Down Expand Up @@ -1976,6 +2066,102 @@ ruleTester.run('no-unused-prop-types', rule, {
errors: [{
message: '\'unused\' PropType is defined but prop is never used'
}]
}, {
code: [
'class Hello extends Component {',
' static propTypes = {',
' unused: PropTypes.bool',
' }',
' constructor (props) {',
' super(props);',
' const {something} = props;',
' doSomething(something);',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions,
errors: [{
message: '\'unused\' PropType is defined but prop is never used',
line: 3,
column: 13
}]
}, {
code: [
'class Hello extends Component {',
' static propTypes = {',
' unused: PropTypes.bool',
' }',
' componentWillReceiveProps (nextProps, nextState) {',
' const {something} = nextProps;',
' return something;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions,
errors: [{
message: '\'unused\' PropType is defined but prop is never used',
line: 3,
column: 13
}]
}, {
code: [
'class Hello extends Component {',
' static propTypes = {',
' unused: PropTypes.bool',
' }',
' shouldComponentUpdate (nextProps, nextState) {',
' const {something} = nextProps;',
' return something;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions,
errors: [{
message: '\'unused\' PropType is defined but prop is never used',
line: 3,
column: 13
}]
}, {
code: [
'class Hello extends Component {',
' static propTypes = {',
' unused: PropTypes.bool',
' }',
' componentWillUpdate (nextProps, nextState) {',
' const {something} = nextProps;',
' return something;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions,
errors: [{
message: '\'unused\' PropType is defined but prop is never used',
line: 3,
column: 13
}]
}, {
code: [
'class Hello extends Component {',
' static propTypes = {',
' unused: PropTypes.bool',
' }',
' componentDidUpdate (prevProps, prevState) {',
' const {something} = prevProps;',
' return something;',
' }',
'}'
].join('\n'),
parser: 'babel-eslint',
parserOptions: parserOptions,
errors: [{
message: '\'unused\' PropType is defined but prop is never used',
line: 3,
column: 13
}]
}/* , {
// Enable this when the following issue is fixed
// https://github.com/yannickcr/eslint-plugin-react/issues/296
Expand Down

0 comments on commit 070b489

Please sign in to comment.