UNPKG

4.54 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const ts = __importStar(require("typescript"));
23const util = __importStar(require("../util"));
24const utils_1 = require("@typescript-eslint/utils");
25exports.default = util.createRule({
26 name: 'no-throw-literal',
27 meta: {
28 type: 'problem',
29 docs: {
30 description: 'Disallow throwing literals as exceptions',
31 recommended: false,
32 extendsBaseRule: true,
33 requiresTypeChecking: true,
34 },
35 schema: [
36 {
37 type: 'object',
38 properties: {
39 allowThrowingAny: {
40 type: 'boolean',
41 },
42 allowThrowingUnknown: {
43 type: 'boolean',
44 },
45 },
46 additionalProperties: false,
47 },
48 ],
49 messages: {
50 object: 'Expected an error object to be thrown.',
51 undef: 'Do not throw undefined.',
52 },
53 },
54 defaultOptions: [
55 {
56 allowThrowingAny: true,
57 allowThrowingUnknown: true,
58 },
59 ],
60 create(context, [options]) {
61 const parserServices = util.getParserServices(context);
62 const program = parserServices.program;
63 const checker = program.getTypeChecker();
64 function isErrorLike(type) {
65 var _a;
66 if (type.isIntersection()) {
67 return type.types.some(isErrorLike);
68 }
69 if (type.isUnion()) {
70 return type.types.every(isErrorLike);
71 }
72 const symbol = type.getSymbol();
73 if (!symbol) {
74 return false;
75 }
76 if (symbol.getName() === 'Error') {
77 const declarations = (_a = symbol.getDeclarations()) !== null && _a !== void 0 ? _a : [];
78 for (const declaration of declarations) {
79 const sourceFile = declaration.getSourceFile();
80 if (program.isSourceFileDefaultLibrary(sourceFile)) {
81 return true;
82 }
83 }
84 }
85 if (symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Interface)) {
86 for (const baseType of checker.getBaseTypes(type)) {
87 if (isErrorLike(baseType)) {
88 return true;
89 }
90 }
91 }
92 return false;
93 }
94 function checkThrowArgument(node) {
95 if (node.type === utils_1.AST_NODE_TYPES.AwaitExpression ||
96 node.type === utils_1.AST_NODE_TYPES.YieldExpression) {
97 return;
98 }
99 const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
100 const type = checker.getTypeAtLocation(tsNode);
101 if (type.flags & ts.TypeFlags.Undefined) {
102 context.report({ node, messageId: 'undef' });
103 return;
104 }
105 if (options.allowThrowingAny && util.isTypeAnyType(type)) {
106 return;
107 }
108 if (options.allowThrowingUnknown && util.isTypeUnknownType(type)) {
109 return;
110 }
111 if (isErrorLike(type)) {
112 return;
113 }
114 context.report({ node, messageId: 'object' });
115 }
116 return {
117 ThrowStatement(node) {
118 if (node.argument) {
119 checkThrowArgument(node.argument);
120 }
121 },
122 };
123 },
124});
125//# sourceMappingURL=no-throw-literal.js.map
\No newline at end of file