Skip to content

Commit

Permalink
Update: add fixer for no-useless-computed-key (#7207)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark authored and kaicataldo committed Sep 30, 2016
1 parent 18376cf commit c36d842
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/rules/no-useless-computed-key.md
@@ -1,5 +1,7 @@
# Disallow unnecessary computed property keys on objects (no-useless-computed-key)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.

It's unnecessary to use computed properties with literals such as:

```js
Expand Down
23 changes: 21 additions & 2 deletions lib/rules/no-useless-computed-key.js
Expand Up @@ -18,7 +18,9 @@ module.exports = {
recommended: false
},

schema: []
schema: [],

fixable: "code"
},
create(context) {
const sourceCode = context.getSourceCode();
Expand All @@ -33,7 +35,24 @@ module.exports = {
nodeType = typeof key.value;

if (key.type === "Literal" && (nodeType === "string" || nodeType === "number")) {
context.report(node, MESSAGE_UNNECESSARY_COMPUTED, { property: sourceCode.getText(key) });
context.report({
node,
message: MESSAGE_UNNECESSARY_COMPUTED,
data: { property: sourceCode.getText(key) },
fix(fixer) {
const leftSquareBracket = sourceCode.getFirstToken(node, node.value.generator || node.value.async ? 1 : 0);
const rightSquareBracket = sourceCode.getTokensBetween(node.key, node.value).find(token => token.value === "]");

const tokensBetween = sourceCode.getTokensBetween(leftSquareBracket, rightSquareBracket, 1);

if (tokensBetween.slice(0, -1).some((token, index) => sourceCode.getText().slice(token.range[1], tokensBetween[index + 1].range[0]).trim())) {

// If there are comments between the brackets and the property name, don't do a fix.
return null;
}
return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], key.raw);
}
});
}
}
};
Expand Down
40 changes: 40 additions & 0 deletions tests/lib/rules/no-useless-computed-key.js
Expand Up @@ -27,34 +27,74 @@ ruleTester.run("no-useless-computed-key", rule, {
invalid: [
{
code: "({ ['0']: 0 })",
output: "({ '0': 0 })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property ['0'] found.", type: "Property"
}]
}, {
code: "({ ['0+1,234']: 0 })",
output: "({ '0+1,234': 0 })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property ['0+1,234'] found.", type: "Property"
}]
}, {
code: "({ [0]: 0 })",
output: "({ 0: 0 })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property [0] found.", type: "Property"
}]
}, {
code: "({ ['x']: 0 })",
output: "({ 'x': 0 })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property ['x'] found.", type: "Property"
}]
}, {
code: "({ ['x']() {} })",
output: "({ 'x'() {} })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property ['x'] found.", type: "Property"
}]
}, {
code: "({ [/* this comment prevents a fix */ 'x']: 0 })",
output: "({ [/* this comment prevents a fix */ 'x']: 0 })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property ['x'] found.", type: "Property"
}]
}, {
code: "({ ['x' /* this comment also prevents a fix */]: 0 })",
output: "({ ['x' /* this comment also prevents a fix */]: 0 })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property ['x'] found.", type: "Property"
}]
}, {
code: "({ [('x')]: 0 })",
output: "({ 'x': 0 })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property ['x'] found.", type: "Property"
}]
}, {
code: "({ *['x']() {} })",
output: "({ *'x'() {} })",
env: {es6: true},
errors: [{
message: "Unnecessarily computed property ['x'] found.", type: "Property"
}]
}, {
code: "({ async ['x']() {} })",
output: "({ async 'x'() {} })",
parserOptions: { ecmaVersion: 8 },
errors: [{
message: "Unnecessarily computed property ['x'] found.", type: "Property"
}]
}
]
});

0 comments on commit c36d842

Please sign in to comment.