Skip to content

Commit

Permalink
feat(es/decorators): Groundwork for stage 3 decorator (#9450)
Browse files Browse the repository at this point in the history
**Description:**

I decided to port the babel transform instead of recreating a new pass using inputs and outputs. Babel transform reuses many codes, and this is the basic API for decorator passes that share the implementation.
  • Loading branch information
kdy1 authored Aug 19, 2024
1 parent 673655c commit 238ba8b
Show file tree
Hide file tree
Showing 8 changed files with 1,963 additions and 1,924 deletions.
7 changes: 7 additions & 0 deletions .changeset/rare-plants-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swc_core: patch
swc_ecma_ast: patch
swc_ecma_transforms_proposal: patch
---

feat(es/decorators): Groundwork for stage 3 decorator
1 change: 1 addition & 0 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ impl Options {
DecoratorVersion::V202203 => Box::new(
swc_ecma_transforms::proposals::decorator_2022_03::decorator_2022_03(),
),
DecoratorVersion::V202311 => todo!("2023-11 decorator"),
};

Box::new(chain!(
Expand Down
25 changes: 16 additions & 9 deletions crates/swc_ecma_ast/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Take for ClassMember {
}

#[ast_node("ClassProperty")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ClassProp {
#[cfg_attr(feature = "serde-impl", serde(default))]
Expand Down Expand Up @@ -135,7 +135,7 @@ pub struct ClassProp {
}

#[ast_node("PrivateProperty")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct PrivateProp {
#[cfg_attr(feature = "serde-impl", serde(default))]
Expand Down Expand Up @@ -176,7 +176,7 @@ pub struct PrivateProp {
}

#[ast_node("ClassMethod")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct ClassMethod {
#[cfg_attr(feature = "serde-impl", serde(default))]
Expand All @@ -199,7 +199,7 @@ pub struct ClassMethod {
}

#[ast_node("PrivateMethod")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct PrivateMethod {
#[cfg_attr(feature = "serde-impl", serde(default))]
Expand Down Expand Up @@ -244,7 +244,7 @@ pub struct Constructor {
}

#[ast_node("Decorator")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct Decorator {
pub span: Span,
Expand All @@ -253,7 +253,7 @@ pub struct Decorator {
pub expr: Box<Expr>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EqIgnoreSpan)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[cfg_attr(
any(feature = "rkyv-impl"),
Expand All @@ -263,6 +263,7 @@ pub struct Decorator {
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))]
#[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
pub enum MethodKind {
#[default]
#[cfg_attr(feature = "serde-impl", serde(rename = "method"))]
Method,
#[cfg_attr(feature = "serde-impl", serde(rename = "getter"))]
Expand All @@ -272,7 +273,7 @@ pub enum MethodKind {
}

#[ast_node("StaticBlock")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct StaticBlock {
pub span: Span,
Expand Down Expand Up @@ -312,12 +313,18 @@ bridge_from!(Key, PropName, BigInt);

impl Take for Key {
fn dummy() -> Self {
Key::Public(Take::dummy())
Default::default()
}
}

impl Default for Key {
fn default() -> Self {
Key::Public(Default::default())
}
}

#[ast_node("AutoAccessor")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct AutoAccessor {
#[cfg_attr(feature = "serde-impl", serde(default))]
Expand Down
7 changes: 6 additions & 1 deletion crates/swc_ecma_ast/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ impl<'a> arbitrary::Arbitrary<'a> for Ident {
}

#[ast_node("PrivateName")]
#[derive(Eq, Hash, EqIgnoreSpan)]
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub struct PrivateName {
pub span: Span,
Expand All @@ -528,6 +528,11 @@ impl Ident {
}
}

#[inline(never)]
pub fn new_private(sym: Atom, span: Span) -> Self {
Self::new(sym, span, SyntaxContext::empty().apply_mark(Mark::new()))
}

pub const fn new_no_ctxt(sym: Atom, span: Span) -> Self {
Self::new(sym, span, SyntaxContext::empty())
}
Expand Down
Loading

0 comments on commit 238ba8b

Please sign in to comment.