Skip to content

Commit

Permalink
fix(eslint-plugin): [no-throw-literal] support type assertions (#2354)
Browse files Browse the repository at this point in the history
  • Loading branch information
cherryblossom000 committed Aug 6, 2020
1 parent 522277d commit 470174a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/no-throw-literal.ts
Expand Up @@ -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);
}
Expand Down
18 changes: 14 additions & 4 deletions packages/eslint-plugin/tests/rules/no-throw-literal.test.ts
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -390,5 +393,12 @@ function bar() {
},
],
},
{
code: `
declare const foo: Error | string;
throw foo as string;
`,
errors: [{ messageId: 'object' }],
},
],
});

0 comments on commit 470174a

Please sign in to comment.