Skip to content

Commit 93de066

Browse files
authored
fix: prefer-exponentiation-operator autofix for async function base (#21322)
* fix: prefer-exponentiation-operator autofix for async function base Fixes #21319 * fix: also accept Keyword token type for async
1 parent 02e34ff commit 93de066

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

‎lib/rules/prefer-exponentiation-operator.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,19 @@ module.exports = {
174174
isStartOfExpressionStatement
175175
) {
176176
const firstTokenOfBase = sourceCode.getFirstToken(base);
177+
const secondTokenOfBase =
178+
sourceCode.getTokenAfter(firstTokenOfBase);
177179

178180
if (
179181
astUtils.isOpeningBraceToken(firstTokenOfBase) ||
180182
(firstTokenOfBase.type === "Keyword" &&
181183
(firstTokenOfBase.value === "function" ||
182-
firstTokenOfBase.value === "class"))
184+
firstTokenOfBase.value === "class")) ||
185+
((firstTokenOfBase.type === "Identifier" ||
186+
firstTokenOfBase.type === "Keyword") &&
187+
firstTokenOfBase.value === "async" &&
188+
secondTokenOfBase.type === "Keyword" &&
189+
secondTokenOfBase.value === "function")
183190
) {
184191
shouldParenthesizeAll = true;
185192
}

‎tests/lib/rules/prefer-exponentiation-operator.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,37 @@ ruleTester.run("prefer-exponentiation-operator", rule, {
487487
"(class{static x=2}.x + 100)**4;",
488488
),
489489

490+
// https://github.com/eslint/eslint/issues/21319
491+
invalid(
492+
"Math.pow(async function(){}.length, 2);",
493+
"(async function(){}.length**2);",
494+
),
495+
invalid(
496+
"Math.pow(async function handler(first, second) {}.length, 2);",
497+
"(async function handler(first, second) {}.length**2);",
498+
),
499+
invalid(
500+
"Math.pow(async function(){}.length, 2) + 100;",
501+
"(async function(){}.length**2) + 100;",
502+
),
503+
invalid(
504+
"(Math.pow(async function(){}.length, 2));",
505+
"(async function(){}.length**2);",
506+
),
507+
invalid(
508+
"100 + Math.pow(async function(){}.length, 2);",
509+
"100 + async function(){}.length**2;",
510+
),
511+
invalid(
512+
"Math.pow(async function(){}.length + 100, 2);",
513+
"(async function(){}.length + 100)**2;",
514+
),
515+
invalid("Math.pow(async.length, 2);", "async.length**2;"),
516+
invalid(
517+
"foo\nMath.pow(async function(){}.length, 2)",
518+
"foo\n;(async function(){}.length**2)",
519+
),
520+
490521
// preceding semicolon needed
491522
invalid("foo\nMath.pow(a + b, c)", "foo\n;(a + b)**c"),
492523
invalid("foo\nMath.pow(+a, b)", "foo\n;(+a)**b"),

0 commit comments

Comments
 (0)