From f25a94fa75e497a6b9ec29a008bcc89818eed60d Mon Sep 17 00:00:00 2001 From: YeonJuan Date: Mon, 14 Nov 2022 09:12:04 +0900 Subject: [PATCH] fix(eslint-plugin): [promise-function-async] handle keyword token (#5907) Co-authored-by: Josh Goldberg --- .../src/rules/promise-function-async.ts | 5 +- .../rules/promise-function-async.test.ts | 82 +++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) diff --git a/packages/eslint-plugin/src/rules/promise-function-async.ts b/packages/eslint-plugin/src/rules/promise-function-async.ts index ba3c3d9478f..4387bc52c9b 100644 --- a/packages/eslint-plugin/src/rules/promise-function-async.ts +++ b/packages/eslint-plugin/src/rules/promise-function-async.ts @@ -175,7 +175,10 @@ export default util.createRule({ } // 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)!; } diff --git a/packages/eslint-plugin/tests/rules/promise-function-async.test.ts b/packages/eslint-plugin/tests/rules/promise-function-async.test.ts index 69b604c811c..ccf99b6afcb 100644 --- a/packages/eslint-plugin/tests/rules/promise-function-async.test.ts +++ b/packages/eslint-plugin/tests/rules/promise-function-async.test.ts @@ -102,6 +102,13 @@ const invalidAsyncModifiers = { constructor() {} } `, + ` +class Foo { + async catch(arg: Promise) { + return arg; + } +} + `, { code: ` function returnsAny(): any { @@ -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() { + return Promise.resolve(3); + } +} + `, + output: ` +class Foo { + async catch() { + return Promise.resolve(1); + } + + public async default() { + return Promise.resolve(2); + } + + @decorator + private async case() { + 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, + }, + ], + }, ], });