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 pattern (fixes #14633) #14635

Merged
merged 1 commit into from May 28, 2021
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
32 changes: 21 additions & 11 deletions lib/rules/arrow-body-style.js
Expand Up @@ -87,17 +87,17 @@ module.exports = {
}

/**
* Gets the closing parenthesis which is the pair of the given opening parenthesis.
* @param {Token} token The opening parenthesis token to get.
* Gets the closing parenthesis by the given node.
* @param {ASTNode} node first node after an opening parenthesis.
* @returns {Token} The found closing parenthesis token.
*/
function findClosingParen(token) {
let node = sourceCode.getNodeByRangeIndex(token.range[0]);
function findClosingParen(node) {
let nodeToCheck = node;

while (!astUtils.isParenthesised(sourceCode, node)) {
node = node.parent;
while (!astUtils.isParenthesised(sourceCode, nodeToCheck)) {
nodeToCheck = nodeToCheck.parent;
}
return sourceCode.getTokenAfter(node);
return sourceCode.getTokenAfter(nodeToCheck);
}

/**
Expand Down Expand Up @@ -226,12 +226,22 @@ module.exports = {
const arrowToken = sourceCode.getTokenBefore(arrowBody, astUtils.isArrowToken);
const [firstTokenAfterArrow, secondTokenAfterArrow] = sourceCode.getTokensAfter(arrowToken, { count: 2 });
const lastToken = sourceCode.getLastToken(node);
const isParenthesisedObjectLiteral =

let parenthesisedObjectLiteral = null;

if (
astUtils.isOpeningParenToken(firstTokenAfterArrow) &&
astUtils.isOpeningBraceToken(secondTokenAfterArrow);
astUtils.isOpeningBraceToken(secondTokenAfterArrow)
) {
const braceNode = sourceCode.getNodeByRangeIndex(secondTokenAfterArrow.range[0]);

if (braceNode.type === "ObjectExpression") {
parenthesisedObjectLiteral = braceNode;
}
}

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

Expand All @@ -247,7 +257,7 @@ module.exports = {
}

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

} else {
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/arrow-body-style.js
Expand Up @@ -810,6 +810,14 @@ ruleTester.run("arrow-body-style", rule, {
`,
options: ["always"],
errors: [{ messageId: "expectedBlock" }]
},

// https://github.com/eslint/eslint/issues/14633
{
code: "const createMarker = (color) => ({ latitude, longitude }, index) => {};",
output: "const createMarker = (color) => {return ({ latitude, longitude }, index) => {}};",
options: ["always"],
errors: [{ messageId: "expectedBlock" }]
}
]
});