From fa4c831a6dc0df547bae45b9c7754e184f9be427 Mon Sep 17 00:00:00 2001 From: Naru Date: Sun, 24 Mar 2024 18:08:33 +0900 Subject: [PATCH 1/2] fix(eslint-plugin): [no-floating-promises] handle TaggedTemplateExpression --- .../src/rules/no-floating-promises.ts | 2 ++ .../tests/rules/no-floating-promises.test.ts | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/packages/eslint-plugin/src/rules/no-floating-promises.ts b/packages/eslint-plugin/src/rules/no-floating-promises.ts index b3ac65296992..b9fb9b1cc4dd 100644 --- a/packages/eslint-plugin/src/rules/no-floating-promises.ts +++ b/packages/eslint-plugin/src/rules/no-floating-promises.ts @@ -290,6 +290,8 @@ export default createRule({ // All other cases are unhandled. return { isUnhandled: true }; + } else if (node.type === AST_NODE_TYPES.TaggedTemplateExpression) { + return { isUnhandled: true }; } else if (node.type === AST_NODE_TYPES.ConditionalExpression) { // We must be getting the promise-like value from one of the branches of the // ternary. Check them directly. diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index 434c9735a46d..e83965c8205f 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -593,6 +593,30 @@ doSomething(); }, ], }, + { + code: ` +declare const myTag: (strings: TemplateStringsArray) => Promise; + +myTag\`abc\`; +myTag\`abc\`.then(() => {}); +myTag\`abc\`.catch(() => {}); +myTag\`abc\`.finally(() => {}); + `, + errors: [ + { + line: 4, + messageId: 'floatingVoid', + }, + { + line: 5, + messageId: 'floatingVoid', + }, + { + line: 7, + messageId: 'floatingVoid', + }, + ], + }, { options: [{ ignoreVoid: true }], code: ` From 7c8c9dda34b16e98ecfb0adf2561fc4defed6a48 Mon Sep 17 00:00:00 2001 From: Naru Date: Tue, 26 Mar 2024 02:20:34 +0900 Subject: [PATCH 2/2] Improve test cases --- .../tests/rules/no-floating-promises.test.ts | 39 +++++++++++++++---- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts index e83965c8205f..bd02ed6d5a87 100644 --- a/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts +++ b/packages/eslint-plugin/tests/rules/no-floating-promises.test.ts @@ -504,6 +504,18 @@ void promiseArray; ['I', 'am', 'just', 'an', 'array']; `, }, + { + code: ` +declare const myTag: (strings: TemplateStringsArray) => Promise; +myTag\`abc\`.catch(() => {}); + `, + }, + { + code: ` +declare const myTag: (strings: TemplateStringsArray) => string; +myTag\`abc\`; + `, + }, ], invalid: [ @@ -596,27 +608,40 @@ doSomething(); { code: ` declare const myTag: (strings: TemplateStringsArray) => Promise; - myTag\`abc\`; -myTag\`abc\`.then(() => {}); -myTag\`abc\`.catch(() => {}); -myTag\`abc\`.finally(() => {}); `, errors: [ { - line: 4, + line: 3, messageId: 'floatingVoid', }, + ], + }, + { + code: ` +declare const myTag: (strings: TemplateStringsArray) => Promise; +myTag\`abc\`.then(() => {}); + `, + errors: [ { - line: 5, + line: 3, messageId: 'floatingVoid', }, + ], + }, + { + code: ` +declare const myTag: (strings: TemplateStringsArray) => Promise; +myTag\`abc\`.finally(() => {}); + `, + errors: [ { - line: 7, + line: 3, messageId: 'floatingVoid', }, ], }, + { options: [{ ignoreVoid: true }], code: `