]> git.proxmox.com Git - pve-eslint.git/blob - eslint/tests/lib/rules/prefer-regex-literals.js
first commit
[pve-eslint.git] / eslint / tests / lib / rules / prefer-regex-literals.js
1 /**
2 * @fileoverview Tests for the prefer-regex-literals rule
3 * @author Milos Djermanovic
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const rule = require("../../../lib/rules/prefer-regex-literals");
13 const { RuleTester } = require("../../../lib/rule-tester");
14
15 //------------------------------------------------------------------------------
16 // Tests
17 //------------------------------------------------------------------------------
18
19 const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2015 } });
20
21 ruleTester.run("prefer-regex-literals", rule, {
22 valid: [
23 "/abc/",
24 "/abc/g",
25
26 // considered as dynamic
27 "new RegExp(pattern)",
28 "RegExp(pattern, 'g')",
29 "new RegExp(f('a'))",
30 "RegExp(prefix + 'a')",
31 "new RegExp('a' + suffix)",
32 "RegExp(`a` + suffix);",
33 "new RegExp(String.raw`a` + suffix);",
34 "RegExp('a', flags)",
35 "RegExp('a', 'g' + flags)",
36 "new RegExp(String.raw`a`, flags);",
37 "RegExp(`${prefix}abc`)",
38 "new RegExp(`a${b}c`);",
39 "new RegExp(`a${''}c`);",
40 "new RegExp(String.raw`a${b}c`);",
41 "new RegExp(String.raw`a${''}c`);",
42 "new RegExp('a' + 'b')",
43 "RegExp(1)",
44
45 // invalid number of arguments
46 "new RegExp;",
47 "new RegExp();",
48 "RegExp();",
49 "new RegExp('a', 'g', 'b');",
50 "RegExp('a', 'g', 'b');",
51 "new RegExp(`a`, `g`, `b`);",
52 "RegExp(`a`, `g`, `b`);",
53 "new RegExp(String.raw`a`, String.raw`g`, String.raw`b`);",
54 "RegExp(String.raw`a`, String.raw`g`, String.raw`b`);",
55
56 // not String.raw``
57 "new RegExp(String`a`);",
58 "RegExp(raw`a`);",
59 "new RegExp(f(String.raw)`a`);",
60 "RegExp(string.raw`a`);",
61 "new RegExp(String.Raw`a`);",
62 "new RegExp(String[raw]`a`);",
63 "RegExp(String.raw.foo`a`);",
64 "new RegExp(String.foo.raw`a`);",
65 "RegExp(foo.String.raw`a`);",
66 "new RegExp(String.raw);",
67
68 // not the global String in String.raw``
69 "let String; new RegExp(String.raw`a`);",
70 "function foo() { var String; new RegExp(String.raw`a`); }",
71 "function foo(String) { RegExp(String.raw`a`); }",
72 "if (foo) { const String = bar; RegExp(String.raw`a`); }",
73 "/* globals String:off */ new RegExp(String.raw`a`);",
74 {
75 code: "RegExp('a', String.raw`g`);",
76 globals: { String: "off" }
77 },
78
79 // not RegExp
80 "new Regexp('abc');",
81 "Regexp(`a`);",
82 "new Regexp(String.raw`a`);",
83
84 // not the global RegExp
85 "let RegExp; new RegExp('a');",
86 "function foo() { var RegExp; RegExp('a', 'g'); }",
87 "function foo(RegExp) { new RegExp(String.raw`a`); }",
88 "if (foo) { const RegExp = bar; RegExp('a'); }",
89 "/* globals RegExp:off */ new RegExp('a');",
90 {
91 code: "RegExp('a');",
92 globals: { RegExp: "off" }
93 },
94 "new globalThis.RegExp('a');",
95 {
96 code: "new globalThis.RegExp('a');",
97 env: { es6: true }
98 },
99 {
100 code: "new globalThis.RegExp('a');",
101 env: { es2017: true }
102 }
103 ],
104
105 invalid: [
106 {
107 code: "new RegExp('abc');",
108 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
109 },
110 {
111 code: "RegExp('abc');",
112 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
113 },
114 {
115 code: "new RegExp('abc', 'g');",
116 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
117 },
118 {
119 code: "RegExp('abc', 'g');",
120 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
121 },
122 {
123 code: "new RegExp(`abc`);",
124 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
125 },
126 {
127 code: "RegExp(`abc`);",
128 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
129 },
130 {
131 code: "new RegExp(`abc`, `g`);",
132 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
133 },
134 {
135 code: "RegExp(`abc`, `g`);",
136 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
137 },
138 {
139 code: "new RegExp(String.raw`abc`);",
140 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
141 },
142 {
143 code: "RegExp(String.raw`abc`);",
144 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
145 },
146 {
147 code: "new RegExp(String.raw`abc`, String.raw`g`);",
148 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
149 },
150 {
151 code: "RegExp(String.raw`abc`, String.raw`g`);",
152 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
153 },
154 {
155 code: "new RegExp(String['raw']`a`);",
156 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
157 },
158 {
159 code: "new RegExp('');",
160 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
161 },
162 {
163 code: "RegExp('', '');",
164 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
165 },
166 {
167 code: "new RegExp(String.raw``);",
168 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
169 },
170 {
171 code: "new RegExp('a', `g`);",
172 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
173 },
174 {
175 code: "RegExp(`a`, 'g');",
176 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
177 },
178 {
179 code: "RegExp(String.raw`a`, 'g');",
180 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
181 },
182 {
183 code: "new RegExp(String.raw`\\d`, `g`);",
184 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
185 },
186 {
187 code: "RegExp('a', String.raw`g`);",
188 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
189 },
190 {
191 code: "new globalThis.RegExp('a');",
192 env: { es2020: true },
193 errors: [{ messageId: "unexpectedRegExp", type: "NewExpression" }]
194 },
195 {
196 code: "globalThis.RegExp('a');",
197 env: { es2020: true },
198 errors: [{ messageId: "unexpectedRegExp", type: "CallExpression" }]
199 }
200 ]
201 });