Skip to content

Commit

Permalink
es2015::destructuring pass (#312)
Browse files Browse the repository at this point in the history
swc_ecma_transforms:
 - es2015::destructuring pass now uses computed member if necessary. (#311)
  • Loading branch information
kdy1 authored Mar 5, 2019
1 parent 5f16412 commit b4a391b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
32 changes: 27 additions & 5 deletions ecmascript/transforms/src/compat/es2015/destructuring/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,34 @@ impl Fold<Vec<VarDeclarator>> for Destructuring {

match prop {
ObjectPatProp::KeyValue(KeyValuePatProp { key, value }) => {
let computed = match key {
PropName::Computed(..) => true,
_ => false,
};

let var_decl = VarDeclarator {
span: prop_span,
name: *value,
init: Some(box make_ref_prop_expr(
&ref_ident,
box prop_name_to_expr(key),
computed,
)),
definite: false,
};
decls.extend(vec![var_decl].fold_with(self));
}
ObjectPatProp::Assign(AssignPatProp { key, value, .. }) => {
let computed = false;

match value {
Some(value) => {
let ref_ident = make_ref_ident(
&mut decls,
Some(box make_ref_prop_expr(
&ref_ident,
box key.clone().into(),
computed,
)),
);

Expand All @@ -263,6 +272,7 @@ impl Fold<Vec<VarDeclarator>> for Destructuring {
init: Some(box make_ref_prop_expr(
&ref_ident,
box key.clone().into(),
computed,
)),
definite: false,
};
Expand Down Expand Up @@ -502,17 +512,25 @@ impl Fold<Expr> for AssignFolder {
let span = prop.span();
match prop {
ObjectPatProp::KeyValue(KeyValuePatProp { key, value }) => {
let computed = match key {
PropName::Computed(..) => true,
_ => false,
};

exprs.push(box Expr::Assign(AssignExpr {
span,
left: PatOrExpr::Pat(value),
op: op!("="),
right: box make_ref_prop_expr(
&ref_ident,
box prop_name_to_expr(key),
computed,
),
}));
}
ObjectPatProp::Assign(AssignPatProp { key, value, .. }) => {
let computed = false;

match value {
Some(value) => {
let prop_ident = make_ref_ident(&mut self.vars, None);
Expand All @@ -526,6 +544,7 @@ impl Fold<Expr> for AssignFolder {
right: box make_ref_prop_expr(
&ref_ident,
box key.clone().into(),
computed,
),
}));

Expand All @@ -548,6 +567,7 @@ impl Fold<Expr> for AssignFolder {
right: box make_ref_prop_expr(
&ref_ident,
box key.clone().into(),
computed,
),
}));
}
Expand Down Expand Up @@ -676,14 +696,16 @@ fn make_ref_ident(decls: &mut Vec<VarDeclarator>, init: Option<Box<Expr>>) -> Id
}
}

fn make_ref_prop_expr(ref_ident: &Ident, prop: Box<Expr>) -> Expr {
fn make_ref_prop_expr(ref_ident: &Ident, prop: Box<Expr>, mut computed: bool) -> Expr {
computed |= match *prop {
Expr::Lit(Lit::Num(..)) => true,
_ => false,
};

Expr::Member(MemberExpr {
span: DUMMY_SP,
obj: ExprOrSuper::Expr(box ref_ident.clone().into()),
computed: match *prop {
Expr::Ident(..) => false,
_ => true,
},
computed,
prop,
})
}
Expand Down
29 changes: 29 additions & 0 deletions ecmascript/transforms/src/compat/es2015/destructuring/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,3 +484,32 @@ test!(
return false;
}"#
);

test!(
::swc_ecma_parser::Syntax::default(),
|_| tr(),
issue_311,
"const Foo = 'foo';
const bar = {
[Foo]: {
qux: 'baz'
}
};
const {
[Foo]: {
qux
}
} = bar;",
"
const Foo = 'foo';
const bar = {
[Foo]: {
qux: 'baz'
}
};
const ref = bar ? bar : _throw(new TypeError(\"Cannot destructure 'undefined' or 'null'\")), \
_ref$Foo = ref[Foo], ref1 = _ref$Foo ? _ref$Foo : _throw(new TypeError(\"Cannot destructure \
'undefined' or 'null'\")), qux = ref1.qux;"
);

0 comments on commit b4a391b

Please sign in to comment.