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.
kdy1 authored Aug 19, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 673655c commit 238ba8b
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
@@ -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!(
25 changes: 16 additions & 9 deletions crates/swc_ecma_ast/src/class.rs
Original file line number Diff line number Diff line change
@@ -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))]
@@ -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))]
@@ -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))]
@@ -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))]
@@ -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,
@@ -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"),
@@ -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"))]
@@ -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,
@@ -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))]
7 changes: 6 additions & 1 deletion crates/swc_ecma_ast/src/ident.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -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())
}
1,914 changes: 3 additions & 1,911 deletions crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs

Large diffs are not rendered by default.

1,923 changes: 1,923 additions & 0 deletions crates/swc_ecma_transforms_proposal/src/decorator_impl.rs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions crates/swc_ecma_transforms_proposal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -17,9 +17,13 @@ pub enum DecoratorVersion {

#[serde(rename = "2022-03")]
V202203,

#[serde(rename = "2023-11")]
V202311,
}

pub mod decorator_2022_03;
mod decorator_impl;
pub mod decorators;
pub mod explicit_resource_management;
mod export_default_from;
6 changes: 3 additions & 3 deletions crates/swc_ecma_transforms_proposal/tests/decorators.rs
Original file line number Diff line number Diff line change
@@ -162,9 +162,9 @@ fn create_pass(comments: Rc<SingleThreadedComments>, input: &Path) -> Box<dyn Fo
BabelPluginEntry::WithConfig(name, config) => match &**name {
"proposal-decorators" => match config {
BabelPluginOption::Decorator { version } => match version {
// DecoratorVersion::V202311 => {
// add!(decorator_2023_11());
// }
DecoratorVersion::V202311 => {
todo!()
}
DecoratorVersion::V202112 => todo!(),
DecoratorVersion::V202203 => {
add!(decorator_2022_03());

0 comments on commit 238ba8b

Please sign in to comment.