Skip to content

Commit

Permalink
Fix: no-useless-computed-key invalid autofix for getters and setters (#…
Browse files Browse the repository at this point in the history
…8335)

Previously, the no-useless-computed-key autofixer could sometimes place a key next to `get`, `set`, or `async` identifier in a property, causing the identifier and the key to combine and become a new identifier. This commit updates the autofixer to insert a space whenever that would happen to prevent the identifiers from combining.
  • Loading branch information
not-an-aardvark committed Mar 28, 2017
1 parent 0541eaf commit 29f4ba5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/rules/no-useless-computed-key.js
Expand Up @@ -9,6 +9,7 @@
//------------------------------------------------------------------------------

const astUtils = require("../ast-utils");
const esUtils = require("esutils");

//------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -55,7 +56,17 @@ module.exports = {
// 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);

const tokenBeforeLeftBracket = sourceCode.getTokenBefore(leftSquareBracket);

// Insert a space before the key to avoid changing identifiers, e.g. ({ get[2]() {} }) to ({ get2() {} })
const needsSpaceBeforeKey = tokenBeforeLeftBracket.range[1] === leftSquareBracket.range[0] &&
esUtils.code.isIdentifierPartES6(tokenBeforeLeftBracket.value.slice(-1).charCodeAt(0)) &&
esUtils.code.isIdentifierPartES6(key.raw.charCodeAt(0));

const replacementKey = (needsSpaceBeforeKey ? " " : "") + key.raw;

return fixer.replaceTextRange([leftSquareBracket.range[0], rightSquareBracket.range[1]], replacementKey);
}
});
}
Expand Down
75 changes: 75 additions & 0 deletions tests/lib/rules/no-useless-computed-key.js
Expand Up @@ -87,6 +87,81 @@ ruleTester.run("no-useless-computed-key", rule, {
errors: [{
message: "Unnecessarily computed property ['x'] found.", type: "Property"
}]
}, {
code: "({ get[.2]() {} })",
output: "({ get.2() {} })",
errors: [{
message: "Unnecessarily computed property [.2] found.", type: "Property"
}]
}, {
code: "({ set[.2](value) {} })",
output: "({ set.2(value) {} })",
errors: [{
message: "Unnecessarily computed property [.2] found.", type: "Property"
}]
}, {
code: "({ async[.2]() {} })",
output: "({ async.2() {} })",
parserOptions: { ecmaVersion: 8 },
errors: [{
message: "Unnecessarily computed property [.2] found.", type: "Property"
}]
}, {
code: "({ [2]() {} })",
output: "({ 2() {} })",
errors: [{
message: "Unnecessarily computed property [2] found.", type: "Property"
}]
}, {
code: "({ get [2]() {} })",
output: "({ get 2() {} })",
errors: [{
message: "Unnecessarily computed property [2] found.", type: "Property"
}]
}, {
code: "({ set [2](value) {} })",
output: "({ set 2(value) {} })",
errors: [{
message: "Unnecessarily computed property [2] found.", type: "Property"
}]
}, {
code: "({ async [2]() {} })",
output: "({ async 2() {} })",
parserOptions: { ecmaVersion: 8 },
errors: [{
message: "Unnecessarily computed property [2] found.", type: "Property"
}]
}, {
code: "({ get[2]() {} })",
output: "({ get 2() {} })",
errors: [{
message: "Unnecessarily computed property [2] found.", type: "Property"
}]
}, {
code: "({ set[2](value) {} })",
output: "({ set 2(value) {} })",
errors: [{
message: "Unnecessarily computed property [2] found.", type: "Property"
}]
}, {
code: "({ async[2]() {} })",
output: "({ async 2() {} })",
parserOptions: { ecmaVersion: 8 },
errors: [{
message: "Unnecessarily computed property [2] found.", type: "Property"
}]
}, {
code: "({ get['foo']() {} })",
output: "({ get'foo'() {} })",
errors: [{
message: "Unnecessarily computed property ['foo'] found.", type: "Property"
}]
}, {
code: "({ *[2]() {} })",
output: "({ *2() {} })",
errors: [{
message: "Unnecessarily computed property [2] found.", type: "Property"
}]
}
]
});

0 comments on commit 29f4ba5

Please sign in to comment.