forked from oxc-project/oxc
/
lib.rs
283 lines (246 loc) · 11.5 KB
/
lib.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
#![allow(clippy::wildcard_imports, clippy::option_map_unit_fn)]
//! Transformer / Transpiler
//!
//! References:
//! * <https://www.typescriptlang.org/tsconfig#target>
//! * <https://babel.dev/docs/presets>
//! * <https://github.com/microsoft/TypeScript/blob/main/src/compiler/transformer.ts>
mod context;
mod es2015;
mod es2016;
mod es2019;
mod es2020;
mod es2021;
mod es2022;
mod es3;
mod options;
mod proposals;
mod react_jsx;
mod regexp;
#[cfg(test)]
mod tester;
mod typescript;
mod utils;
use std::{cell::RefCell, rc::Rc, sync::Arc};
use es2015::TemplateLiterals;
use oxc_allocator::Allocator;
use oxc_ast::{
ast::*,
visit::walk_mut::{
walk_assignment_expression_mut, walk_catch_clause_mut, walk_class_body_mut,
walk_declaration_mut, walk_expression_mut, walk_function_mut, walk_method_definition_mut,
walk_object_expression_mut, walk_object_property_mut, walk_program_mut, walk_statement_mut,
walk_statements_mut, walk_variable_declarator_mut,
},
AstBuilder, VisitMut,
};
use oxc_diagnostics::Error;
use oxc_semantic::{ScopeFlags, Semantic};
use oxc_span::SourceType;
use proposals::Decorators;
use crate::{
context::TransformerCtx,
es2015::*,
es2016::ExponentiationOperator,
es2019::{JsonStrings, OptionalCatchBinding},
es2020::NullishCoalescingOperator,
es2021::{LogicalAssignmentOperators, NumericSeparator},
es2022::ClassStaticBlock,
es3::PropertyLiteral,
react_jsx::ReactJsx,
regexp::RegexpFlags,
typescript::TypeScript,
utils::CreateVars,
};
pub use crate::{
es2015::ArrowFunctionsOptions,
es2020::NullishCoalescingOperatorOptions,
options::{TransformOptions, TransformTarget},
proposals::DecoratorsOptions,
react_jsx::{ReactJsxOptions, ReactJsxRuntime, ReactJsxRuntimeOption},
typescript::TypescriptOptions,
};
pub struct Transformer<'a> {
ctx: TransformerCtx<'a>,
decorators: Option<Decorators<'a>>,
#[allow(unused)]
typescript: Option<TypeScript<'a>>,
react_jsx: Option<ReactJsx<'a>>,
regexp_flags: Option<RegexpFlags<'a>>,
// es2022
es2022_class_static_block: Option<ClassStaticBlock<'a>>,
// es2021
es2021_logical_assignment_operators: Option<LogicalAssignmentOperators<'a>>,
es2021_numeric_separator: Option<NumericSeparator<'a>>,
// es2020
es2020_nullish_coalescing_operators: Option<NullishCoalescingOperator<'a>>,
// es2019
es2019_json_strings: Option<JsonStrings<'a>>,
es2019_optional_catch_binding: Option<OptionalCatchBinding<'a>>,
// es2016
es2016_exponentiation_operator: Option<ExponentiationOperator<'a>>,
// es2015
es2015_function_name: Option<FunctionName<'a>>,
es2015_arrow_functions: Option<ArrowFunctions<'a>>,
es2015_shorthand_properties: Option<ShorthandProperties<'a>>,
es2015_template_literals: Option<TemplateLiterals<'a>>,
es2015_duplicate_keys: Option<DuplicateKeys<'a>>,
es2015_instanceof: Option<Instanceof<'a>>,
es2015_literals: Option<Literals<'a>>,
es2015_new_target: Option<NewTarget<'a>>,
es3_property_literal: Option<PropertyLiteral<'a>>,
}
impl<'a> Transformer<'a> {
#[rustfmt::skip]
pub fn new(
allocator: &'a Allocator,
source_type: SourceType,
semantic: Semantic<'a>,
options: TransformOptions,
) -> Self {
let ast = Rc::new(AstBuilder::new(allocator));
let ctx = TransformerCtx::new(
Rc::clone(&ast),
Rc::new(RefCell::new(semantic)),
);
Self {
ctx: ctx.clone(),
decorators: Decorators::new(Rc::clone(&ast), ctx.clone(), &options),
typescript: source_type.is_typescript().then(|| TypeScript::new(Rc::clone(&ast), ctx.clone(), &options)),
regexp_flags: RegexpFlags::new(Rc::clone(&ast), &options),
// es2022
es2022_class_static_block: es2022::ClassStaticBlock::new(Rc::clone(&ast), &options),
// es2021
es2021_logical_assignment_operators: LogicalAssignmentOperators::new(Rc::clone(&ast), ctx.clone(), &options),
es2021_numeric_separator: NumericSeparator::new(ctx.clone(), &options),
// es2020
es2020_nullish_coalescing_operators: NullishCoalescingOperator::new(Rc::clone(&ast), ctx.clone(), &options),
// es2019
es2019_json_strings: JsonStrings::new(Rc::clone(&ast), &options),
es2019_optional_catch_binding: OptionalCatchBinding::new(Rc::clone(&ast), &options),
// es2016
es2016_exponentiation_operator: ExponentiationOperator::new(Rc::clone(&ast), ctx.clone(), &options),
// es2015
es2015_function_name: FunctionName::new(Rc::clone(&ast), ctx.clone(), &options),
es2015_arrow_functions: ArrowFunctions::new(Rc::clone(&ast), ctx.clone(), &options),
es2015_shorthand_properties: ShorthandProperties::new(Rc::clone(&ast), &options),
es2015_template_literals: TemplateLiterals::new(Rc::clone(&ast), &options),
es2015_duplicate_keys: DuplicateKeys::new(Rc::clone(&ast), &options),
es2015_instanceof: Instanceof::new(Rc::clone(&ast), ctx.clone(), &options),
es2015_literals: Literals::new(ctx.clone(), &options),
es2015_new_target: NewTarget::new(Rc::clone(&ast),ctx.clone(), &options),
// other
es3_property_literal: PropertyLiteral::new(Rc::clone(&ast), &options),
react_jsx: ReactJsx::new(Rc::clone(&ast), ctx.clone(), options)
}
}
/// # Errors
/// Returns `Vec<Error>` if any errors were collected during the transformation.
pub fn build(mut self, program: &mut Program<'a>) -> Result<(), Vec<Error>> {
self.visit_program(program);
let errors: Vec<_> = self
.ctx
.errors()
.into_iter()
.map(|e| e.with_source_code(Arc::new(self.ctx.semantic().source_text().to_string())))
.collect();
if errors.is_empty() {
Ok(())
} else {
Err(errors)
}
}
}
impl<'a> VisitMut<'a> for Transformer<'a> {
fn visit_program(&mut self, program: &mut Program<'a>) {
walk_program_mut(self, program);
self.typescript.as_mut().map(|t| t.transform_program(program));
self.react_jsx.as_mut().map(|t| t.add_react_jsx_runtime_imports(program));
self.decorators.as_mut().map(|t| t.transform_program(program));
}
fn visit_assignment_expression(&mut self, expr: &mut AssignmentExpression<'a>) {
self.es2015_function_name.as_mut().map(|t| t.transform_assignment_expression(expr));
walk_assignment_expression_mut(self, expr);
}
fn visit_statements(&mut self, stmts: &mut oxc_allocator::Vec<'a, Statement<'a>>) {
self.typescript.as_mut().map(|t| t.transform_statements(stmts));
walk_statements_mut(self, stmts);
// TODO: we need scope id to insert the vars into the correct statements
self.es2021_logical_assignment_operators.as_mut().map(|t| t.add_vars_to_statements(stmts));
self.es2020_nullish_coalescing_operators.as_mut().map(|t| t.add_vars_to_statements(stmts));
self.es2016_exponentiation_operator.as_mut().map(|t| t.add_vars_to_statements(stmts));
self.es2015_arrow_functions.as_mut().map(|t| t.transform_statements(stmts));
}
fn visit_statement(&mut self, stmt: &mut Statement<'a>) {
self.typescript.as_mut().map(|t| t.transform_statement(stmt));
self.decorators.as_mut().map(|t| t.transform_statement(stmt));
walk_statement_mut(self, stmt);
}
fn visit_declaration(&mut self, decl: &mut Declaration<'a>) {
walk_declaration_mut(self, decl);
self.typescript.as_mut().map(|t| t.transform_declaration(decl));
self.decorators.as_mut().map(|t| t.transform_declaration(decl));
}
fn visit_expression(&mut self, expr: &mut Expression<'a>) {
// self.typescript.as_mut().map(|t| t.transform_expression(expr));
self.react_jsx.as_mut().map(|t| t.transform_expression(expr));
self.regexp_flags.as_mut().map(|t| t.transform_expression(expr));
self.es2021_logical_assignment_operators.as_mut().map(|t| t.transform_expression(expr));
self.es2020_nullish_coalescing_operators.as_mut().map(|t| t.transform_expression(expr));
self.es2015_arrow_functions.as_mut().map(|t| t.transform_expression(expr));
self.es2015_instanceof.as_mut().map(|t| t.transform_expression(expr));
self.es2016_exponentiation_operator.as_mut().map(|t| t.transform_expression(expr));
self.es2015_template_literals.as_mut().map(|t| t.transform_expression(expr));
self.es2015_new_target.as_mut().map(|t| t.transform_expression(expr));
walk_expression_mut(self, expr);
}
fn visit_catch_clause(&mut self, clause: &mut CatchClause<'a>) {
self.es2019_optional_catch_binding.as_mut().map(|t| t.transform_catch_clause(clause));
walk_catch_clause_mut(self, clause);
}
fn visit_object_expression(&mut self, expr: &mut ObjectExpression<'a>) {
self.es2015_function_name.as_mut().map(|t| t.transform_object_expression(expr));
self.es2015_duplicate_keys.as_mut().map(|t| t.transform_object_expression(expr));
walk_object_expression_mut(self, expr);
}
fn visit_object_property(&mut self, prop: &mut ObjectProperty<'a>) {
self.es2015_new_target.as_mut().map(|t| t.enter_object_property(prop));
self.es2015_shorthand_properties.as_mut().map(|t| t.transform_object_property(prop));
self.es3_property_literal.as_mut().map(|t| t.transform_object_property(prop));
walk_object_property_mut(self, prop);
self.es2015_new_target.as_mut().map(|t| t.leave_object_property(prop));
}
fn visit_class_body(&mut self, body: &mut ClassBody<'a>) {
self.es2022_class_static_block.as_mut().map(|t| t.transform_class_body(body));
walk_class_body_mut(self, body);
}
fn visit_variable_declarator(&mut self, declarator: &mut VariableDeclarator<'a>) {
self.es2015_function_name.as_mut().map(|t| t.transform_variable_declarator(declarator));
walk_variable_declarator_mut(self, declarator);
}
fn visit_directive(&mut self, directive: &mut Directive<'a>) {
self.es2019_json_strings.as_mut().map(|t| t.transform_directive(directive));
}
fn visit_number_literal(&mut self, lit: &mut NumericLiteral<'a>) {
self.es2021_numeric_separator.as_mut().map(|t| t.transform_number_literal(lit));
self.es2015_literals.as_mut().map(|t| t.transform_number_literal(lit));
}
fn visit_bigint_literal(&mut self, lit: &mut BigIntLiteral<'a>) {
self.es2021_numeric_separator.as_mut().map(|t| t.transform_bigint_literal(lit));
}
fn visit_string_literal(&mut self, lit: &mut StringLiteral<'a>) {
self.es2019_json_strings.as_mut().map(|t| t.transform_string_literal(lit));
self.es2015_literals.as_mut().map(|t| t.transform_string_literal(lit));
}
fn visit_method_definition(&mut self, def: &mut MethodDefinition<'a>) {
self.es2015_new_target.as_mut().map(|t| t.enter_method_definition(def));
self.typescript.as_mut().map(|t| t.transform_method_definition(def));
walk_method_definition_mut(self, def);
self.es2015_new_target.as_mut().map(|t| t.leave_method_definition(def));
}
fn visit_function(&mut self, func: &mut Function<'a>, flags: Option<ScopeFlags>) {
self.es2015_new_target.as_mut().map(|t| t.enter_function(func));
walk_function_mut(self, func, flags);
self.es2015_new_target.as_mut().map(|t| t.leave_function(func));
}
}