es2015::destructuring pass (#312)

swc_ecma_transforms:
 - es2015::destructuring pass now uses computed member if necessary. (#311)
This commit is contained in:
강동윤 2019-03-05 17:44:47 +09:00 committed by GitHub
parent 5f16412273
commit b4a391b3a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 5 deletions

View File

@ -226,18 +226,26 @@ 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(
@ -245,6 +253,7 @@ impl Fold<Vec<VarDeclarator>> for Destructuring {
Some(box make_ref_prop_expr(
&ref_ident,
box key.clone().into(),
computed,
)),
);
@ -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,
};
@ -502,6 +512,11 @@ 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),
@ -509,10 +524,13 @@ impl Fold<Expr> for AssignFolder {
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);
@ -526,6 +544,7 @@ impl Fold<Expr> for AssignFolder {
right: box make_ref_prop_expr(
&ref_ident,
box key.clone().into(),
computed,
),
}));
@ -548,6 +567,7 @@ impl Fold<Expr> for AssignFolder {
right: box make_ref_prop_expr(
&ref_ident,
box key.clone().into(),
computed,
),
}));
}
@ -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,
})
}

View File

@ -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;"
);