Skip to content

Commit f2d8d26

Browse files
committed
[Fix] no-unused-state: avoid crashing on a class field function with destructured state
Fixes #3568
1 parent 0855e5f commit f2d8d26

File tree

3 files changed

+26
-7
lines changed

3 files changed

+26
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
1818
* [`jsx-first-prop-new-line`]: ensure autofix preserves generics in component name ([#3546][] @ljharb)
1919
* [`no-unknown-property`]: allow `fill` prop on `<symbol>` ([#3555][] @stefanprobst)
2020
* [`display-name`], [`prop-types`]: when checking for a capitalized name, ignore underscores entirely ([#3560][] @ljharb)
21+
* [`no-unused-state`]: avoid crashing on a class field function with destructured state ([#3568][] @ljharb)
2122

23+
[#3568]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3568
2224
[#3560]: https://github.com/jsx-eslint/eslint-plugin-react/issues/3560
2325
[#3555]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3555
2426
[#3548]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3548

lib/rules/no-unused-state.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -379,14 +379,16 @@ module.exports = {
379379
}
380380
const argVar = scope.variables.find((x) => x.name === stateArg.name);
381381

382-
const stateRefs = argVar.references;
382+
if (argVar) {
383+
const stateRefs = argVar.references;
383384

384-
stateRefs.forEach((ref) => {
385-
const identifier = ref.identifier;
386-
if (identifier && identifier.parent && identifier.parent.type === 'MemberExpression') {
387-
addUsedStateField(identifier.parent.property);
388-
}
389-
});
385+
stateRefs.forEach((ref) => {
386+
const identifier = ref.identifier;
387+
if (identifier && identifier.parent && identifier.parent.type === 'MemberExpression') {
388+
addUsedStateField(identifier.parent.property);
389+
}
390+
});
391+
}
390392
},
391393

392394
'PropertyDefinition:exit'(node) {

tests/lib/rules/no-unused-state.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,21 @@ eslintTester.run('no-unused-state', rule, {
10951095
parserOptions: {
10961096
sourceType: 'module',
10971097
},
1098+
},
1099+
{
1100+
code: `
1101+
class Component extends React.Component {
1102+
static getDerivedStateFromProps = ({value, disableAnimation}: ToggleProps, {isControlled, isOn}: ToggleState) => {
1103+
return { isControlled, isOn };
1104+
};
1105+
1106+
render() {
1107+
const { isControlled, isOn } = this.state;
1108+
return <div>{isControlled ? 'controlled' : ''}{isOn ? 'on' : ''}</div>;
1109+
}
1110+
}
1111+
`,
1112+
features: ['types', 'class fields'],
10981113
}
10991114
)),
11001115

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy