Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 11, 2024
1 parent 1282221 commit 13841ca
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
12 changes: 11 additions & 1 deletion crates/oxc_transformer/src/es2018/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub(crate) mod async_generator_functions;
mod object_rest_spread;
mod options;

use oxc_ast::ast::{Expression, ForOfStatement, Function, Statement};
use oxc_ast::ast::{Expression, ForOfStatement, Function, Statement, VariableDeclaration};
use oxc_traverse::{Traverse, TraverseCtx};

use crate::context::TransformCtx;
Expand Down Expand Up @@ -67,4 +67,14 @@ impl<'a, 'ctx> Traverse<'a> for ES2018<'a, 'ctx> {
self.async_generator_functions.exit_function(node, ctx);
}
}

fn enter_variable_declaration(
&mut self,
decl: &mut VariableDeclaration<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if self.options.object_rest_spread.is_some() {
self.object_rest_spread.enter_variable_declaration(decl, ctx);
}
}
}
60 changes: 59 additions & 1 deletion crates/oxc_transformer/src/es2018/object_rest_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
use serde::Deserialize;

use oxc_ast::{ast::*, NONE};
use oxc_semantic::{ReferenceFlags, SymbolId};
use oxc_semantic::{ReferenceFlags, SymbolFlags, SymbolId};
use oxc_span::SPAN;
use oxc_traverse::{Traverse, TraverseCtx};

Expand All @@ -46,6 +46,7 @@ pub struct ObjectRestSpreadOptions {

pub struct ObjectRestSpread<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,

options: ObjectRestSpreadOptions,
}

Expand All @@ -59,6 +60,14 @@ impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.transform_object_expression(expr, ctx);
}

fn enter_variable_declaration(
&mut self,
decl: &mut VariableDeclaration<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.transform_variable_declaration(decl, ctx);
}
}

impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
Expand Down Expand Up @@ -148,3 +157,52 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
}
}

impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
fn transform_variable_declaration(
&mut self,
decl: &mut VariableDeclaration<'a>,
ctx: &mut TraverseCtx<'a>,
) {
for variable_declarator in decl.declarations.iter_mut() {
self.transform_variable_declarator(variable_declarator, ctx);
}
}

fn transform_variable_declarator(
&mut self,
decl: &mut VariableDeclarator<'a>,
ctx: &mut TraverseCtx<'a>,
) {
// Get the object pattern.
let BindingPatternKind::ObjectPattern(pat) = &decl.id.kind else { return };
// Does not have a object rest.
let Some(rest) = &pat.rest else {
return;
};
// Rest binding is not an identifier (syntax error).
if !rest.argument.kind.is_binding_identifier() {
return;
}
// Does not have an init (syntax error).
let Some(init) = &decl.init else { return };

let binding = ctx
.generate_uid_in_current_scope_based_on_node(init, SymbolFlags::FunctionScopedVariable);

decl.id.kind = ctx
.ast
.binding_pattern_kind_from_binding_identifier(binding.create_binding_identifier(ctx));

// let mut new_declarators = ctx.ast.vec();

let BindingPatternKind::ObjectPattern(pat) = &mut decl.id.kind else { unreachable!() };
let BindingPatternKind::BindingIdentifier(rest_ident) = pat.rest.unwrap().argument.kind
else {
unreachable!()
};

// rest
dbg!(binding);
}
}
8 changes: 8 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ impl<'a, 'ctx> Traverse<'a> for TransformerImpl<'a, 'ctx> {
}
}

fn enter_variable_declaration(
&mut self,
decl: &mut VariableDeclaration<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.x2_es2018.enter_variable_declaration(decl, ctx);
}

fn enter_variable_declarator(
&mut self,
decl: &mut VariableDeclarator<'a>,
Expand Down

0 comments on commit 13841ca

Please sign in to comment.