Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: add fixer for no-useless-computed-key #7207

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/rules/no-useless-computed-key.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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"
}]
}
]
});