oxc_transformer/es2018/
mod.rs

1use oxc_ast::ast::*;
2use oxc_traverse::Traverse;
3
4use crate::{
5    context::{TransformCtx, TraverseCtx},
6    state::TransformState,
7};
8
9mod async_generator_functions;
10mod object_rest_spread;
11mod options;
12
13pub use async_generator_functions::AsyncGeneratorFunctions;
14pub use object_rest_spread::{ObjectRestSpread, ObjectRestSpreadOptions};
15pub use options::ES2018Options;
16
17pub struct ES2018<'a, 'ctx> {
18    options: ES2018Options,
19
20    // Plugins
21    object_rest_spread: ObjectRestSpread<'a, 'ctx>,
22    async_generator_functions: AsyncGeneratorFunctions<'a, 'ctx>,
23}
24
25impl<'a, 'ctx> ES2018<'a, 'ctx> {
26    pub fn new(options: ES2018Options, ctx: &'ctx TransformCtx<'a>) -> Self {
27        Self {
28            object_rest_spread: ObjectRestSpread::new(
29                options.object_rest_spread.unwrap_or_default(),
30                ctx,
31            ),
32            async_generator_functions: AsyncGeneratorFunctions::new(ctx),
33            options,
34        }
35    }
36}
37
38impl<'a> Traverse<'a, TransformState<'a>> for ES2018<'a, '_> {
39    fn exit_program(&mut self, program: &mut oxc_ast::ast::Program<'a>, ctx: &mut TraverseCtx<'a>) {
40        if self.options.object_rest_spread.is_some() {
41            self.object_rest_spread.exit_program(program, ctx);
42        }
43    }
44
45    fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
46        if self.options.object_rest_spread.is_some() {
47            self.object_rest_spread.enter_expression(expr, ctx);
48        }
49    }
50
51    fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
52        if self.options.async_generator_functions {
53            self.async_generator_functions.exit_expression(expr, ctx);
54        }
55    }
56
57    fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
58        if self.options.async_generator_functions {
59            self.async_generator_functions.enter_statement(stmt, ctx);
60        }
61    }
62
63    fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
64        if self.options.async_generator_functions {
65            self.async_generator_functions.exit_statement(stmt, ctx);
66        }
67    }
68
69    fn enter_for_in_statement(&mut self, stmt: &mut ForInStatement<'a>, ctx: &mut TraverseCtx<'a>) {
70        if self.options.object_rest_spread.is_some() {
71            self.object_rest_spread.enter_for_in_statement(stmt, ctx);
72        }
73    }
74
75    fn enter_for_of_statement(&mut self, stmt: &mut ForOfStatement<'a>, ctx: &mut TraverseCtx<'a>) {
76        if self.options.async_generator_functions {
77            self.async_generator_functions.enter_for_of_statement(stmt, ctx);
78        }
79        if self.options.object_rest_spread.is_some() {
80            self.object_rest_spread.enter_for_of_statement(stmt, ctx);
81        }
82    }
83
84    fn enter_arrow_function_expression(
85        &mut self,
86        arrow: &mut ArrowFunctionExpression<'a>,
87        ctx: &mut TraverseCtx<'a>,
88    ) {
89        if self.options.object_rest_spread.is_some() {
90            self.object_rest_spread.enter_arrow_function_expression(arrow, ctx);
91        }
92    }
93
94    fn enter_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
95        if self.options.object_rest_spread.is_some() {
96            self.object_rest_spread.enter_function(func, ctx);
97        }
98    }
99
100    fn exit_function(&mut self, node: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
101        if self.options.async_generator_functions {
102            self.async_generator_functions.exit_function(node, ctx);
103        }
104    }
105
106    fn enter_variable_declaration(
107        &mut self,
108        decl: &mut VariableDeclaration<'a>,
109        ctx: &mut TraverseCtx<'a>,
110    ) {
111        if self.options.object_rest_spread.is_some() {
112            self.object_rest_spread.enter_variable_declaration(decl, ctx);
113        }
114    }
115
116    fn enter_catch_clause(&mut self, clause: &mut CatchClause<'a>, ctx: &mut TraverseCtx<'a>) {
117        if self.options.object_rest_spread.is_some() {
118            self.object_rest_spread.enter_catch_clause(clause, ctx);
119        }
120    }
121}