forked from oxc-project/oxc
/
mod.rs
165 lines (148 loc) · 7.55 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
use oxc_allocator::Allocator;
use oxc_codegen::{Codegen, CodegenOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
fn test(source_text: &str, expected: &str) {
let allocator = Allocator::default();
let source_type = SourceType::default().with_module(true);
let program = Parser::new(&allocator, source_text, source_type).parse().program;
let program = allocator.alloc(program);
let result = Codegen::<false>::new("", source_text, CodegenOptions::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).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");
test(r"let x = '\b'", "let x = '\\b';\n");
test(r"let x = '\f'", "let x = '\\f';\n");
test("let x = '\t'", "let x = '\\t';\n");
test(r"let x = '\v'", "let x = '\\v';\n");
test("let x = '\\n'", "let x = '\\n';\n");
test("let x = '\\''", "let x = \"'\";\n");
test("let x = '\\\"'", "let x = '\"';\n");
// test( "let x = '\\'''", "let x = `''`;\n");
test("let x = '\\\\'", "let x = '\\\\';\n");
test("let x = '\x00'", "let x = '\\0';\n");
test("let x = '\x00!'", "let x = '\\0!';\n");
test("let x = '\x001'", "let x = '\\x001';\n");
test("let x = '\\0'", "let x = '\\0';\n");
test("let x = '\\0!'", "let x = '\\0!';\n");
test("let x = '\x07'", "let x = '\\x07';\n");
test("let x = '\x07!'", "let x = '\\x07!';\n");
test("let x = '\x071'", "let x = '\\x071';\n");
test("let x = '\\7'", "let x = '\\x07';\n");
test("let x = '\\7!'", "let x = '\\x07!';\n");
// test( "let x = '\\01'", "let x = '\x01';\n");
// test( "let x = '\x10'", "let x = '\x10';\n");
// test( "let x = '\\x10'", "let x = '\x10';\n");
test("let x = '\x1B'", "let x = '\\x1B';\n");
test("let x = '\\x1B'", "let x = '\\x1B';\n");
// test( r#"let x = '\uABCD'"#, r#"let x = "\uABCD";"#);
// test( "let x = '\\uABCD'", r#"let x = '\uABCD';\n"#);
// test( r#"let x = '\U000123AB'"#, r#"let x = '\U000123AB';\n"#);
// test( "let x = '\\u{123AB}'", r#"let x = '\U000123AB';\n"#);
// test( "let x = '\\uD808\\uDFAB'", r#"let x = '\U000123AB';\n"#);
test("let x = '\\uD808'", "let x = '\\\\ud808';\n");
test("let x = '\\uD808X'", "let x = '\\\\ud808X';\n");
test("let x = '\\uDFAB'", "let x = '\\\\udfab';\n");
test("let x = '\\uDFABX'", "let x = '\\\\udfabX';\n");
// 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");
test("let x = `\\x01`", "let x = `\\x01`;\n");
test("let x = `\\0${0}`", "let x = `\\0${0}`;\n");
// test("let x = `\\x01${0}`", "let x = `\x01${0}`;\n");
test("let x = `${0}\\0`", "let x = `${0}\\0`;\n");
// test("let x = `${0}\\x01`", "let x = `${0}\x01`;\n");
test("let x = `${0}\\0${1}`", "let x = `${0}\\0${1}`;\n");
// test("let x = `${0}\\x01${1}`", "let x = `${0}\x01${1}`;\n");
test("let x = String.raw`\\1`", "let x = String.raw`\\1`;\n");
test("let x = String.raw`\\x01`", "let x = String.raw`\\x01`;\n");
test("let x = String.raw`\\1${0}`", "let x = String.raw`\\1${0}`;\n");
test("let x = String.raw`\\x01${0}`", "let x = String.raw`\\x01${0}`;\n");
test("let x = String.raw`${0}\\1`", "let x = String.raw`${0}\\1`;\n");
test("let x = String.raw`${0}\\x01`", "let x = String.raw`${0}\\x01`;\n");
test("let x = String.raw`${0}\\1${1}`", "let x = String.raw`${0}\\1${1}`;\n");
test("let x = String.raw`${0}\\x01${1}`", "let x = String.raw`${0}\\x01${1}`;\n");
test("let x = `${y}`", "let x = `${y}`;\n");
test("let x = `$(y)`", "let x = `$(y)`;\n");
test("let x = `{y}$`", "let x = `{y}$`;\n");
test("let x = `$}y{`", "let x = `$}y{`;\n");
test("let x = `\\${y}`", "let x = `\\${y}`;\n");
// test("let x = `$\\{y}`", "let x = `\\${y}`;\n");
test("await tag`x`", "await tag`x`;\n");
test("await (tag`x`)", "await tag`x`;\n");
test("(await tag)`x`", "(await tag)`x`;\n");
test("await tag`${x}`", "await tag`${x}`;\n");
test("await (tag`${x}`)", "await tag`${x}`;\n");
test("(await tag)`${x}`", "(await tag)`${x}`;\n");
test("new tag`x`", "new tag`x`();\n");
test("new (tag`x`)", "new tag`x`();\n");
test("new tag()`x`", "new tag()`x`;\n");
test("(new tag)`x`", "new tag()`x`;\n");
test("new tag`${x}`", "new tag`${x}`();\n");
test("new (tag`${x}`)", "new tag`${x}`();\n");
test("new tag()`${x}`", "new tag()`${x}`;\n");
test("(new tag)`${x}`", "new tag()`${x}`;\n");
}
#[test]
fn module_decl() {
test("export * as foo from 'foo'", "export * as foo from 'foo';\n");
test("import x from './foo.js' with {}", "import x from './foo.js' with {\n};\n");
test("import {} from './foo.js' with {}", "import './foo.js' with {\n};\n");
test("export * from './foo.js' with {}", "export * from './foo.js' with {\n};\n");
}
#[test]
fn new_expr() {
test("new (foo()).bar();", "new (foo()).bar();\n");
}
#[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);
}