]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-throw-literal.js
import 8.3.0 source
[pve-eslint.git] / eslint / tests / lib / rules / no-throw-literal.js
1 /**
2 * @fileoverview Tests for no-throw-literal rule.
3 * @author Dieter Oberkofler
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/no-throw-literal"),
13 { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester();
20
21 ruleTester.run("no-throw-literal", rule, {
22 valid: [
23 "throw new Error();",
24 "throw new Error('error');",
25 "throw Error('error');",
26 "var e = new Error(); throw e;",
27 "try {throw new Error();} catch (e) {throw e;};",
28 "throw a;", // Identifier
29 "throw foo();", // CallExpression
30 "throw new foo();", // NewExpression
31 "throw foo.bar;", // MemberExpression
32 "throw foo[bar];", // MemberExpression
33 { code: "class C { #field; foo() { throw foo.#field; } }", parserOptions: { ecmaVersion: 2022 } }, // MemberExpression
34 "throw foo = new Error();", // AssignmentExpression with the `=` operator
35 { code: "throw foo.bar ||= 'literal'", parserOptions: { ecmaVersion: 2021 } }, // AssignmentExpression with a logical operator
36 { code: "throw foo[bar] ??= 'literal'", parserOptions: { ecmaVersion: 2021 } }, // AssignmentExpression with a logical operator
37 "throw 1, 2, new Error();", // SequenceExpression
38 "throw 'literal' && new Error();", // LogicalExpression (right)
39 "throw new Error() || 'literal';", // LogicalExpression (left)
40 "throw foo ? new Error() : 'literal';", // ConditionalExpression (consequent)
41 "throw foo ? 'literal' : new Error();", // ConditionalExpression (alternate)
42 { code: "throw tag `${foo}`;", parserOptions: { ecmaVersion: 6 } }, // TaggedTemplateExpression
43 { code: "function* foo() { var index = 0; throw yield index++; }", parserOptions: { ecmaVersion: 6 } }, // YieldExpression
44 { code: "async function foo() { throw await bar; }", parserOptions: { ecmaVersion: 8 } }, // AwaitExpression
45 { code: "throw obj?.foo", parserOptions: { ecmaVersion: 2020 } }, // ChainExpression
46 { code: "throw obj?.foo()", parserOptions: { ecmaVersion: 2020 } } // ChainExpression
47 ],
48 invalid: [
49 {
50 code: "throw 'error';",
51 errors: [{
52 messageId: "object",
53 type: "ThrowStatement"
54 }]
55 },
56 {
57 code: "throw 0;",
58 errors: [{
59 messageId: "object",
60 type: "ThrowStatement"
61 }]
62 },
63 {
64 code: "throw false;",
65 errors: [{
66 messageId: "object",
67 type: "ThrowStatement"
68 }]
69 },
70 {
71 code: "throw null;",
72 errors: [{
73 messageId: "object",
74 type: "ThrowStatement"
75 }]
76 },
77 {
78 code: "throw {};",
79 errors: [{
80 messageId: "object",
81 type: "ThrowStatement"
82 }]
83 },
84 {
85 code: "throw undefined;",
86 errors: [{
87 messageId: "undef",
88 type: "ThrowStatement"
89 }]
90 },
91
92 // String concatenation
93 {
94 code: "throw 'a' + 'b';",
95 errors: [{
96 messageId: "object",
97 type: "ThrowStatement"
98 }]
99 },
100 {
101 code: "var b = new Error(); throw 'a' + b;",
102 errors: [{
103 messageId: "object",
104 type: "ThrowStatement"
105 }]
106 },
107
108 // AssignmentExpression
109 {
110 code: "throw foo = 'error';", // RHS is a literal
111 errors: [{
112 messageId: "object",
113 type: "ThrowStatement"
114 }]
115 },
116 {
117 code: "throw foo += new Error();", // evaluates to a primitive value, or throws while evaluating
118 errors: [{
119 messageId: "object",
120 type: "ThrowStatement"
121 }]
122 },
123 {
124 code: "throw foo &= new Error();", // evaluates to a primitive value, or throws while evaluating
125 errors: [{
126 messageId: "object",
127 type: "ThrowStatement"
128 }]
129 },
130 {
131 code: "throw foo &&= 'literal'", // evaluates either to a falsy value of `foo` (which, then, cannot be an Error object), or to 'literal'
132 parserOptions: { ecmaVersion: 2021 },
133 errors: [{
134 messageId: "object",
135 type: "ThrowStatement"
136 }]
137 },
138
139 // SequenceExpression
140 {
141 code: "throw new Error(), 1, 2, 3;",
142 errors: [{
143 messageId: "object",
144 type: "ThrowStatement"
145 }]
146 },
147
148 // LogicalExpression
149 {
150 code: "throw 'literal' && 'not an Error';",
151 errors: [{
152 messageId: "object",
153 type: "ThrowStatement"
154 }]
155 },
156 {
157 code: "throw foo && 'literal'", // evaluates either to a falsy value of `foo` (which, then, cannot be an Error object), or to 'literal'
158 errors: [{
159 messageId: "object",
160 type: "ThrowStatement"
161 }]
162 },
163
164 // ConditionalExpression
165 {
166 code: "throw foo ? 'not an Error' : 'literal';",
167 errors: [{
168 messageId: "object",
169 type: "ThrowStatement"
170 }]
171 },
172
173 // TemplateLiteral
174 {
175 code: "throw `${err}`;",
176 parserOptions: { ecmaVersion: 6 },
177 errors: [{
178 messageId: "object",
179 type: "ThrowStatement"
180
181 }]
182 }
183 ]
184 });