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 error in prefer-arrow-callback autofix #16722

Merged
merged 1 commit into from Dec 31, 2022
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
3 changes: 2 additions & 1 deletion lib/rules/prefer-arrow-callback.js
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
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
}
]
});