-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat: add suggestion to require-await
to remove async
keyword
#18716
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
feat: add suggestion to require-await
to remove async
keyword
#18716
Conversation
✅ Deploy Preview for docs-eslint canceled.
|
class A {
a = 0
async [b](){}
} class A {
a = 0
async * b(){}
} foo
async () => {} |
If we want to avoid removing comments, we could remove the range up to the first comment or token after const asyncToken = sourceCode.getFirstToken(nodeWithAsyncKeyword, token => token.value === "async");
return [asyncToken.range[0], sourceCode.getTokenAfter(asyncToken, { includeComments: true }).range[0]]; |
To avoid continuation, we could check if the token after |
Generator functions are not checked by the
What about using /*
* If the async token and the token before it cannot be adjacent,
* then ASI is currently being used. If the previous token and
* the next token can be placed next to each other, then removing
* the async keyword will change the meaning of the code.
*/
const previousToken = sourceCode.getTokenBefore(asyncToken, { includeComments: false });
if (previousToken && !astUtils.canTokensBeAdjacent(previousToken, asyncToken)) {
const nextToken = sourceCode.getTokenAfter(asyncToken, { includeComments: false });
if (astUtils.canTokensBeAdjacent(previousToken, nextToken)) {
return null;
}
} |
The rules a = 0
Object() with the suggestion of a = 0
;({}) or, similarly: foo
Array("a", "b") after the suggestion of foo
;["a", "b"] This is done by calling the |
It looks like the rule does not report problems for async generators, nor for empty functions. Any issue I'm missing with the above code? |
I didn't run this rule, I was looking at the fix logic yesterday, and bring up cases that remove |
Fair enough. It seems that we don't need to worry about the token after |
Unfortunately that function doesn't give the desired result for various cases and causes a semi-colon to be inserted when it shouldn't. -(async function() { doSomething() })
+(;function() { doSomething() }) -({ async foo() { doSomething() } })
+(;function() { doSomething() }) -class A { async foo() { doSomething() } }
+class A { ;foo() { doSomething() } } -(class { async foo() { doSomething() } })
+(class { ;foo() { doSomething() } }) -(class { async ''() { doSomething() } })
+(class { ;''() { doSomething() } }) -async function foo() { await (async () => { doSomething() }) }
+async function foo() { await (;() => { doSomething() }) } The code using |
|
It works but you also need to check that the In eslint/lib/rules/no-array-constructor.js Line 106 in 21ebf8a
Additionally, for |
Ah, right, I understand now. Thanks for the explanation. 😄 So this condition: const addSemiColon =
nextToken.type === "Punctuator" &&
(nextToken.value === "[" || nextToken.value === "(") &&
astUtils.isStartOfExpressionStatement(nodeWithAsyncKeyword) &&
astUtils.needsPrecedingSemicolon(sourceCode, nodeWithAsyncKeyword); works for all but this test: class A {
a = 0
async [b](){ return 0; }
} In that test, (nodeWithAsyncKeyword.type === "MethodDefinition" || astUtils.isStartOfExpressionStatement(nodeWithAsyncKeyword)) and that makes the test pass. Is |
Fixes #18713
Prerequisites checklist
What is the purpose of this pull request? (put an "X" next to an item)
[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[x] Changes an existing rule (template)
[ ] Add autofix to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
#18713
What changes did you make? (Give an overview)
The
require-await
rule now includes a suggestion to remove theasync
keyword.Is there anything you'd like reviewers to focus on?
No.