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

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

Merged
merged 2 commits into from Nov 11, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
5 changes: 5 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.