Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positives inside lifecycle methods #2099

Merged
merged 1 commit into from Dec 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/util/usedPropTypes.js
Expand Up @@ -285,13 +285,14 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
* @returns {Boolean} True if we are using a prop, false if not.
*/
function isPropTypesUsage(node) {
const isThisPropsUsage = node.object.type === 'ThisExpression' && node.property.name === 'props';
const isPropsUsage = isThisPropsUsage || node.object.name === 'nextProps' || node.object.name === 'prevProps';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix!
Later on it'd be great not to depend on the names (similar to #1829), but for now not crashing is the priority 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

const isClassUsage = (
(utils.getParentES6Component() || utils.getParentES5Component()) &&
((node.object.type === 'ThisExpression' && node.property.name === 'props')
|| isPropArgumentInSetStateUpdater(node))
(isThisPropsUsage || isPropArgumentInSetStateUpdater(node))
);
const isStatelessFunctionUsage = node.object.name === 'props' && !isAssignmentToProp(node);
return isClassUsage || isStatelessFunctionUsage || inLifeCycleMethod();
return isClassUsage || isStatelessFunctionUsage || (isPropsUsage && inLifeCycleMethod());
}

/**
Expand Down
77 changes: 77 additions & 0 deletions tests/lib/rules/prop-types.js
Expand Up @@ -2170,6 +2170,83 @@ ruleTester.run('prop-types', rule, {
pragma: 'Foo'
}
}
},
{
code: `
class Foo extends React.Component {
propTypes = {
actions: PropTypes.object.isRequired,
};
componentWillReceiveProps (nextProps) {
this.props.actions.doSomething();
}

componentWillUnmount () {
this.props.actions.doSomething();
}

render() {
return <div>foo</div>;
}
}
`,
parser: 'babel-eslint'
},
{
code: `
class Foo extends React.Component {
componentDidUpdate() { this.inputRef.focus(); }
render() {
return (
<div>
<input ref={(node) => { this.inputRef = node; }} />
</div>
)
}
}
`
},
{
code: `
class Foo extends React.Component {
componentDidUpdate(nextProps, nextState) {
const {
first_organization,
second_organization,
} = this.state;
return true;
}
render() {
return <div>hi</div>;
}
}
`
},
{
code: `
class Foo extends React.Component {
shouldComponentUpdate(nextProps) {
if (this.props.search !== nextProps.search) {
let query = nextProps.query;
let result = nextProps.list.filter(item => {
return (item.name.toLowerCase().includes(query.trim().toLowerCase()));
});

this.setState({ result });

return true;
}
}
render() {
return <div>foo</div>;
}
}
Foo.propTypes = {
search: PropTypes.object,
list: PropTypes.array,
query: PropTypes.string,
};
`
}
],

Expand Down