Skip to content

Commit c36d842

Browse files
not-an-aardvarkkaicataldo
authored andcommitted
Update: add fixer for no-useless-computed-key (#7207)
1 parent 18376cf commit c36d842

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

docs/rules/no-useless-computed-key.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Disallow unnecessary computed property keys on objects (no-useless-computed-key)
22

3+
(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.
4+
35
It's unnecessary to use computed properties with literals such as:
46

57
```js

lib/rules/no-useless-computed-key.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ module.exports = {
1818
recommended: false
1919
},
2020

21-
schema: []
21+
schema: [],
22+
23+
fixable: "code"
2224
},
2325
create(context) {
2426
const sourceCode = context.getSourceCode();
@@ -33,7 +35,24 @@ module.exports = {
3335
nodeType = typeof key.value;
3436

3537
if (key.type === "Literal" && (nodeType === "string" || nodeType === "number")) {
36-
context.report(node, MESSAGE_UNNECESSARY_COMPUTED, { property: sourceCode.getText(key) });
38+
context.report({
39+
node,
40+
message: MESSAGE_UNNECESSARY_COMPUTED,
41+
data: { property: sourceCode.getText(key) },
42+
fix(fixer) {
43+
const leftSquareBracket = sourceCode.getFirstToken(node, node.value.generator || node.value.async ? 1 : 0);
44+
const rightSquareBracket = sourceCode.getTokensBetween(node.key, node.value).find(token => token.value === "]");
45+
46+
const tokensBetween = sourceCode.getTokensBetween(leftSquareBracket, rightSquareBracket, 1);
47+
48+
if (tokensBetween.slice(0, -1).some((token, index) => sourceCode.getText().slice(token.range[1], tokensBetween[index + 1].range[0]).trim())) {
49+
50+
// If there are comments between the brackets and the property name, don't do a fix.
51+
return null;
52+
}
53+
return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], key.raw);
54+
}
55+
});
3756
}
3857
}
3958
};

tests/lib/rules/no-useless-computed-key.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,34 +27,74 @@ ruleTester.run("no-useless-computed-key", rule, {
2727
invalid: [
2828
{
2929
code: "({ ['0']: 0 })",
30+
output: "({ '0': 0 })",
3031
env: {es6: true},
3132
errors: [{
3233
message: "Unnecessarily computed property ['0'] found.", type: "Property"
3334
}]
3435
}, {
3536
code: "({ ['0+1,234']: 0 })",
37+
output: "({ '0+1,234': 0 })",
3638
env: {es6: true},
3739
errors: [{
3840
message: "Unnecessarily computed property ['0+1,234'] found.", type: "Property"
3941
}]
4042
}, {
4143
code: "({ [0]: 0 })",
44+
output: "({ 0: 0 })",
4245
env: {es6: true},
4346
errors: [{
4447
message: "Unnecessarily computed property [0] found.", type: "Property"
4548
}]
4649
}, {
4750
code: "({ ['x']: 0 })",
51+
output: "({ 'x': 0 })",
4852
env: {es6: true},
4953
errors: [{
5054
message: "Unnecessarily computed property ['x'] found.", type: "Property"
5155
}]
5256
}, {
5357
code: "({ ['x']() {} })",
58+
output: "({ 'x'() {} })",
5459
env: {es6: true},
5560
errors: [{
5661
message: "Unnecessarily computed property ['x'] found.", type: "Property"
5762
}]
63+
}, {
64+
code: "({ [/* this comment prevents a fix */ 'x']: 0 })",
65+
output: "({ [/* this comment prevents a fix */ 'x']: 0 })",
66+
env: {es6: true},
67+
errors: [{
68+
message: "Unnecessarily computed property ['x'] found.", type: "Property"
69+
}]
70+
}, {
71+
code: "({ ['x' /* this comment also prevents a fix */]: 0 })",
72+
output: "({ ['x' /* this comment also prevents a fix */]: 0 })",
73+
env: {es6: true},
74+
errors: [{
75+
message: "Unnecessarily computed property ['x'] found.", type: "Property"
76+
}]
77+
}, {
78+
code: "({ [('x')]: 0 })",
79+
output: "({ 'x': 0 })",
80+
env: {es6: true},
81+
errors: [{
82+
message: "Unnecessarily computed property ['x'] found.", type: "Property"
83+
}]
84+
}, {
85+
code: "({ *['x']() {} })",
86+
output: "({ *'x'() {} })",
87+
env: {es6: true},
88+
errors: [{
89+
message: "Unnecessarily computed property ['x'] found.", type: "Property"
90+
}]
91+
}, {
92+
code: "({ async ['x']() {} })",
93+
output: "({ async 'x'() {} })",
94+
parserOptions: { ecmaVersion: 8 },
95+
errors: [{
96+
message: "Unnecessarily computed property ['x'] found.", type: "Property"
97+
}]
5898
}
5999
]
60100
});

0 commit comments

Comments
 (0)