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] prop-types: fix false positives on renames in object destructuring #3142

Merged
merged 1 commit into from
Nov 18, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
### Fixed
* [`no-invalid-html-attribute`]: allow `link` `rel` to have `apple-touch-icon`, `mask-icon` ([#3132][] @ljharb)
* [`no-unused-class-component-methods`]: add `getChildContext` lifecycle method ([#3136][] @yoyo837)
* [`prop-types`]: fix false positives on renames in object destructuring ([#3142][] @golopot)

### Changed
* [readme] fix syntax typo ([#3141][] @moselhy)

[#3142]: https://github.com/yannickcr/eslint-plugin-react/pull/3142
[#3141]: https://github.com/yannickcr/eslint-plugin-react/pull/3141
[#3136]: https://github.com/yannickcr/eslint-plugin-react/pull/3136
[#3132]: https://github.com/yannickcr/eslint-plugin-react/issue/3132
Expand Down
2 changes: 1 addition & 1 deletion lib/util/usedPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
if (properties[k].value.type === 'ObjectPattern') {
markPropTypesAsUsed(properties[k].value, parentNames.concat([propName]));
} else if (properties[k].value.type === 'Identifier') {
propVariables.set(propName, parentNames.concat(propName));
propVariables.set(properties[k].value.name, parentNames.concat(propName));
}
}
break;
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/rules/no-unused-prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -4921,7 +4921,7 @@ ruleTester.run('no-unused-prop-types', rule, {
foo: PropTypes.string,
bar: PropTypes.string,
};

componentWillUpdate (nextProps) {
if (nextProps.foo) {
return true;
Expand Down Expand Up @@ -4994,7 +4994,7 @@ ruleTester.run('no-unused-prop-types', rule, {
foo: PropTypes.string,
bar: PropTypes.string,
};

shouldComponentUpdate (nextProps) {
if (nextProps.foo) {
return true;
Expand Down Expand Up @@ -5086,7 +5086,7 @@ ruleTester.run('no-unused-prop-types', rule, {
foo: PropTypes.string,
bar: PropTypes.string,
};

componentDidUpdate (nextProps) {
if (nextProps.foo) {
return true;
Expand Down Expand Up @@ -5319,7 +5319,7 @@ ruleTester.run('no-unused-prop-types', rule, {
{
// None of the props are used issue #1162
code: `
import React from "react";
import React from "react";
var Hello = React.createReactClass({
propTypes: {
name: React.PropTypes.string
Expand Down
22 changes: 22 additions & 0 deletions tests/lib/rules/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -3542,6 +3542,28 @@ ruleTester.run('prop-types', rule, {
}
`,
features: ['class fields'],
},

// #2944
{
code: `
const styles = { width: 0 };

function Foo(props) {
const { styles: x } = props;
return (
<p>
{styles.width} {x._}
</p>
);
}

Foo.propTypes = {
styles: PropTypes.shape({
_: PropTypes.number,
}),
};
`,
}
)),

Expand Down