Skip to content

Commit

Permalink
Fix: syntax errors created by object-shorthand autofix (fixes #7574) (
Browse files Browse the repository at this point in the history
#7575)

* Fix: syntax errors created by `object-shorthand` autofix (fixes #7574)

* Add additional tests
  • Loading branch information
not-an-aardvark authored and vitorbal committed Nov 11, 2016
1 parent 1b3b65c commit b8d6e48
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
31 changes: 21 additions & 10 deletions lib/rules/object-shorthand.js
Expand Up @@ -100,6 +100,7 @@ module.exports = {
const PARAMS = context.options[1] || {};
const IGNORE_CONSTRUCTORS = PARAMS.ignoreConstructors;
const AVOID_QUOTES = PARAMS.avoidQuotes;
const sourceCode = context.getSourceCode();

//--------------------------------------------------------------------------
// Helpers
Expand Down Expand Up @@ -296,14 +297,19 @@ module.exports = {
node,
message: "Expected method shorthand.",
fix(fixer) {

// NOTE: If this rule is enhanced to handle arrow functions as well, this logic needs to be updated.
const functionToken = sourceCode.getTokens(node).find(token => token.type === "Keyword" && token.value === "function");

if (node.value.generator) {
return fixer.replaceTextRange(
[node.key.range[0], node.value.range[0] + "function*".length],
`*[${node.key.name}]`
);
return fixer.replaceTextRange([sourceCode.getTokenBefore(node.key).range[0], sourceCode.getTokenAfter(functionToken).range[1]], `*[${sourceCode.getText(node.key)}]`);
}

if (node.value.async) {
return fixer.replaceTextRange([sourceCode.getTokenBefore(node.key).range[0], functionToken.range[1]], `async [${sourceCode.getText(node.key)}]`);
}

return fixer.removeRange([node.key.range[1] + 1, node.value.range[0] + "function".length]);
return fixer.removeRange([sourceCode.getTokenAfter(node.key).range[1], functionToken.range[1]]);
}
});
return;
Expand All @@ -314,14 +320,19 @@ module.exports = {
node,
message: "Expected method shorthand.",
fix(fixer) {

// NOTE: If this rule is enhanced to handle arrow functions as well, this logic needs to be updated.
const functionToken = sourceCode.getTokens(node).find(token => token.type === "Keyword" && token.value === "function");

if (node.value.generator) {
return fixer.replaceTextRange(
[node.key.range[0], node.value.range[0] + "function*".length],
`*${node.key.name}`
);
return fixer.replaceTextRange([node.key.range[0], sourceCode.getTokenAfter(functionToken).range[1]], `*${sourceCode.getText(node.key)}`);
}

if (node.value.async) {
return fixer.replaceTextRange([node.key.range[0], functionToken.range[1]], `async ${sourceCode.getText(node.key)}`);
}

return fixer.removeRange([node.key.range[1], node.value.range[0] + "function".length]);
return fixer.removeRange([node.key.range[1], functionToken.range[1]]);
}
});
} else if (node.value.type === "Identifier" && node.key.name === node.value.name && APPLY_TO_PROPS) {
Expand Down
9 changes: 9 additions & 0 deletions tests/lib/rules/object-shorthand.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b8d6e48

Please sign in to comment.