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(eslint-plugin): [promise-function-async] handle keyword token #5907

Merged
merged 2 commits into from Nov 14, 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
5 changes: 4 additions & 1 deletion packages/eslint-plugin/src/rules/promise-function-async.ts
Expand Up @@ -175,7 +175,10 @@ export default util.createRule<Options, MessageIds>({
}

// if current token is a keyword like `static` or `public` then skip it
while (keyToken.type === AST_TOKEN_TYPES.Keyword) {
while (
keyToken.type === AST_TOKEN_TYPES.Keyword &&
keyToken.range[0] < method.key.range[0]
) {
keyToken = sourceCode.getTokenAfter(keyToken)!;
}

Expand Down
82 changes: 82 additions & 0 deletions packages/eslint-plugin/tests/rules/promise-function-async.test.ts
Expand Up @@ -102,6 +102,13 @@ const invalidAsyncModifiers = {
constructor() {}
}
`,
`
class Foo {
async catch<T>(arg: Promise<T>) {
return arg;
}
}
`,
{
code: `
function returnsAny(): any {
Expand Down Expand Up @@ -670,5 +677,80 @@ class Test {
}
`,
},
// https://github.com/typescript-eslint/typescript-eslint/issues/5729
{
code: `
class Foo {
catch() {
return Promise.resolve(1);
}
public default() {
return Promise.resolve(2);
}
@decorator
private case<T>() {
return Promise.resolve(3);
}
}
`,
output: `
class Foo {
async catch() {
return Promise.resolve(1);
}
public async default() {
return Promise.resolve(2);
}
@decorator
private async case<T>() {
return Promise.resolve(3);
}
}
`,
errors: [
{
line: 3,
column: 3,
messageId,
},
{
line: 7,
column: 3,
messageId,
},
{
line: 12,
column: 3,
messageId,
},
],
},
{
code: `
const foo = {
catch() {
return Promise.resolve(1);
},
};
`,
output: `
const foo = {
async catch() {
return Promise.resolve(1);
},
};
`,
errors: [
{
line: 3,
column: 3,
messageId,
},
],
},
],
});