-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
Copy pathmod.rs
400 lines (375 loc) · 15.1 KB
/
mod.rs
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
use rustc_hash::FxHashMap;
fn test(source_text: &str, expected: &str, options: Option<CodegenOptions>) {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true);
let parse_return = Parser::new(&allocator, source_text, source_type).parse();
let program = parse_return.program;
let program = allocator.alloc(program);
let options = options.unwrap_or_default();
let result = Codegen::<false>::new(
"",
source_text,
options,
Some(oxc_codegen::CommentGenRelated {
trivials: parse_return.trivias,
move_comment_map: FxHashMap::default(),
}),
)
.build(program)
.source_text;
assert_eq!(expected, result, "for source {source_text}, expect {expected}, got {result}");
}
fn test_ts(source_text: &str, expected: &str, is_typescript_definition: bool) {
let allocator = Allocator::default();
let source_type = SourceType::default()
.with_typescript(true)
.with_typescript_definition(is_typescript_definition)
.with_module(true);
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let codegen_options = CodegenOptions { enable_typescript: true, ..CodegenOptions::default() };
let result =
Codegen::<false>::new("", source_text, codegen_options, None).build(program).source_text;
assert_eq!(expected, result, "for source {source_text}, expect {expected}, got {result}");
}
#[test]
fn string() {
test("let x = ''", "let x = '';\n", None);
test(r"let x = '\b'", "let x = '\\b';\n", None);
test(r"let x = '\f'", "let x = '\\f';\n", None);
test("let x = '\t'", "let x = '\t';\n", None);
test(r"let x = '\v'", "let x = '\\v';\n", None);
test("let x = '\\n'", "let x = '\\n';\n", None);
test("let x = '\\''", "let x = \"'\";\n", None);
test("let x = '\\\"'", "let x = '\"';\n", None);
// test( "let x = '\\'''", "let x = `''`;\n", None);
test("let x = '\\\\'", "let x = '\\\\';\n", None);
test("let x = '\x00'", "let x = '\\0';\n", None);
test("let x = '\x00!'", "let x = '\\0!';\n", None);
test("let x = '\x001'", "let x = '\\x001';\n", None);
test("let x = '\\0'", "let x = '\\0';\n", None);
test("let x = '\\0!'", "let x = '\\0!';\n", None);
test("let x = '\x07'", "let x = '\\x07';\n", None);
test("let x = '\x07!'", "let x = '\\x07!';\n", None);
test("let x = '\x071'", "let x = '\\x071';\n", None);
test("let x = '\\7'", "let x = '\\x07';\n", None);
test("let x = '\\7!'", "let x = '\\x07!';\n", None);
test("let x = '\\01'", "let x = '\x01';\n", None);
test("let x = '\x10'", "let x = '\x10';\n", None);
test("let x = '\\x10'", "let x = '\x10';\n", None);
test("let x = '\x1B'", "let x = '\\x1B';\n", None);
test("let x = '\\x1B'", "let x = '\\x1B';\n", None);
test("let x = '\\uABCD'", "let x = 'ꯍ';\n", None);
// test( "let x = '\\uABCD'", r#"let x = '\uABCD';\n"#, None);
// test( r#"let x = '\U000123AB'"#, r#"let x = '\U000123AB';\n"#, None);
// test( "let x = '\\u{123AB}'", r#"let x = '\U000123AB';\n"#, None);
// test( "let x = '\\uD808\\uDFAB'", r#"let x = '\U000123AB';\n"#, None);
test("let x = '\\uD808'", "let x = '\\\\ud808';\n", None);
test("let x = '\\uD808X'", "let x = '\\\\ud808X';\n", None);
test("let x = '\\uDFAB'", "let x = '\\\\udfab';\n", None);
test("let x = '\\uDFABX'", "let x = '\\\\udfabX';\n", None);
// test( "let x = '\\x80'", r#"let x = '\U00000080';\n"#);
// test( "let x = '\\xFF'", r#"let x = '\U000000FF';\n"#);
// test( "let x = '\\xF0\\x9F\\x8D\\x95'", r#"let x = '\U000000F0\U0000009F\U0000008D\U00000095';\n"#);
// test("let x = '\\uD801\\uDC02\\uDC03\\uD804'", r#"let x = '\U00010402\\uDC03\\uD804';\n"#)
}
#[test]
fn template() {
test("let x = `\\0`", "let x = `\\0`;\n", None);
test("let x = `\\x01`", "let x = `\\x01`;\n", None);
test("let x = `\\0${0}`", "let x = `\\0${0}`;\n", None);
// test("let x = `\\x01${0}`", "let x = `\x01${0}`;\n", None);
test("let x = `${0}\\0`", "let x = `${0}\\0`;\n", None);
// test("let x = `${0}\\x01`", "let x = `${0}\x01`;\n", None);
test("let x = `${0}\\0${1}`", "let x = `${0}\\0${1}`;\n", None);
// test("let x = `${0}\\x01${1}`", "let x = `${0}\x01${1}`;\n", None);
test("let x = String.raw`\\1`", "let x = String.raw`\\1`;\n", None);
test("let x = String.raw`\\x01`", "let x = String.raw`\\x01`;\n", None);
test("let x = String.raw`\\1${0}`", "let x = String.raw`\\1${0}`;\n", None);
test("let x = String.raw`\\x01${0}`", "let x = String.raw`\\x01${0}`;\n", None);
test("let x = String.raw`${0}\\1`", "let x = String.raw`${0}\\1`;\n", None);
test("let x = String.raw`${0}\\x01`", "let x = String.raw`${0}\\x01`;\n", None);
test("let x = String.raw`${0}\\1${1}`", "let x = String.raw`${0}\\1${1}`;\n", None);
test("let x = String.raw`${0}\\x01${1}`", "let x = String.raw`${0}\\x01${1}`;\n", None);
test("let x = `${y}`", "let x = `${y}`;\n", None);
test("let x = `$(y)`", "let x = `$(y)`;\n", None);
test("let x = `{y}$`", "let x = `{y}$`;\n", None);
test("let x = `$}y{`", "let x = `$}y{`;\n", None);
test("let x = `\\${y}`", "let x = `\\${y}`;\n", None);
// test("let x = `$\\{y}`", "let x = `\\${y}`;\n", None);
test("await tag`x`", "await tag`x`;\n", None);
test("await (tag`x`)", "await tag`x`;\n", None);
test("(await tag)`x`", "(await tag)`x`;\n", None);
test("await tag`${x}`", "await tag`${x}`;\n", None);
test("await (tag`${x}`)", "await tag`${x}`;\n", None);
test("(await tag)`${x}`", "(await tag)`${x}`;\n", None);
test("new tag`x`", "new tag`x`();\n", None);
test("new (tag`x`)", "new tag`x`();\n", None);
test("new tag()`x`", "new tag()`x`;\n", None);
test("(new tag)`x`", "new tag()`x`;\n", None);
test("new tag`${x}`", "new tag`${x}`();\n", None);
test("new (tag`${x}`)", "new tag`${x}`();\n", None);
test("new tag()`${x}`", "new tag()`${x}`;\n", None);
test("(new tag)`${x}`", "new tag()`${x}`;\n", None);
}
#[test]
fn module_decl() {
test("export * as foo from 'foo'", "export * as foo from 'foo';\n", None);
test("import x from './foo.js' with {}", "import x from './foo.js' with {\n};\n", None);
test("import {} from './foo.js' with {}", "import './foo.js' with {\n};\n", None);
test("export * from './foo.js' with {}", "export * from './foo.js' with {\n};\n", None);
}
#[test]
fn new_expr() {
test("new (foo()).bar();", "new (foo()).bar();\n", None);
}
#[test]
fn for_stmt() {
test("for (let x = 0; x < 10; x++) {}", "for (let x = 0; x < 10; x++) {\n}\n", None);
test("for (;;) {}", "for (;;) {\n}\n", None);
test("for (let x = 1;;) {}", "for (let x = 1;;) {\n}\n", None);
test("for (;true;) {}", "for (; true;) {\n}\n", None);
test("for (;;i++) {}", "for (;; i++) {\n}\n", None);
test("for (using x = 1;;) {}", "for (using x = 1;;) {\n}\n", None);
}
#[test]
fn typescript() {
test_ts("let x: string = `\\x01`;", "let x: string = `\\x01`;\n", false);
test_ts("function foo<T extends string>(x: T, y: string, ...restOfParams: Omit<T, 'x'>): T {\n\treturn x;\n}", "function foo<T extends string>(x: T, y: string, ...restOfParams: Omit<T, 'x'>): T {\n\treturn x;\n}\n", false);
test_ts(
"let x: string[] = ['abc', 'def', 'ghi'];",
"let x: (string)[] = ['abc', 'def', 'ghi'];\n",
false,
);
test_ts(
"let x: Array<string> = ['abc', 'def', 'ghi',];",
"let x: Array<string> = ['abc', 'def', 'ghi',];\n",
false,
);
test_ts(
"let x: [string, number] = ['abc', 123];",
"let x: [string, number] = ['abc', 123];\n",
false,
);
test_ts("let x: string | number = 'abc';", "let x: ((string) | (number)) = 'abc';\n", false);
test_ts("let x: string & number = 'abc';", "let x: ((string) & (number)) = 'abc';\n", false);
test_ts("let x: typeof String = 'string';", "let x: typeof String = 'string';\n", false);
test_ts("let x: keyof string = 'length';", "let x: keyof string = 'length';\n", false);
test_ts(
"let x: keyof typeof String = 'length';",
"let x: keyof typeof String = 'length';\n",
false,
);
test_ts("let x: string['length'] = 123;", "let x: string['length'] = 123;\n", false);
test_ts("function isString(value: unknown): asserts value is string {\n\tif (typeof value !== 'string') {\n\t\tthrow new Error('Not a string');\n\t}\n}", "function isString(value: unknown): asserts value is string {\n\tif (typeof value !== 'string') {\n\t\tthrow new Error('Not a string');\n\t}\n}\n", false);
// type-only imports/exports
test_ts("import type { Foo } from 'foo';", "import type {Foo} from 'foo';\n", false);
test_ts("import { Foo, type Bar } from 'foo';", "import {Foo,type Bar} from 'foo';\n", false);
test_ts("export { Foo, type Bar } from 'foo';", "export { Foo, type Bar } from 'foo';", false);
}
fn test_comment_helper(source_text: &str, expected: &str) {
test(
source_text,
expected,
Some(CodegenOptions {
enable_source_map: true,
enable_typescript: false,
preserve_annotate_comments: true,
}),
);
}
#[test]
fn annotate_comment() {
test_comment_helper(
r"
x([
/* #__NO_SIDE_EFFECTS__ */ function() {},
/* #__NO_SIDE_EFFECTS__ */ function y() {},
/* #__NO_SIDE_EFFECTS__ */ function*() {},
/* #__NO_SIDE_EFFECTS__ */ function* y() {},
/* #__NO_SIDE_EFFECTS__ */ async function() {},
/* #__NO_SIDE_EFFECTS__ */ async function y() {},
/* #__NO_SIDE_EFFECTS__ */ async function*() {},
/* #__NO_SIDE_EFFECTS__ */ async function* y() {},
])
",
r"x([/* #__NO_SIDE_EFFECTS__ */ function() {
}, /* #__NO_SIDE_EFFECTS__ */ function y() {
}, /* #__NO_SIDE_EFFECTS__ */ function* () {
}, /* #__NO_SIDE_EFFECTS__ */ function* y() {
}, /* #__NO_SIDE_EFFECTS__ */ async function() {
}, /* #__NO_SIDE_EFFECTS__ */ async function y() {
}, /* #__NO_SIDE_EFFECTS__ */ async function* () {
}, /* #__NO_SIDE_EFFECTS__ */ async function* y() {
},]);
",
);
test_comment_helper(
r"
x([
/* #__NO_SIDE_EFFECTS__ */ y => y,
/* #__NO_SIDE_EFFECTS__ */ () => {},
/* #__NO_SIDE_EFFECTS__ */ (y) => (y),
/* #__NO_SIDE_EFFECTS__ */ async y => y,
/* #__NO_SIDE_EFFECTS__ */ async () => {},
/* #__NO_SIDE_EFFECTS__ */ async (y) => (y),
])",
r"x([/* #__NO_SIDE_EFFECTS__ */ y => y, /* #__NO_SIDE_EFFECTS__ */ () => {
}, /* #__NO_SIDE_EFFECTS__ */ y => y, /* #__NO_SIDE_EFFECTS__ */ async y => y, /* #__NO_SIDE_EFFECTS__ */ async() => {
}, /* #__NO_SIDE_EFFECTS__ */ async y => y,]);
",
);
test_comment_helper(
r"
x([
/* #__NO_SIDE_EFFECTS__ */ y => y,
/* #__NO_SIDE_EFFECTS__ */ () => {},
/* #__NO_SIDE_EFFECTS__ */ (y) => (y),
/* #__NO_SIDE_EFFECTS__ */ async y => y,
/* #__NO_SIDE_EFFECTS__ */ async () => {},
/* #__NO_SIDE_EFFECTS__ */ async (y) => (y),
])",
r"x([/* #__NO_SIDE_EFFECTS__ */ y => y, /* #__NO_SIDE_EFFECTS__ */ () => {
}, /* #__NO_SIDE_EFFECTS__ */ y => y, /* #__NO_SIDE_EFFECTS__ */ async y => y, /* #__NO_SIDE_EFFECTS__ */ async() => {
}, /* #__NO_SIDE_EFFECTS__ */ async y => y,]);
",
);
//
test_comment_helper(
r"
// #__NO_SIDE_EFFECTS__
function a() {}
// #__NO_SIDE_EFFECTS__
function* b() {}
// #__NO_SIDE_EFFECTS__
async function c() {}
// #__NO_SIDE_EFFECTS__
async function* d() {}
",
r"// #__NO_SIDE_EFFECTS__
function a() {
}
// #__NO_SIDE_EFFECTS__
function* b() {
}
// #__NO_SIDE_EFFECTS__
async function c() {
}
// #__NO_SIDE_EFFECTS__
async function* d() {
}
",
);
test_comment_helper(
r"
// #__NO_SIDE_EFFECTS__
function a() {}
// #__NO_SIDE_EFFECTS__
function* b() {}
// #__NO_SIDE_EFFECTS__
async function c() {}
// #__NO_SIDE_EFFECTS__
async function* d() {}
",
r"// #__NO_SIDE_EFFECTS__
function a() {
}
// #__NO_SIDE_EFFECTS__
function* b() {
}
// #__NO_SIDE_EFFECTS__
async function c() {
}
// #__NO_SIDE_EFFECTS__
async function* d() {
}
",
);
test_comment_helper(
r"
/* @__NO_SIDE_EFFECTS__ */ export function a() {}
/* @__NO_SIDE_EFFECTS__ */ export function* b() {}
/* @__NO_SIDE_EFFECTS__ */ export async function c() {}
/* @__NO_SIDE_EFFECTS__ */ export async function* d() {} ",
r"/* @__NO_SIDE_EFFECTS__ */ export function a() {
}
/* @__NO_SIDE_EFFECTS__ */ export function* b() {
}
/* @__NO_SIDE_EFFECTS__ */ export async function c() {
}
/* @__NO_SIDE_EFFECTS__ */ export async function* d() {
}
",
);
// Only "c0" and "c2" should have "no side effects" (Rollup only respects "const" and only for the first one)
test_comment_helper(
r"
/* #__NO_SIDE_EFFECTS__ */ export var v0 = function() {}, v1 = function() {}
/* #__NO_SIDE_EFFECTS__ */ export let l0 = function() {}, l1 = function() {}
/* #__NO_SIDE_EFFECTS__ */ export const c0 = function() {}, c1 = function() {}
/* #__NO_SIDE_EFFECTS__ */ export var v2 = () => {}, v3 = () => {}
/* #__NO_SIDE_EFFECTS__ */ export let l2 = () => {}, l3 = () => {}
/* #__NO_SIDE_EFFECTS__ */ export const c2 = () => {}, c3 = () => {}
",
r"export var v0 = function() {
}, v1 = function() {
};
export let l0 = function() {
}, l1 = function() {
};
export const c0 = /* #__NO_SIDE_EFFECTS__ */ function() {
}, c1 = function() {
};
export var v2 = () => {
}, v3 = () => {
};
export let l2 = () => {
}, l3 = () => {
};
export const c2 = /* #__NO_SIDE_EFFECTS__ */ () => {
}, c3 = () => {
};
",
);
// Only "c0" and "c2" should have "no side effects" (Rollup only respects "const" and only for the first one)
test_comment_helper(
r"
/* #__NO_SIDE_EFFECTS__ */ var v0 = function() {}, v1 = function() {}
/* #__NO_SIDE_EFFECTS__ */ let l0 = function() {}, l1 = function() {}
/* #__NO_SIDE_EFFECTS__ */ const c0 = function() {}, c1 = function() {}
/* #__NO_SIDE_EFFECTS__ */ var v2 = () => {}, v3 = () => {}
/* #__NO_SIDE_EFFECTS__ */ let l2 = () => {}, l3 = () => {}
/* #__NO_SIDE_EFFECTS__ */ const c2 = () => {}, c3 = () => {}
",
r"var v0 = function() {
}, v1 = function() {
};
let l0 = function() {
}, l1 = function() {
};
const c0 = /* #__NO_SIDE_EFFECTS__ */ function() {
}, c1 = function() {
};
var v2 = () => {
}, v3 = () => {
};
let l2 = () => {
}, l3 = () => {
};
const c2 = /* #__NO_SIDE_EFFECTS__ */ () => {
}, c3 = () => {
};
",
);
}
#[test]
fn unicode_escape() {
test("console.log('你好');", "console.log('你好');\n", None);
test("console.log('こんにちは');", "console.log('こんにちは');\n", None);
test("console.log('안녕하세요');", "console.log('안녕하세요');\n", None);
test("console.log('🧑🤝🧑');", "console.log('🧑🤝🧑');\n", None);
}