Skip to content

Commit 8f00023

Browse files
golopotljharb
authored andcommitted
[Fix] prop-types: fix false positives on renames in object destructuring
Fixes #2944
1 parent 0132c47 commit 8f00023

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
88
### Fixed
99
* [`no-invalid-html-attribute`]: allow `link` `rel` to have `apple-touch-icon`, `mask-icon` ([#3132][] @ljharb)
1010
* [`no-unused-class-component-methods`]: add `getChildContext` lifecycle method ([#3136][] @yoyo837)
11+
* [`prop-types`]: fix false positives on renames in object destructuring ([#3142][] @golopot)
1112

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

16+
[#3142]: https://github.com/yannickcr/eslint-plugin-react/pull/3142
1517
[#3141]: https://github.com/yannickcr/eslint-plugin-react/pull/3141
1618
[#3136]: https://github.com/yannickcr/eslint-plugin-react/pull/3136
1719
[#3132]: https://github.com/yannickcr/eslint-plugin-react/issue/3132

lib/util/usedPropTypes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ module.exports = function usedPropTypesInstructions(context, components, utils)
395395
if (properties[k].value.type === 'ObjectPattern') {
396396
markPropTypesAsUsed(properties[k].value, parentNames.concat([propName]));
397397
} else if (properties[k].value.type === 'Identifier') {
398-
propVariables.set(propName, parentNames.concat(propName));
398+
propVariables.set(properties[k].value.name, parentNames.concat(propName));
399399
}
400400
}
401401
break;

tests/lib/rules/no-unused-prop-types.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4921,7 +4921,7 @@ ruleTester.run('no-unused-prop-types', rule, {
49214921
foo: PropTypes.string,
49224922
bar: PropTypes.string,
49234923
};
4924-
4924+
49254925
componentWillUpdate (nextProps) {
49264926
if (nextProps.foo) {
49274927
return true;
@@ -4994,7 +4994,7 @@ ruleTester.run('no-unused-prop-types', rule, {
49944994
foo: PropTypes.string,
49954995
bar: PropTypes.string,
49964996
};
4997-
4997+
49984998
shouldComponentUpdate (nextProps) {
49994999
if (nextProps.foo) {
50005000
return true;
@@ -5086,7 +5086,7 @@ ruleTester.run('no-unused-prop-types', rule, {
50865086
foo: PropTypes.string,
50875087
bar: PropTypes.string,
50885088
};
5089-
5089+
50905090
componentDidUpdate (nextProps) {
50915091
if (nextProps.foo) {
50925092
return true;
@@ -5319,7 +5319,7 @@ ruleTester.run('no-unused-prop-types', rule, {
53195319
{
53205320
// None of the props are used issue #1162
53215321
code: `
5322-
import React from "react";
5322+
import React from "react";
53235323
var Hello = React.createReactClass({
53245324
propTypes: {
53255325
name: React.PropTypes.string

tests/lib/rules/prop-types.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3542,6 +3542,28 @@ ruleTester.run('prop-types', rule, {
35423542
}
35433543
`,
35443544
features: ['class fields'],
3545+
},
3546+
3547+
// #2944
3548+
{
3549+
code: `
3550+
const styles = { width: 0 };
3551+
3552+
function Foo(props) {
3553+
const { styles: x } = props;
3554+
return (
3555+
<p>
3556+
{styles.width} {x._}
3557+
</p>
3558+
);
3559+
}
3560+
3561+
Foo.propTypes = {
3562+
styles: PropTypes.shape({
3563+
_: PropTypes.number,
3564+
}),
3565+
};
3566+
`,
35453567
}
35463568
)),
35473569

0 commit comments

Comments
 (0)