Skip to content

Commit f25a94f

Browse files
fix(eslint-plugin): [promise-function-async] handle keyword token (#5907)
Co-authored-by: Josh Goldberg <[email protected]>
1 parent 769e8c8 commit f25a94f

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

packages/eslint-plugin/src/rules/promise-function-async.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,10 @@ export default util.createRule<Options, MessageIds>({
175175
}
176176

177177
// if current token is a keyword like `static` or `public` then skip it
178-
while (keyToken.type === AST_TOKEN_TYPES.Keyword) {
178+
while (
179+
keyToken.type === AST_TOKEN_TYPES.Keyword &&
180+
keyToken.range[0] < method.key.range[0]
181+
) {
179182
keyToken = sourceCode.getTokenAfter(keyToken)!;
180183
}
181184

packages/eslint-plugin/tests/rules/promise-function-async.test.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ const invalidAsyncModifiers = {
102102
constructor() {}
103103
}
104104
`,
105+
`
106+
class Foo {
107+
async catch<T>(arg: Promise<T>) {
108+
return arg;
109+
}
110+
}
111+
`,
105112
{
106113
code: `
107114
function returnsAny(): any {
@@ -670,5 +677,80 @@ class Test {
670677
}
671678
`,
672679
},
680+
// https://github.com/typescript-eslint/typescript-eslint/issues/5729
681+
{
682+
code: `
683+
class Foo {
684+
catch() {
685+
return Promise.resolve(1);
686+
}
687+
688+
public default() {
689+
return Promise.resolve(2);
690+
}
691+
692+
@decorator
693+
private case<T>() {
694+
return Promise.resolve(3);
695+
}
696+
}
697+
`,
698+
output: `
699+
class Foo {
700+
async catch() {
701+
return Promise.resolve(1);
702+
}
703+
704+
public async default() {
705+
return Promise.resolve(2);
706+
}
707+
708+
@decorator
709+
private async case<T>() {
710+
return Promise.resolve(3);
711+
}
712+
}
713+
`,
714+
errors: [
715+
{
716+
line: 3,
717+
column: 3,
718+
messageId,
719+
},
720+
{
721+
line: 7,
722+
column: 3,
723+
messageId,
724+
},
725+
{
726+
line: 12,
727+
column: 3,
728+
messageId,
729+
},
730+
],
731+
},
732+
{
733+
code: `
734+
const foo = {
735+
catch() {
736+
return Promise.resolve(1);
737+
},
738+
};
739+
`,
740+
output: `
741+
const foo = {
742+
async catch() {
743+
return Promise.resolve(1);
744+
},
745+
};
746+
`,
747+
errors: [
748+
{
749+
line: 3,
750+
column: 3,
751+
messageId,
752+
},
753+
],
754+
},
673755
],
674756
});

0 commit comments

Comments
 (0)