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: arrow-body-style crash with object literal body (fixes #12884) #12886

Merged
merged 1 commit into from Feb 28, 2020
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
41 changes: 26 additions & 15 deletions lib/rules/arrow-body-style.js
Expand Up @@ -91,7 +91,7 @@ module.exports = {
* @returns {Token} The found closing parenthesis token.
*/
function findClosingParen(token) {
let node = sourceCode.getNodeByRangeIndex(token.range[1]);
let node = sourceCode.getNodeByRangeIndex(token.range[0]);

while (!astUtils.isParenthesised(sourceCode, node)) {
node = node.parent;
Expand Down Expand Up @@ -206,24 +206,35 @@ module.exports = {
fix(fixer) {
const fixes = [];
const arrowToken = sourceCode.getTokenBefore(arrowBody, astUtils.isArrowToken);
const firstBodyToken = sourceCode.getTokenAfter(arrowToken);
const lastBodyToken = sourceCode.getLastToken(node);
const [firstTokenAfterArrow, secondTokenAfterArrow] = sourceCode.getTokensAfter(arrowToken, { count: 2 });
const lastToken = sourceCode.getLastToken(node);
const isParenthesisedObjectLiteral =
astUtils.isOpeningParenToken(firstBodyToken) &&
astUtils.isOpeningBraceToken(sourceCode.getTokenAfter(firstBodyToken));

// Wrap the value by a block and a return statement.
fixes.push(
fixer.insertTextBefore(firstBodyToken, "{return "),
fixer.insertTextAfter(lastBodyToken, "}")
);
astUtils.isOpeningParenToken(firstTokenAfterArrow) &&
astUtils.isOpeningBraceToken(secondTokenAfterArrow);

// If the value is object literal, remove parentheses which were forced by syntax.
if (isParenthesisedObjectLiteral) {
fixes.push(
fixer.remove(firstBodyToken),
fixer.remove(findClosingParen(firstBodyToken))
);
const openingParenToken = firstTokenAfterArrow;
const openingBraceToken = secondTokenAfterArrow;

if (astUtils.isTokenOnSameLine(openingParenToken, openingBraceToken)) {
fixes.push(fixer.replaceText(openingParenToken, "{return "));
} else {

// Avoid ASI
fixes.push(
fixer.replaceText(openingParenToken, "{"),
fixer.insertTextBefore(openingBraceToken, "return ")
);
}

// Closing paren for the object doesn't have to be lastToken, e.g.: () => ({}).foo()
fixes.push(fixer.remove(findClosingParen(openingBraceToken)));
fixes.push(fixer.insertTextAfter(lastToken, "}"));

} else {
fixes.push(fixer.insertTextBefore(firstTokenAfterArrow, "{return "));
fixes.push(fixer.insertTextAfter(lastToken, "}"));
}

return fixes;
Expand Down
94 changes: 94 additions & 0 deletions tests/lib/rules/arrow-body-style.js
Expand Up @@ -45,6 +45,19 @@ ruleTester.run("arrow-body-style", rule, {
{ code: "var foo = () => { return { bar: 0 }; };", options: ["as-needed", { requireReturnForObjectLiteral: true }] }
],
invalid: [
{
code: "var foo = () => 0",
output: "var foo = () => {return 0}",
options: ["always"],
errors: [
{
line: 1,
column: 17,
type: "ArrowFunctionExpression",
messageId: "expectedBlock"
}
]
},
{
code: "var foo = () => 0;",
output: "var foo = () => {return 0};",
Expand All @@ -71,6 +84,45 @@ ruleTester.run("arrow-body-style", rule, {
}
]
},
{
code: "var foo = () => ( {});",
output: "var foo = () => {return {}};",
options: ["always"],
errors: [
{
line: 1,
column: 20,
type: "ArrowFunctionExpression",
messageId: "expectedBlock"
}
]
},
{
code: "(() => ({}))",
output: "(() => {return {}})",
options: ["always"],
errors: [
{
line: 1,
column: 9,
type: "ArrowFunctionExpression",
messageId: "expectedBlock"
}
]
},
{
code: "(() => ( {}))",
output: "(() => {return {}})",
options: ["always"],
errors: [
{
line: 1,
column: 10,
type: "ArrowFunctionExpression",
messageId: "expectedBlock"
}
]
},
{
code: "var foo = () => { return 0; };",
output: "var foo = () => 0;",
Expand Down Expand Up @@ -472,6 +524,48 @@ ruleTester.run("arrow-body-style", rule, {
output: "var foo = () => {return {foo: 1}.foo()};",
options: ["always"],
errors: [{ messageId: "expectedBlock" }]
},
{
code: "var foo = () => ( {foo: 1} ).foo();",
output: "var foo = () => {return {foo: 1} .foo()};",
options: ["always"],
errors: [{ messageId: "expectedBlock" }]
},
{
code: `
var foo = () => ({
bar: 1,
baz: 2
});
`,
output: `
var foo = () => {return {
bar: 1,
baz: 2
}};
`,
options: ["always"],
errors: [{ messageId: "expectedBlock" }]
},
{
code: `
parsedYears = _map(years, (year) => (
{
index : year,
title : splitYear(year)
}
));
`,
output: `
parsedYears = _map(years, (year) => {
return {
index : year,
title : splitYear(year)
}
});
`,
options: ["always"],
errors: [{ messageId: "expectedBlock" }]
}
]
});