Skip to content

Commit

Permalink
decoration args
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Aug 26, 2024
1 parent af2a9a0 commit 8ac9b9e
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions crates/swc_ecma_transforms_proposal/src/decorator_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl DecoratorPass {
_ => ThisExpr { span: DUMMY_SP }.as_arg(),
};

let mut e_lhs = Vec::new();
let mut element_lhs = Vec::new();
let mut combined_args = vec![first_arg];

for id in self
Expand All @@ -141,7 +141,7 @@ impl DecoratorPass {
.drain(..)
.chain(self.state.proto_lhs.drain(..))
{
e_lhs.push(Some(id.into()));
element_lhs.push(Some(id.into()));
}

if let Some(init) = self.state.init_proto.clone() {
Expand All @@ -152,7 +152,7 @@ impl DecoratorPass {
definite: false,
});

e_lhs.push(Some(init.into()));
element_lhs.push(Some(init.into()));
}

if let Some(init) = self.state.init_static.clone() {
Expand All @@ -163,7 +163,7 @@ impl DecoratorPass {
definite: false,
});

e_lhs.push(Some(init.into()));
element_lhs.push(Some(init.into()));
}

combined_args.push(
Expand All @@ -187,26 +187,22 @@ impl DecoratorPass {
.as_arg(),
);

if let Some(super_class) = self.state.super_class.as_ref() {
combined_args.push(super_class.clone().as_arg());
}

let e_pat = if e_lhs.is_empty() {
let e_pat = if element_lhs.is_empty() {
None
} else {
Some(ObjectPatProp::KeyValue(KeyValuePatProp {
key: PropName::Ident("e".into()),
value: ArrayPat {
span: DUMMY_SP,
elems: e_lhs,
elems: element_lhs,
type_ann: Default::default(),
optional: false,
}
.into(),
}))
};

let c_pat = if self.state.class_lhs.is_empty() {
let class_pat = if self.state.class_lhs.is_empty() {
None
} else {
Some(ObjectPatProp::KeyValue(KeyValuePatProp {
Expand All @@ -221,7 +217,7 @@ impl DecoratorPass {
}))
};

let mut rhs = match self.version {
let rhs = match self.version {
DecoratorVersion::V202112 => todo!(),
DecoratorVersion::V202203 => Box::new(
CallExpr {
Expand All @@ -242,6 +238,13 @@ impl DecoratorPass {
combined_args.push((self.state.class_decorations_flag as f64).as_arg());
}

if let Some(brand_name) = &self.state.maybe_private_brand_name {
combined_args
.push(self.create_private_brand_check_closure(brand_name).as_arg());
} else if self.state.super_class.is_some() {
combined_args.push(Expr::undefined(DUMMY_SP).as_arg());
}

if let Some(super_class) = &self.state.super_class {
combined_args.push(super_class.clone().as_arg());
}
Expand All @@ -263,7 +266,7 @@ impl DecoratorPass {
op: op!("="),
left: ObjectPat {
span: DUMMY_SP,
props: e_pat.into_iter().chain(c_pat).collect(),
props: e_pat.into_iter().chain(class_pat).collect(),
optional: false,
type_ann: None,
}
Expand Down Expand Up @@ -853,6 +856,24 @@ impl DecoratorPass {
}
}
}

fn create_private_brand_check_closure(&self, brand_name: &PrivateName) -> Box<Expr> {
let param = private_ident!("_");
ArrowExpr {
params: vec![param.clone().into()],
body: Box::new(BlockStmtOrExpr::Expr(
BinExpr {
span: DUMMY_SP,
op: op!("in"),
left: brand_name.clone().into(),
right: param.clone().into(),
}
.into(),
)),
..Default::default()
}
.into()
}
}

impl VisitMut for DecoratorPass {
Expand Down

0 comments on commit 8ac9b9e

Please sign in to comment.