From 9d53c761983dd964109b9f13eb9bfe20caf9defb Mon Sep 17 00:00:00 2001 From: Alexander T Date: Wed, 1 Apr 2020 18:41:44 +0300 Subject: [PATCH] fix(eslint-plugin): [no-throw-literal] fix crash caused by getBaseTypes (#1830) --- .../src/rules/no-throw-literal.ts | 9 +++--- .../tests/rules/no-throw-literal.test.ts | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-throw-literal.ts b/packages/eslint-plugin/src/rules/no-throw-literal.ts index c8c5bcc1837..ffa8a614e6b 100644 --- a/packages/eslint-plugin/src/rules/no-throw-literal.ts +++ b/packages/eslint-plugin/src/rules/no-throw-literal.ts @@ -43,10 +43,11 @@ export default util.createRule({ } } - const baseTypes = checker.getBaseTypes(type as ts.InterfaceType); - for (const baseType of baseTypes) { - if (isErrorLike(baseType)) { - return true; + if (symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Interface)) { + for (const baseType of checker.getBaseTypes(type as ts.InterfaceType)) { + if (isErrorLike(baseType)) { + return true; + } } } diff --git a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts index 1367c6a8f69..7d7b95acbb2 100644 --- a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts +++ b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts @@ -321,5 +321,36 @@ throw new CustomError(); }, ], }, + { + code: ` +function foo() { + const res: T; + throw res; +} + `, + errors: [ + { + messageId: 'object', + line: 4, + column: 9, + }, + ], + }, + { + code: ` +function foo(fn: () => Promise) { + const promise = fn(); + const res = promise.then(() => {}).catch(() => {}); + throw res; +} + `, + errors: [ + { + messageId: 'object', + line: 5, + column: 9, + }, + ], + }, ], });