forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
no-throw-literal.ts
130 lines (114 loc) · 3.05 KB
/
no-throw-literal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import * as ts from 'typescript';
import * as util from '../util';
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
type MessageIds = 'object' | 'undef';
type Options = [
{
allowThrowingAny?: boolean;
allowThrowingUnknown?: boolean;
},
];
export default util.createRule<Options, MessageIds>({
name: 'no-throw-literal',
meta: {
type: 'problem',
docs: {
description: 'Disallow throwing literals as exceptions',
recommended: false,
extendsBaseRule: true,
requiresTypeChecking: true,
},
schema: [
{
type: 'object',
properties: {
allowThrowingAny: {
type: 'boolean',
},
allowThrowingUnknown: {
type: 'boolean',
},
},
additionalProperties: false,
},
],
messages: {
object: 'Expected an error object to be thrown.',
undef: 'Do not throw undefined.',
},
},
defaultOptions: [
{
allowThrowingAny: true,
allowThrowingUnknown: true,
},
],
create(context, [options]) {
const parserServices = util.getParserServices(context);
const program = parserServices.program;
const checker = program.getTypeChecker();
function isErrorLike(type: ts.Type): boolean {
if (type.isIntersection()) {
return type.types.some(isErrorLike);
}
if (type.isUnion()) {
return type.types.every(isErrorLike);
}
const symbol = type.getSymbol();
if (!symbol) {
return false;
}
if (symbol.getName() === 'Error') {
const declarations = symbol.getDeclarations() ?? [];
for (const declaration of declarations) {
const sourceFile = declaration.getSourceFile();
if (program.isSourceFileDefaultLibrary(sourceFile)) {
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;
}
}
}
return false;
}
function checkThrowArgument(node: TSESTree.Node): void {
if (
node.type === AST_NODE_TYPES.AwaitExpression ||
node.type === AST_NODE_TYPES.YieldExpression
) {
return;
}
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode);
if (type.flags & ts.TypeFlags.Undefined) {
context.report({ node, messageId: 'undef' });
return;
}
if (options.allowThrowingAny && util.isTypeAnyType(type)) {
return;
}
if (options.allowThrowingUnknown && util.isTypeUnknownType(type)) {
return;
}
if (isErrorLike(type)) {
return;
}
context.report({ node, messageId: 'object' });
}
return {
ThrowStatement(node): void {
if (node.argument) {
checkThrowArgument(node.argument);
}
},
};
},
});