/
no-throw-literal.js
183 lines (170 loc) · 5.89 KB
/
no-throw-literal.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @fileoverview Tests for no-throw-literal rule.
* @author Dieter Oberkofler
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/no-throw-literal"),
{ RuleTester } = require("../../../lib/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("no-throw-literal", rule, {
valid: [
"throw new Error();",
"throw new Error('error');",
"throw Error('error');",
"var e = new Error(); throw e;",
"try {throw new Error();} catch (e) {throw e;};",
"throw a;", // Identifier
"throw foo();", // CallExpression
"throw new foo();", // NewExpression
"throw foo.bar;", // MemberExpression
"throw foo[bar];", // MemberExpression
"throw foo = new Error();", // AssignmentExpression with the `=` operator
{ code: "throw foo.bar ||= 'literal'", parserOptions: { ecmaVersion: 2021 } }, // AssignmentExpression with a logical operator
{ code: "throw foo[bar] ??= 'literal'", parserOptions: { ecmaVersion: 2021 } }, // AssignmentExpression with a logical operator
"throw 1, 2, new Error();", // SequenceExpression
"throw 'literal' && new Error();", // LogicalExpression (right)
"throw new Error() || 'literal';", // LogicalExpression (left)
"throw foo ? new Error() : 'literal';", // ConditionalExpression (consequent)
"throw foo ? 'literal' : new Error();", // ConditionalExpression (alternate)
{ code: "throw tag `${foo}`;", parserOptions: { ecmaVersion: 6 } }, // TaggedTemplateExpression
{ code: "function* foo() { var index = 0; throw yield index++; }", parserOptions: { ecmaVersion: 6 } }, // YieldExpression
{ code: "async function foo() { throw await bar; }", parserOptions: { ecmaVersion: 8 } }, // AwaitExpression
{ code: "throw obj?.foo", parserOptions: { ecmaVersion: 2020 } }, // ChainExpression
{ code: "throw obj?.foo()", parserOptions: { ecmaVersion: 2020 } } // ChainExpression
],
invalid: [
{
code: "throw 'error';",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw 0;",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw false;",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw null;",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw {};",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw undefined;",
errors: [{
messageId: "undef",
type: "ThrowStatement"
}]
},
// String concatenation
{
code: "throw 'a' + 'b';",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "var b = new Error(); throw 'a' + b;",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
// AssignmentExpression
{
code: "throw foo = 'error';", // RHS is a literal
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw foo += new Error();", // evaluates to a primitive value, or throws while evaluating
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw foo &= new Error();", // evaluates to a primitive value, or throws while evaluating
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw foo &&= 'literal'", // evaluates either to a falsy value of `foo` (which, then, cannot be an Error object), or to 'literal'
parserOptions: { ecmaVersion: 2021 },
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
// SequenceExpression
{
code: "throw new Error(), 1, 2, 3;",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
// LogicalExpression
{
code: "throw 'literal' && 'not an Error';",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
{
code: "throw foo && 'literal'", // evaluates either to a falsy value of `foo` (which, then, cannot be an Error object), or to 'literal'
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
// ConditionalExpression
{
code: "throw foo ? 'not an Error' : 'literal';",
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
},
// TemplateLiteral
{
code: "throw `${err}`;",
parserOptions: { ecmaVersion: 6 },
errors: [{
messageId: "object",
type: "ThrowStatement"
}]
}
]
});