Skip to content

Commit

Permalink
fix: correct syntax error in prefer-arrow-callback autofix (#16722)
Browse files Browse the repository at this point in the history
Fixes #16718
  • Loading branch information
fasttime committed Dec 31, 2022
1 parent 87b2470 commit 35439f1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/rules/prefer-arrow-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ module.exports = {
// Convert the function expression to an arrow function.
const functionToken = sourceCode.getFirstToken(node, node.async ? 1 : 0);
const leftParenToken = sourceCode.getTokenAfter(functionToken, astUtils.isOpeningParenToken);
const tokenBeforeBody = sourceCode.getTokenBefore(node.body);

if (sourceCode.commentsExistBetween(functionToken, leftParenToken)) {

Expand All @@ -348,7 +349,7 @@ module.exports = {
// Remove extra tokens and spaces.
yield fixer.removeRange([functionToken.range[0], leftParenToken.range[0]]);
}
yield fixer.insertTextBefore(node.body, "=> ");
yield fixer.insertTextAfter(tokenBeforeBody, " =>");

// Get the node that will become the new arrow function.
let replacedNode = callbackInfo.isLexicalThis ? node.parent.parent : node;
Expand Down
40 changes: 40 additions & 0 deletions tests/lib/rules/prefer-arrow-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,46 @@ ruleTester.run("prefer-arrow-callback", rule, {
code: "foo((function() { return this; }?.bind)(this));",
output: null,
errors
},

// https://github.com/eslint/eslint/issues/16718
{
code: `
test(
function ()
{ }
);
`,
output: `
test(
() =>
{ }
);
`,
errors
},
{
code: `
test(
function (
...args
) /* Lorem ipsum
dolor sit amet. */ {
return args;
}
);
`,
output: `
test(
(
...args
) => /* Lorem ipsum
dolor sit amet. */ {
return args;
}
);
`,
errors
}
]
});

0 comments on commit 35439f1

Please sign in to comment.