Skip to content

Commit ed44436

Browse files
committed
Do not take properties with computed keys into account for the no-unused-state rule
- unless the key is a literal
1 parent 44ca52b commit ed44436

File tree

2 files changed

+135
-1
lines changed

2 files changed

+135
-1
lines changed

lib/rules/no-unused-state.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ module.exports = {
9292
// current set of state fields.
9393
function addStateFields(node) {
9494
for (const prop of node.properties) {
95-
if (prop.type === 'Property' && getName(prop.key) !== null) {
95+
const key = prop.key;
96+
97+
if (
98+
prop.type === 'Property' &&
99+
(key.type === 'Literal' ||
100+
(prop.computed === false && key.type === 'Identifier')) &&
101+
getName(prop.key) !== null
102+
) {
96103
classInfo.stateFields.add(prop);
97104
}
98105
}

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

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,54 @@ eslintTester.run('no-unused-state', rule, {
4646
return <SomeComponent foo={this.state.foo} />;
4747
}
4848
});`,
49+
`var ComputedKeyFromVariableTest = createReactClass({
50+
getInitialState: function() {
51+
return { [foo]: 0 };
52+
},
53+
render: function() {
54+
return <SomeComponent />;
55+
}
56+
});`,
57+
`var ComputedKeyFromBooleanLiteralTest = createReactClass({
58+
getInitialState: function() {
59+
return { [true]: 0 };
60+
},
61+
render: function() {
62+
return <SomeComponent foo={this.state[true]} />;
63+
}
64+
});`,
65+
`var ComputedKeyFromExpressionTest = createReactClass({
66+
getInitialState: function() {
67+
return { [foo + bar]: 0 };
68+
},
69+
render: function() {
70+
return <SomeComponent />;
71+
}
72+
});`,
73+
`var ComputedKeyFromBinaryExpressionTest = createReactClass({
74+
getInitialState: function() {
75+
return { ['foo' + 'bar' * 8]: 0 };
76+
},
77+
render: function() {
78+
return <SomeComponent />;
79+
}
80+
});`,
81+
`var ComputedKeyFromStringLiteralTest = createReactClass({
82+
getInitialState: function() {
83+
return { ['foo']: 0 };
84+
},
85+
render: function() {
86+
return <SomeComponent foo={this.state.foo} />;
87+
}
88+
});`,
89+
`var ComputedKeyFromTemplateLiteralTest = createReactClass({
90+
getInitialState: function() {
91+
return { [\`foo\${bar}\`]: 0 };
92+
},
93+
render: function() {
94+
return <SomeComponent />;
95+
}
96+
});`,
4997
`var GetInitialStateMethodTest = createReactClass({
5098
getInitialState() {
5199
return { foo: 0 };
@@ -86,6 +134,54 @@ eslintTester.run('no-unused-state', rule, {
86134
return <SomeComponent foo={this.state.foo} />;
87135
}
88136
}`,
137+
`class ComputedKeyFromVariableTest extends React.Component {
138+
constructor() {
139+
this.state = { [foo]: 0 };
140+
}
141+
render() {
142+
return <SomeComponent />;
143+
}
144+
}`,
145+
`class ComputedKeyFromBooleanLiteralTest extends React.Component {
146+
constructor() {
147+
this.state = { [false]: 0 };
148+
}
149+
render() {
150+
return <SomeComponent foo={this.state['false']} />;
151+
}
152+
}`,
153+
`class ComputedKeyFromExpressionTest extends React.Component {
154+
constructor() {
155+
this.state = { [foo + bar]: 0 };
156+
}
157+
render() {
158+
return <SomeComponent />;
159+
}
160+
}`,
161+
`class ComputedKeyFromBinaryExpressionTest extends React.Component {
162+
constructor() {
163+
this.state = { [1 + 2 * 8]: 0 };
164+
}
165+
render() {
166+
return <SomeComponent />;
167+
}
168+
}`,
169+
`class ComputedKeyFromStringLiteralTest extends React.Component {
170+
constructor() {
171+
this.state = { ['foo']: 0 };
172+
}
173+
render() {
174+
return <SomeComponent foo={this.state.foo} />;
175+
}
176+
}`,
177+
`class ComputedKeyFromTemplateLiteralTest extends React.Component {
178+
constructor() {
179+
this.state = { [\`foo\${bar}\`]: 0 };
180+
}
181+
render() {
182+
return <SomeComponent />;
183+
}
184+
}`,
89185
`class SetStateTest extends React.Component {
90186
onFooChange(newFoo) {
91187
this.setState({ foo: newFoo });
@@ -284,6 +380,17 @@ eslintTester.run('no-unused-state', rule, {
284380
})`,
285381
errors: getErrorMessages(['foo'])
286382
},
383+
{
384+
code: `var UnusedComputedLiteralKeyStateTest = createReactClass({
385+
getInitialState: function() {
386+
return { ['foo']: 0 };
387+
},
388+
render: function() {
389+
return <SomeComponent />;
390+
}
391+
})`,
392+
errors: getErrorMessages(['foo'])
393+
},
287394
{
288395
code: `var UnusedGetInitialStateMethodTest = createReactClass({
289396
getInitialState() {
@@ -338,6 +445,26 @@ eslintTester.run('no-unused-state', rule, {
338445
errors: getErrorMessages(['foo']),
339446
parser: 'babel-eslint'
340447
},
448+
{
449+
code: `class UnusedComputedStringLiteralKeyStateTest extends React.Component {
450+
state = { ['foo']: 0 };
451+
render() {
452+
return <SomeComponent />;
453+
}
454+
}`,
455+
errors: getErrorMessages(['foo']),
456+
parser: 'babel-eslint'
457+
},
458+
{
459+
code: `class UnusedComputedBooleanLiteralKeyStateTest extends React.Component {
460+
state = { [true]: 0 };
461+
render() {
462+
return <SomeComponent />;
463+
}
464+
}`,
465+
errors: getErrorMessages(['true']),
466+
parser: 'babel-eslint'
467+
},
341468
{
342469
code: `class UnusedStateWhenPropsAreSpreadTest extends React.Component {
343470
constructor() {

0 commit comments

Comments
 (0)