]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/no-throw-literal.js
2 * @fileoverview Tests for no-throw-literal rule.
3 * @author Dieter Oberkofler
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const rule
= require("../../../lib/rules/no-throw-literal"),
13 { RuleTester
} = require("../../../lib/rule-tester");
15 //------------------------------------------------------------------------------
17 //------------------------------------------------------------------------------
19 const ruleTester
= new RuleTester();
21 ruleTester
.run("no-throw-literal", rule
, {
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
50 code
: "throw 'error';",
53 type
: "ThrowStatement"
60 type
: "ThrowStatement"
67 type
: "ThrowStatement"
74 type
: "ThrowStatement"
81 type
: "ThrowStatement"
85 code
: "throw undefined;",
88 type
: "ThrowStatement"
92 // String concatenation
94 code
: "throw 'a' + 'b';",
97 type
: "ThrowStatement"
101 code
: "var b = new Error(); throw 'a' + b;",
104 type
: "ThrowStatement"
108 // AssignmentExpression
110 code
: "throw foo = 'error';", // RHS is a literal
113 type
: "ThrowStatement"
117 code
: "throw foo += new Error();", // evaluates to a primitive value, or throws while evaluating
120 type
: "ThrowStatement"
124 code
: "throw foo &= new Error();", // evaluates to a primitive value, or throws while evaluating
127 type
: "ThrowStatement"
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 },
135 type
: "ThrowStatement"
139 // SequenceExpression
141 code
: "throw new Error(), 1, 2, 3;",
144 type
: "ThrowStatement"
150 code
: "throw 'literal' && 'not an Error';",
153 type
: "ThrowStatement"
157 code
: "throw foo && 'literal'", // evaluates either to a falsy value of `foo` (which, then, cannot be an Error object), or to 'literal'
160 type
: "ThrowStatement"
164 // ConditionalExpression
166 code
: "throw foo ? 'not an Error' : 'literal';",
169 type
: "ThrowStatement"
175 code
: "throw `${err}`;",
176 parserOptions
: { ecmaVersion
: 6 },
179 type
: "ThrowStatement"