forked from sindresorhus/eslint-plugin-unicorn
/
throw-new-error.js
129 lines (124 loc) · 2.49 KB
/
throw-new-error.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
import test from 'ava';
import avaRuleTester from 'eslint-ava-rule-tester';
import rule from '../rules/throw-new-error';
const messageId = 'throw-new-error';
const ruleTester = avaRuleTester(test, {
parserOptions: {
ecmaVersion: 2020
}
});
const errors = [{messageId}];
ruleTester.run('new-error', rule, {
valid: [
'throw new Error()',
'new Error()',
'throw new TypeError()',
'throw new EvalError()',
'throw new RangeError()',
'throw new ReferenceError()',
'throw new SyntaxError()',
'throw new URIError()',
'throw new CustomError()',
'throw new FooBarBazError()',
'throw new ABCError()',
// Not `FooError` like
'throw getError()',
// Not `CallExpression`
'throw CustomError',
// Not `Identifier` / `MemberExpression`
'throw getErrorConstructor()()',
// `MemberExpression.computed`
'throw lib[Error]()',
// `MemberExpression.property` not `Identifier`
'throw lib["Error"]()',
// Not `FooError` like
'throw lib.getError()'
],
invalid: [
{
code: 'throw Error()',
output: 'throw new Error()',
errors
},
{
code: 'throw (Error)()',
output: 'throw new (Error)()',
errors
},
{
code: 'throw lib.Error()',
output: 'throw new lib.Error()',
errors
},
{
code: 'throw lib.mod.Error()',
output: 'throw new lib.mod.Error()',
errors
},
{
code: 'throw lib[mod].Error()',
output: 'throw new lib[mod].Error()',
errors
},
{
code: 'throw (lib.mod).Error()',
output: 'throw new (lib.mod).Error()',
errors
},
{
code: 'throw Error(\'foo\')',
output: 'throw new Error(\'foo\')',
errors
},
{
code: 'throw CustomError(\'foo\')',
output: 'throw new CustomError(\'foo\')',
errors
},
{
code: 'throw FooBarBazError(\'foo\')',
output: 'throw new FooBarBazError(\'foo\')',
errors
},
{
code: 'throw ABCError(\'foo\')',
output: 'throw new ABCError(\'foo\')',
errors
},
{
code: 'throw Abc3Error(\'foo\')',
output: 'throw new Abc3Error(\'foo\')',
errors
},
{
code: 'throw TypeError()',
output: 'throw new TypeError()',
errors
},
{
code: 'throw EvalError()',
output: 'throw new EvalError()',
errors
},
{
code: 'throw RangeError()',
output: 'throw new RangeError()',
errors
},
{
code: 'throw ReferenceError()',
output: 'throw new ReferenceError()',
errors
},
{
code: 'throw SyntaxError()',
output: 'throw new SyntaxError()',
errors
},
{
code: 'throw URIError()',
output: 'throw new URIError()',
errors
}
]
});