@@ -29,38 +29,38 @@ const testCase = (original, optimized) => ({
2929test ( {
3030 valid : [
3131 // Literal regex
32- String . raw ` const foo = /\d/` ,
33- String . raw ` const foo = /\W/i` ,
34- String . raw ` const foo = /\w/gi` ,
32+ ' const foo = /\\d/' ,
33+ ' const foo = /\\ W/i' ,
34+ ' const foo = /\\ w/gi' ,
3535 'const foo = /[a-z]/gi' ,
36- String . raw ` const foo = /\d*?/gi` ,
36+ ' const foo = /\\ d*?/gi' ,
3737
3838 // Should not crash ESLint (#446 and #448)
39- String . raw `/\{\ {verificationUrl\}\ }/gu` ,
40- String . raw ` /^test-(?<name>[a-zA-Z-\d]+)$/u` ,
39+ '/\\{\\ {verificationUrl\\}\\ }/gu' ,
40+ ' /^test-(?<name>[a-zA-Z-\\ d]+)$/u' ,
4141 String . raw `/[\p{Script_Extensions=Greek}--π]/v` ,
4242
4343 // Should not suggest wrong regex (#447)
44- String . raw ` /(\s|\.|@|_|-)/u` ,
45- String . raw ` /[\s.@_-]/u` ,
44+ ' /(\\ s|\\ .|@|_|-)/u' ,
45+ ' /[\\ s.@_-]/u' ,
4646
4747 // Should not remove repeating patterns too easily (#769)
48- String . raw ` /http:\/\ /[^/]+\/pull\/commits/gi` ,
48+ ' /http:\\/\\ /[^/]+\\ /pull\\ /commits/gi' ,
4949
5050 {
5151 code : '/[GgHhIiå.Z:a-f"0-8%A*ä]/' ,
5252 options : disableSortCharacterClassesOptions ,
5353 } ,
5454
5555 // `RegExp()` constructor
56- String . raw ` new RegExp('\d')` ,
57- String . raw ` new RegExp('\d ', 'ig')` ,
58- String . raw ` new RegExp('\ d*?')` ,
56+ ' new RegExp(\'\\d\')' ,
57+ ' new RegExp(\'\\d\ ', \ 'ig\')' ,
58+ ' new RegExp(\'\\ d*?\')' ,
5959 'new RegExp(\'[a-z]\', \'i\')' ,
60- String . raw ` new RegExp(/\d/)` ,
61- String . raw ` new RegExp(/\d/gi)` ,
62- String . raw ` new RegExp(/\d/, 'ig')` ,
63- String . raw ` new RegExp(/\d*?/)` ,
60+ ' new RegExp(/\\ d/)' ,
61+ ' new RegExp(/\\ d/gi)' ,
62+ ' new RegExp(/\\ d/, \ 'ig\')' ,
63+ ' new RegExp(/\\ d*?/)' ,
6464 'new RegExp(/[a-z]/, \'i\')' ,
6565 // Not `new`
6666 'RegExp("[0-9]")' ,
@@ -79,105 +79,105 @@ test({
7979 '/[ ;-]/g' ,
8080
8181 // #994
82- String . raw `/\ s?\s?/` , // https://github.com/DmitrySoshnikov/regexp-tree/issues/216#issuecomment-762073297
83- String . raw `/\ s{0,2}/` ,
82+ '/\\ s?\\ s?/' , // https://github.com/DmitrySoshnikov/regexp-tree/issues/216#issuecomment-762073297
83+ '/\\ s{0,2}/' ,
8484 ] ,
8585 invalid : [
8686 // Literal regex
8787 {
88- code : String . raw ` const foo = /\w/ig` ,
89- errors : createError ( String . raw `/\ w/ig` , String . raw `/\ w/gi` ) ,
90- output : String . raw ` const foo = /\w/gi` ,
88+ code : ' const foo = /\\ w/ig' ,
89+ errors : createError ( '/\\ w/ig' , '/\\ w/gi' ) ,
90+ output : ' const foo = /\\ w/gi' ,
9191 } ,
9292 {
9393 code : 'const foo = /[0-9]/' ,
94- errors : createError ( '/[0-9]/' , String . raw `/\d/` ) ,
95- output : String . raw ` const foo = /\d/` ,
94+ errors : createError ( '/[0-9]/' , '/\\d/' ) ,
95+ output : ' const foo = /\\d/' ,
9696 } ,
9797 {
9898 code : 'const foo = /[0-9]/ig' ,
99- errors : createError ( '/[0-9]/ig' , String . raw `/\ d/gi` ) ,
100- output : String . raw ` const foo = /\d/gi` ,
99+ errors : createError ( '/[0-9]/ig' , '/\\ d/gi' ) ,
100+ output : ' const foo = /\\ d/gi' ,
101101 } ,
102102 {
103103 code : 'const foo = /[^0-9]/' ,
104- errors : createError ( '/[^0-9]/' , String . raw `/\D/` ) ,
105- output : String . raw ` const foo = /\D/` ,
104+ errors : createError ( '/[^0-9]/' , '/\\D/' ) ,
105+ output : ' const foo = /\\D/' ,
106106 } ,
107107 {
108108 code : 'const foo = /[A-Za-z0-9_]/' ,
109- errors : createError ( '/[A-Za-z0-9_]/' , String . raw `/\w/` ) ,
110- output : String . raw ` const foo = /\w/` ,
109+ errors : createError ( '/[A-Za-z0-9_]/' , '/\\w/' ) ,
110+ output : ' const foo = /\\w/' ,
111111 } ,
112112 {
113- code : String . raw ` const foo = /[A-Za-z\d_]/` ,
114- errors : createError ( String . raw ` /[A-Za-z\d_]/` , String . raw `/\w/` ) ,
115- output : String . raw ` const foo = /\w/` ,
113+ code : ' const foo = /[A-Za-z\\ d_]/' ,
114+ errors : createError ( ' /[A-Za-z\\ d_]/' , '/\\w/' ) ,
115+ output : ' const foo = /\\w/' ,
116116 } ,
117117 {
118118 code : 'const foo = /[a-zA-Z0-9_]/' ,
119- errors : createError ( '/[a-zA-Z0-9_]/' , String . raw `/\w/` ) ,
120- output : String . raw ` const foo = /\w/` ,
119+ errors : createError ( '/[a-zA-Z0-9_]/' , '/\\w/' ) ,
120+ output : ' const foo = /\\w/' ,
121121 } ,
122122 {
123- code : String . raw ` const foo = /[a-zA-Z\d_]/` ,
124- errors : createError ( String . raw ` /[a-zA-Z\d_]/` , String . raw `/\w/` ) ,
125- output : String . raw ` const foo = /\w/` ,
123+ code : ' const foo = /[a-zA-Z\\ d_]/' ,
124+ errors : createError ( ' /[a-zA-Z\\ d_]/' , '/\\w/' ) ,
125+ output : ' const foo = /\\w/' ,
126126 } ,
127127 {
128- code : String . raw ` const foo = /[A-Za-z0-9_]+[0-9]?\.[A-Za-z0-9_]*/` ,
129- errors : createError ( String . raw ` /[A-Za-z0-9_]+[0-9]?\.[A-Za-z0-9_]*/` , String . raw `/\ w+\d?\.\ w*/` ) ,
130- output : String . raw ` const foo = /\w+\d?\.\ w*/` ,
128+ code : ' const foo = /[A-Za-z0-9_]+[0-9]?\\ .[A-Za-z0-9_]*/' ,
129+ errors : createError ( ' /[A-Za-z0-9_]+[0-9]?\\ .[A-Za-z0-9_]*/' , '/\\ w+\\ d?\\.\\ w*/' ) ,
130+ output : ' const foo = /\\ w+\\ d?\\.\\ w*/' ,
131131 } ,
132132 {
133133 code : 'const foo = /[a-z0-9_]/i' ,
134- errors : createError ( '/[a-z0-9_]/i' , String . raw `/\ w/i` ) ,
135- output : String . raw ` const foo = /\w/i` ,
134+ errors : createError ( '/[a-z0-9_]/i' , '/\\ w/i' ) ,
135+ output : ' const foo = /\\ w/i' ,
136136 } ,
137137 {
138- code : String . raw ` const foo = /[a-z\d_]/i` ,
139- errors : createError ( String . raw ` /[a-z\d_]/i` , String . raw `/\ w/i` ) ,
140- output : String . raw ` const foo = /\w/i` ,
138+ code : ' const foo = /[a-z\\ d_]/i' ,
139+ errors : createError ( ' /[a-z\\ d_]/i' , '/\\ w/i' ) ,
140+ output : ' const foo = /\\ w/i' ,
141141 } ,
142142 {
143143 code : 'const foo = /[^A-Za-z0-9_]/' ,
144- errors : createError ( '/[^A-Za-z0-9_]/' , String . raw `/\W/` ) ,
145- output : String . raw ` const foo = /\W/` ,
144+ errors : createError ( '/[^A-Za-z0-9_]/' , '/\\W/' ) ,
145+ output : ' const foo = /\\W/' ,
146146 } ,
147147 {
148- code : String . raw ` const foo = /[^A-Za-z\d_]/` ,
149- errors : createError ( String . raw ` /[^A-Za-z\d_]/` , String . raw `/\W/` ) ,
150- output : String . raw ` const foo = /\W/` ,
148+ code : ' const foo = /[^A-Za-z\\ d_]/' ,
149+ errors : createError ( ' /[^A-Za-z\\ d_]/' , '/\\W/' ) ,
150+ output : ' const foo = /\\W/' ,
151151 } ,
152152 {
153153 code : 'const foo = /[^a-z0-9_]/i' ,
154- errors : createError ( '/[^a-z0-9_]/i' , String . raw `/\ W/i` ) ,
155- output : String . raw ` const foo = /\W/i` ,
154+ errors : createError ( '/[^a-z0-9_]/i' , '/\\ W/i' ) ,
155+ output : ' const foo = /\\ W/i' ,
156156 } ,
157157 {
158- code : String . raw ` const foo = /[^a-z\d_]/i` ,
159- errors : createError ( String . raw ` /[^a-z\d_]/i` , String . raw `/\ W/i` ) ,
160- output : String . raw ` const foo = /\W/i` ,
158+ code : ' const foo = /[^a-z\\ d_]/i' ,
159+ errors : createError ( ' /[^a-z\\ d_]/i' , '/\\ W/i' ) ,
160+ output : ' const foo = /\\ W/i' ,
161161 } ,
162162 {
163- code : String . raw ` const foo = /[^a-z\d_]/ig` ,
164- errors : createError ( String . raw ` /[^a-z\d_]/ig` , String . raw `/\ W/gi` ) ,
165- output : String . raw ` const foo = /\W/gi` ,
163+ code : ' const foo = /[^a-z\\ d_]/ig' ,
164+ errors : createError ( ' /[^a-z\\ d_]/ig' , '/\\ W/gi' ) ,
165+ output : ' const foo = /\\ W/gi' ,
166166 } ,
167167 {
168- code : String . raw ` const foo = /[^\d_a-z]/ig` ,
169- errors : createError ( String . raw ` /[^\d_a-z]/ig` , String . raw `/\ W/gi` ) ,
170- output : String . raw ` const foo = /\W/gi` ,
168+ code : ' const foo = /[^\\ d_a-z]/ig' ,
169+ errors : createError ( ' /[^\\ d_a-z]/ig' , '/\\ W/gi' ) ,
170+ output : ' const foo = /\\ W/gi' ,
171171 } ,
172172 {
173173 code : 'const foo = /[a-z0-9_]/' ,
174- errors : createError ( '/[a-z0-9_]/' , String . raw ` /[\d_a-z]/` ) ,
175- output : String . raw ` const foo = /[\d_a-z]/` ,
174+ errors : createError ( '/[a-z0-9_]/' , ' /[\\ d_a-z]/' ) ,
175+ output : ' const foo = /[\\ d_a-z]/' ,
176176 } ,
177177 {
178178 code : 'const foo = /^by @([a-zA-Z0-9-]+)/' ,
179- errors : createError ( '/^by @([a-zA-Z0-9-]+)/' , String . raw ` /^by @([\dA-Za-z-]+)/` ) ,
180- output : String . raw ` const foo = /^by @([\dA-Za-z-]+)/` ,
179+ errors : createError ( '/^by @([a-zA-Z0-9-]+)/' , ' /^by @([\\ dA-Za-z-]+)/' ) ,
180+ output : ' const foo = /^by @([\\ dA-Za-z-]+)/' ,
181181 } ,
182182 {
183183 code : '/[GgHhIiå.Z:a-f"0-8%A*ä]/' ,
@@ -188,100 +188,100 @@ test({
188188 {
189189 code : '/[a0-9b]/' ,
190190 options : disableSortCharacterClassesOptions ,
191- errors : createError ( '/[a0-9b]/' , String . raw ` /[a\db]/` ) ,
192- output : String . raw ` /[a\db]/` ,
191+ errors : createError ( '/[a0-9b]/' , ' /[a\\ db]/' ) ,
192+ output : ' /[a\\ db]/' ,
193193 } ,
194194
195195 // `RegExp()` constructor
196196 {
197197 code : 'const foo = new RegExp(\'[0-9]\')' ,
198- errors : createError ( '[0-9]' , String . raw `\d` ) ,
199- output : String . raw ` const foo = new RegExp('\\d')` ,
198+ errors : createError ( '[0-9]' , '\\d' ) ,
199+ output : ' const foo = new RegExp(\ '\\\\d\')' ,
200200 } ,
201201 {
202202 code : 'const foo = new RegExp("[0-9]")' ,
203- errors : createError ( '[0-9]' , String . raw `\d` ) ,
204- output : String . raw ` const foo = new RegExp("\\d")` ,
203+ errors : createError ( '[0-9]' , '\\d' ) ,
204+ output : ' const foo = new RegExp("\\\\ d")' ,
205205 } ,
206206 {
207- code : String . raw ` const foo = new RegExp('\ '[0-9]\'')` ,
208- errors : createError ( '\'[0-9]\'' , String . raw `'\d'` ) ,
209- output : String . raw ` const foo = new RegExp('\ '\\d\'')` ,
207+ code : ' const foo = new RegExp(\'\\\ '[0-9]\\\'\')' ,
208+ errors : createError ( '\'[0-9]\'' , '\'\\d\'' ) ,
209+ output : ' const foo = new RegExp(\'\\\ '\\\\d\\\'\')' ,
210210 } ,
211211 {
212212 code : 'const foo = new RegExp("\'[0-9]\'")' ,
213- errors : createError ( '\'[0-9]\'' , String . raw `'\d'` ) ,
214- output : String . raw ` const foo = new RegExp("'\\d '")` ,
213+ errors : createError ( '\'[0-9]\'' , '\'\\d\'' ) ,
214+ output : ' const foo = new RegExp("\ '\\\\d\ '")' ,
215215 } ,
216216 {
217217 code : 'const foo = new RegExp(\'[0-9]\', \'ig\')' ,
218- errors : createError ( '[0-9]' , String . raw `\d` ) ,
219- output : String . raw ` const foo = new RegExp('\\d ', 'ig')` ,
218+ errors : createError ( '[0-9]' , '\\d' ) ,
219+ output : ' const foo = new RegExp(\ '\\\\d\ ', \ 'ig\')' ,
220220 } ,
221221 {
222222 code : 'const foo = new RegExp(/[0-9]/, \'ig\')' ,
223- errors : createError ( '/[0-9]/' , String . raw `/\d/` ) ,
224- output : String . raw ` const foo = new RegExp(/\d/, 'ig')` ,
223+ errors : createError ( '/[0-9]/' , '/\\d/' ) ,
224+ output : ' const foo = new RegExp(/\\ d/, \ 'ig\')' ,
225225 } ,
226226 {
227227 code : 'const foo = new RegExp(/^[^*]*[*]?$/)' ,
228- errors : createError ( '/^[^*]*[*]?$/' , String . raw ` /^[^*]*\*?$/` ) ,
229- output : String . raw ` const foo = new RegExp(/^[^*]*\*?$/)` ,
228+ errors : createError ( '/^[^*]*[*]?$/' , ' /^[^*]*\\ *?$/' ) ,
229+ output : ' const foo = new RegExp(/^[^*]*\\ *?$/)' ,
230230 } ,
231231 // No `flags`
232232 {
233233 code : 'const foo = new RegExp(/[0-9]/)' ,
234- errors : createError ( '/[0-9]/' , String . raw `/\d/` ) ,
235- output : String . raw ` const foo = new RegExp(/\d/)` ,
234+ errors : createError ( '/[0-9]/' , '/\\d/' ) ,
235+ output : ' const foo = new RegExp(/\\ d/)' ,
236236 } ,
237237 // `flags` not `Literal`
238238 {
239239 code : 'const foo = new RegExp(/[0-9]/, ig)' ,
240- errors : createError ( '/[0-9]/' , String . raw `/\d/` ) ,
241- output : String . raw ` const foo = new RegExp(/\d/, ig)` ,
240+ errors : createError ( '/[0-9]/' , '/\\d/' ) ,
241+ output : ' const foo = new RegExp(/\\ d/, ig)' ,
242242 } ,
243243 // `flags` not `string`
244244 {
245245 code : 'const foo = new RegExp(/[0-9]/, 0)' ,
246- errors : createError ( '/[0-9]/' , String . raw `/\d/` ) ,
247- output : String . raw ` const foo = new RegExp(/\d/, 0)` ,
246+ errors : createError ( '/[0-9]/' , '/\\d/' ) ,
247+ output : ' const foo = new RegExp(/\\ d/, 0)' ,
248248 } ,
249249 // `\s` rewrite
250250 testCase (
251- String . raw ` /[ \f\n\r\t\v\ u00a0\u1680\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]+/` ,
252- String . raw `/\ s+/` ,
251+ ' /[ \\f\\n\\r\\t\\v\\ u00a0\\ u1680\\ u2000-\\ u200a\\ u2028\\ u2029\\ u202f\\ u205f\\ u3000\\ ufeff]+/' ,
252+ '/\\ s+/' ,
253253 ) ,
254254 // #499
255255 {
256- code : String . raw ` /^[a-z][a-z0-9\-]{5,29}$/` ,
257- errors : createError ( String . raw ` /^[a-z][a-z0-9\-]{5,29}$/` , String . raw ` /^[a-z][\da-z\-]{5,29}$/` ) ,
258- output : String . raw ` /^[a-z][\da-z\-]{5,29}$/` ,
256+ code : ' /^[a-z][a-z0-9\\ -]{5,29}$/' ,
257+ errors : createError ( ' /^[a-z][a-z0-9\\ -]{5,29}$/' , ' /^[a-z][\\ da-z\\ -]{5,29}$/' ) ,
258+ output : ' /^[a-z][\\ da-z\\ -]{5,29}$/' ,
259259 } ,
260260 // #477
261261 testCase (
262- String . raw ` /[ \n\t\r\ ]]/g` ,
263- String . raw ` /[\t\n\ r \]]/g` ,
262+ ' /[ \\n\\t\\r\\ ]]/g' ,
263+ ' /[\\t\\n\\ r \\ ]]/g' ,
264264 ) ,
265265 testCase (
266- String . raw ` /[ \n\t\r\ f"#'()/;[\\\]{}]/g` ,
267- String . raw ` /[\t\n\f\ r "#'()/;[\\\]{}]/g` ,
266+ ' /[ \\n\\t\\r\\ f"#\ '()/;[\\\\\\ ]{}]/g' ,
267+ ' /[\\t\\n\\f\\ r "#\ '()/;[\\\\\\ ]{}]/g' ,
268268 ) ,
269269 testCase (
270- String . raw ` /[ \n\t\r\ f(){}:;@!'"\\\][#]|\/(?=\*)/g` ,
271- String . raw ` /[\t\n\f\ r !"#'():;@[\\\]{}]|\/(?=\*)/g` ,
270+ ' /[ \\n\\t\\r\\ f(){}:;@!\ '"\\\\\\ ][#]|\\ /(?=\\ *)/g' ,
271+ ' /[\\t\\n\\f\\ r !"#\ '():;@[\\\\\\ ]{}]|\\ /(?=\\ *)/g' ,
272272 ) ,
273273 // #994
274274 testCase (
275- String . raw `/\ s?\s?\s?/` ,
276- String . raw `/\ s{0,3}/` ,
275+ '/\\ s?\\ s?\\ s?/' ,
276+ '/\\ s{0,3}/' ,
277277 ) ,
278278 // Actual message
279279 {
280280 code : '/[0-9]/' ,
281- output : String . raw `/\d/` ,
281+ output : '/\\d/' ,
282282 errors : [
283283 {
284- message : String . raw ` /[0-9]/ can be optimized to /\d/.` ,
284+ message : ' /[0-9]/ can be optimized to /\\ d/.' ,
285285 } ,
286286 ] ,
287287 } ,
@@ -301,11 +301,11 @@ test({
301301 // Not fixable
302302 {
303303 code : 'const foo = /[0-9]/.toString' ,
304- errors : createError ( '/[0-9]/' , String . raw `/\d/` ) ,
304+ errors : createError ( '/[0-9]/' , '/\\d/' ) ,
305305 } ,
306306 {
307307 code : 'const foo = /[0-9]/.source' ,
308- errors : createError ( '/[0-9]/' , String . raw `/\d/` ) ,
308+ errors : createError ( '/[0-9]/' , '/\\d/' ) ,
309309 } ,
310310 ] ,
311311} ) ;
0 commit comments