Skip to content

Commit 195d008

Browse files
committed
Account for template literal with no expression
1 parent cd160c1 commit 195d008

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

lib/rules/no-unused-state.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ function uncast(node) {
2525
// method definitions, ObjectExpression property keys).
2626
function getName(node) {
2727
node = uncast(node);
28-
if (node.type === 'Identifier') {
28+
const type = node.type;
29+
30+
if (type === 'Identifier') {
2931
return node.name;
30-
} else if (node.type === 'Literal') {
32+
} else if (type === 'Literal') {
3133
return String(node.value);
34+
} else if (type === 'TemplateLiteral' && node.expressions.length === 0) {
35+
return node.quasis[0].value.raw;
3236
}
3337
return null;
3438
}
@@ -97,6 +101,7 @@ module.exports = {
97101
if (
98102
prop.type === 'Property' &&
99103
(key.type === 'Literal' ||
104+
(key.type === 'TemplateLiteral' && key.expressions.length === 0) ||
100105
(prop.computed === false && key.type === 'Identifier')) &&
101106
getName(prop.key) !== null
102107
) {

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ eslintTester.run('no-unused-state', rule, {
102102
return <SomeComponent />;
103103
}
104104
});`,
105+
`var ComputedKeyFromTemplateLiteralTest = createReactClass({
106+
getInitialState: function() {
107+
return { [\`foo\`]: 0 };
108+
},
109+
render: function() {
110+
return <SomeComponent foo={this.state['foo']} />;
111+
}
112+
});`,
105113
`var GetInitialStateMethodTest = createReactClass({
106114
getInitialState() {
107115
return { foo: 0 };
@@ -198,6 +206,14 @@ eslintTester.run('no-unused-state', rule, {
198206
return <SomeComponent />;
199207
}
200208
}`,
209+
`class ComputedKeyFromTemplateLiteralTest extends React.Component {
210+
constructor() {
211+
this.state = { [\`foo\`]: 0 };
212+
}
213+
render() {
214+
return <SomeComponent foo={this.state.foo} />;
215+
}
216+
}`,
201217
`class SetStateTest extends React.Component {
202218
onFooChange(newFoo) {
203219
this.setState({ foo: newFoo });
@@ -407,6 +423,17 @@ eslintTester.run('no-unused-state', rule, {
407423
})`,
408424
errors: getErrorMessages(['foo'])
409425
},
426+
{
427+
code: `var UnusedComputedTemplateLiteralKeyStateTest = createReactClass({
428+
getInitialState: function() {
429+
return { [\`foo\`]: 0 };
430+
},
431+
render: function() {
432+
return <SomeComponent />;
433+
}
434+
})`,
435+
errors: getErrorMessages(['foo'])
436+
},
410437
{
411438
code: `var UnusedComputedNumberLiteralKeyStateTest = createReactClass({
412439
getInitialState: function() {
@@ -493,6 +520,26 @@ eslintTester.run('no-unused-state', rule, {
493520
errors: getErrorMessages(['foo']),
494521
parser: 'babel-eslint'
495522
},
523+
{
524+
code: `class UnusedComputedTemplateLiteralKeyStateTest extends React.Component {
525+
state = { [\`foo\`]: 0 };
526+
render() {
527+
return <SomeComponent />;
528+
}
529+
}`,
530+
errors: getErrorMessages(['foo']),
531+
parser: 'babel-eslint'
532+
},
533+
{
534+
code: `class UnusedComputedTemplateLiteralKeyStateTest extends React.Component {
535+
state = { [\`foo \\n bar\`]: 0 };
536+
render() {
537+
return <SomeComponent />;
538+
}
539+
}`,
540+
errors: getErrorMessages(['foo \\n bar']),
541+
parser: 'babel-eslint'
542+
},
496543
{
497544
code: `class UnusedComputedBooleanLiteralKeyStateTest extends React.Component {
498545
state = { [true]: 0 };

0 commit comments

Comments
 (0)