Skip to content

Commit

Permalink
fix(eslint-plugin): [promise-function-async] handle keyword token (#5907
Browse files Browse the repository at this point in the history
)

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
yeonjuan and JoshuaKGoldberg committed Nov 14, 2022
1 parent 769e8c8 commit f25a94f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
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,
},
],
},
],
});

0 comments on commit f25a94f

Please sign in to comment.