Skip to content

Commit f7d50c2

Browse files
committed
[Fix] no-unused-state: fix a crash from 9b227aa
Fixes #3204
1 parent 492b5f1 commit f7d50c2

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

lib/rules/no-unused-state.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ module.exports = {
319319
},
320320

321321
'ClassProperty, PropertyDefinition'(node) {
322+
if (!classInfo) {
323+
return;
324+
}
322325
// If we see state being assigned as a class property using an object
323326
// expression, record all the fields of that object as state fields.
324327
const unwrappedValueNode = ast.unwrapTSAsExpression(node.value);

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,7 +1009,42 @@ eslintTester.run('no-unused-state', rule, {
10091009
}
10101010
`,
10111011
features: ['ts', 'no-babel-old', 'no-ts-old'],
1012-
} : []
1012+
} : [],
1013+
{
1014+
code: `
1015+
class Foo extends React.Component {
1016+
onCancel = (data) => {
1017+
console.log('Cancelled', data)
1018+
this.setState({ status: 'Cancelled. Try again?' })
1019+
}
1020+
render() {
1021+
const { status } = this.state;
1022+
return <div>{status}</div>
1023+
}
1024+
}
1025+
`,
1026+
features: ['class fields'],
1027+
},
1028+
{
1029+
code: `
1030+
class KarmaRefundPillComponent extends GenericPillComponent {
1031+
renderContent = () => {
1032+
const { action } = this.props
1033+
1034+
return (
1035+
<Box fontSize={[1]} mx={[2]} minWidth="10px" minHeight="26px" alignItems="center">
1036+
<FormattedText
1037+
fields={getKarmaClaimLevel1Fields(action)}
1038+
i18nKey="pillTemplates.karmarefund.summary"
1039+
fontSize={[1]}
1040+
/>
1041+
</Box>
1042+
)
1043+
}
1044+
}
1045+
`,
1046+
features: ['ts'],
1047+
}
10131048
)),
10141049

10151050
invalid: parsers.all([

0 commit comments

Comments
 (0)