From 470174ad51fdb12d82129a896559075513f6c912 Mon Sep 17 00:00:00 2001 From: cherryblossom000 <31467609+cherryblossom000@users.noreply.github.com> Date: Fri, 7 Aug 2020 07:31:12 +1000 Subject: [PATCH] fix(eslint-plugin): [no-throw-literal] support type assertions (#2354) --- .../src/rules/no-throw-literal.ts | 3 ++- .../tests/rules/no-throw-literal.test.ts | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-throw-literal.ts b/packages/eslint-plugin/src/rules/no-throw-literal.ts index 67ba9e52ebe..b8017c4483a 100644 --- a/packages/eslint-plugin/src/rules/no-throw-literal.ts +++ b/packages/eslint-plugin/src/rules/no-throw-literal.ts @@ -66,7 +66,8 @@ export default util.createRule({ case AST_NODE_TYPES.Identifier: case AST_NODE_TYPES.CallExpression: case AST_NODE_TYPES.NewExpression: - case AST_NODE_TYPES.MemberExpression: { + case AST_NODE_TYPES.MemberExpression: + case AST_NODE_TYPES.TSAsExpression: { const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node); return checker.getTypeAtLocation(tsNode); } 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 09ecadeb629..883894bf2a1 100644 --- a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts +++ b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts @@ -101,14 +101,17 @@ function foo() { throw Object.assign(new Error('message'), { foo: 'bar' }); } `, - { - code: ` + ` const foo: Error | SyntaxError = bar(); function bar() { throw foo; } - `, - }, + `, + ` +declare const foo: Error | string; +throw foo as Error; + `, + 'throw new Error() as Error;', ], invalid: [ { @@ -390,5 +393,12 @@ function bar() { }, ], }, + { + code: ` +declare const foo: Error | string; +throw foo as string; + `, + errors: [{ messageId: 'object' }], + }, ], });