Skip to content

Commit 35439f1

Browse files
authored
fix: correct syntax error in prefer-arrow-callback autofix (#16722)
Fixes #16718
1 parent 87b2470 commit 35439f1

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

lib/rules/prefer-arrow-callback.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ module.exports = {
335335
// Convert the function expression to an arrow function.
336336
const functionToken = sourceCode.getFirstToken(node, node.async ? 1 : 0);
337337
const leftParenToken = sourceCode.getTokenAfter(functionToken, astUtils.isOpeningParenToken);
338+
const tokenBeforeBody = sourceCode.getTokenBefore(node.body);
338339

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

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

353354
// Get the node that will become the new arrow function.
354355
let replacedNode = callbackInfo.isLexicalThis ? node.parent.parent : node;

tests/lib/rules/prefer-arrow-callback.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,46 @@ ruleTester.run("prefer-arrow-callback", rule, {
205205
code: "foo((function() { return this; }?.bind)(this));",
206206
output: null,
207207
errors
208+
},
209+
210+
// https://github.com/eslint/eslint/issues/16718
211+
{
212+
code: `
213+
test(
214+
function ()
215+
{ }
216+
);
217+
`,
218+
output: `
219+
test(
220+
() =>
221+
{ }
222+
);
223+
`,
224+
errors
225+
},
226+
{
227+
code: `
228+
test(
229+
function (
230+
...args
231+
) /* Lorem ipsum
232+
dolor sit amet. */ {
233+
return args;
234+
}
235+
);
236+
`,
237+
output: `
238+
test(
239+
(
240+
...args
241+
) => /* Lorem ipsum
242+
dolor sit amet. */ {
243+
return args;
244+
}
245+
);
246+
`,
247+
errors
208248
}
209249
]
210250
});

0 commit comments

Comments
 (0)