Skip to content

Commit 182e95d

Browse files
committed
[Fix] destructuring-assignment: detect refs nested in functions
Fixes #3102
1 parent 8a0b352 commit 182e95d

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ This change log adheres to standards from [Keep a CHANGELOG](http://keepachangel
1515
* [`jsx-props-no-multi-spaces`]: avoid a crash on long member chains in tag names in `typescript-eslint` parser (@ljharb)
1616
* [`no-unused-prop-types`], `usedPropTypes`: avoid crash with typescript-eslint parser (@ljharb)
1717
* [`display-name`]: unwrap TS `as` expressions ([#3110][] @ljharb)
18+
* [`destructuring-assignment`]: detect refs nested in functions ([#3102] @ljharb)
1819

1920
[#3110]: https://github.com/yannickcr/eslint-plugin-react/pull/3110
21+
[#3102]: https://github.com/yannickcr/eslint-plugin-react/issue/3102
2022
[#3092]: https://github.com/yannickcr/eslint-plugin-react/pull/3092
2123
[#2166]: https://github.com/yannickcr/eslint-plugin-react/pull/2166
2224
[#1980]: https://github.com/yannickcr/eslint-plugin-react/pull/1980

lib/rules/destructuring-assignment.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,17 @@ module.exports = {
182182
'FunctionExpression:exit': handleStatelessComponentExit,
183183

184184
MemberExpression(node) {
185-
const SFCComponent = components.get(context.getScope(node).block);
186-
const classComponent = utils.getParentComponent(node);
185+
let scope = context.getScope(node);
186+
let SFCComponent = components.get(scope.block);
187+
while (!SFCComponent && scope.upper && scope.upper !== scope) {
188+
SFCComponent = components.get(scope.upper.block);
189+
scope = scope.upper;
190+
}
187191
if (SFCComponent) {
188192
handleSFCUsage(node);
189193
}
194+
195+
const classComponent = utils.getParentComponent(node);
190196
if (classComponent) {
191197
handleClassUsage(node);
192198
}

tests/lib/rules/destructuring-assignment.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,17 @@ ruleTester.run('destructuring-assignment', rule, {
529529
{
530530
messageId: 'useDestructAssignment',
531531
data: { type: 'props' },
532+
line: 5,
533+
},
534+
{
535+
messageId: 'useDestructAssignment',
536+
data: { type: 'props' },
537+
line: 7,
538+
},
539+
{
540+
messageId: 'useDestructAssignment',
541+
data: { type: 'props' },
542+
line: 8,
532543
},
533544
],
534545
},
@@ -565,5 +576,44 @@ ruleTester.run('destructuring-assignment', rule, {
565576
},
566577
],
567578
},
579+
{
580+
code: `
581+
import React from 'react';
582+
583+
const TestComp = (props) => {
584+
props.onClick3102();
585+
586+
return (
587+
<div
588+
onClick={(evt) => {
589+
if (props.onClick3102) {
590+
props.onClick3102(evt);
591+
}
592+
}}
593+
>
594+
<div />
595+
</div>
596+
);
597+
};
598+
`,
599+
parser: parsers.BABEL_ESLINT,
600+
errors: [
601+
{
602+
messageId: 'useDestructAssignment',
603+
data: { type: 'props' },
604+
line: 5,
605+
},
606+
{
607+
messageId: 'useDestructAssignment',
608+
data: { type: 'props' },
609+
line: 10,
610+
},
611+
{
612+
messageId: 'useDestructAssignment',
613+
data: { type: 'props' },
614+
line: 11,
615+
},
616+
],
617+
},
568618
]),
569619
});

0 commit comments

Comments
 (0)