Skip to content

Commit 238ba8b

Browse files
authoredAug 19, 2024··
feat(es/decorators): Groundwork for stage 3 decorator (#9450)
**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.
1 parent 673655c commit 238ba8b

File tree

8 files changed

+1963
-1924
lines changed

8 files changed

+1963
-1924
lines changed
 

‎.changeset/rare-plants-shout.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_ast: patch
4+
swc_ecma_transforms_proposal: patch
5+
---
6+
7+
feat(es/decorators): Groundwork for stage 3 decorator

‎crates/swc/src/config/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,7 @@ impl Options {
687687
DecoratorVersion::V202203 => Box::new(
688688
swc_ecma_transforms::proposals::decorator_2022_03::decorator_2022_03(),
689689
),
690+
DecoratorVersion::V202311 => todo!("2023-11 decorator"),
690691
};
691692

692693
Box::new(chain!(

‎crates/swc_ecma_ast/src/class.rs

+16-9
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl Take for ClassMember {
9090
}
9191

9292
#[ast_node("ClassProperty")]
93-
#[derive(Eq, Hash, EqIgnoreSpan)]
93+
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
9494
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
9595
pub struct ClassProp {
9696
#[cfg_attr(feature = "serde-impl", serde(default))]
@@ -135,7 +135,7 @@ pub struct ClassProp {
135135
}
136136

137137
#[ast_node("PrivateProperty")]
138-
#[derive(Eq, Hash, EqIgnoreSpan)]
138+
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
139139
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
140140
pub struct PrivateProp {
141141
#[cfg_attr(feature = "serde-impl", serde(default))]
@@ -176,7 +176,7 @@ pub struct PrivateProp {
176176
}
177177

178178
#[ast_node("ClassMethod")]
179-
#[derive(Eq, Hash, EqIgnoreSpan)]
179+
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
180180
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
181181
pub struct ClassMethod {
182182
#[cfg_attr(feature = "serde-impl", serde(default))]
@@ -199,7 +199,7 @@ pub struct ClassMethod {
199199
}
200200

201201
#[ast_node("PrivateMethod")]
202-
#[derive(Eq, Hash, EqIgnoreSpan)]
202+
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
203203
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
204204
pub struct PrivateMethod {
205205
#[cfg_attr(feature = "serde-impl", serde(default))]
@@ -244,7 +244,7 @@ pub struct Constructor {
244244
}
245245

246246
#[ast_node("Decorator")]
247-
#[derive(Eq, Hash, EqIgnoreSpan)]
247+
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
248248
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
249249
pub struct Decorator {
250250
pub span: Span,
@@ -253,7 +253,7 @@ pub struct Decorator {
253253
pub expr: Box<Expr>,
254254
}
255255

256-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EqIgnoreSpan)]
256+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, EqIgnoreSpan, Default)]
257257
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
258258
#[cfg_attr(
259259
any(feature = "rkyv-impl"),
@@ -263,6 +263,7 @@ pub struct Decorator {
263263
#[cfg_attr(feature = "rkyv-impl", archive_attr(repr(u32)))]
264264
#[cfg_attr(feature = "serde-impl", derive(serde::Serialize, serde::Deserialize))]
265265
pub enum MethodKind {
266+
#[default]
266267
#[cfg_attr(feature = "serde-impl", serde(rename = "method"))]
267268
Method,
268269
#[cfg_attr(feature = "serde-impl", serde(rename = "getter"))]
@@ -272,7 +273,7 @@ pub enum MethodKind {
272273
}
273274

274275
#[ast_node("StaticBlock")]
275-
#[derive(Eq, Hash, EqIgnoreSpan)]
276+
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
276277
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
277278
pub struct StaticBlock {
278279
pub span: Span,
@@ -312,12 +313,18 @@ bridge_from!(Key, PropName, BigInt);
312313

313314
impl Take for Key {
314315
fn dummy() -> Self {
315-
Key::Public(Take::dummy())
316+
Default::default()
317+
}
318+
}
319+
320+
impl Default for Key {
321+
fn default() -> Self {
322+
Key::Public(Default::default())
316323
}
317324
}
318325

319326
#[ast_node("AutoAccessor")]
320-
#[derive(Eq, Hash, EqIgnoreSpan)]
327+
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
321328
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
322329
pub struct AutoAccessor {
323330
#[cfg_attr(feature = "serde-impl", serde(default))]

‎crates/swc_ecma_ast/src/ident.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ impl<'a> arbitrary::Arbitrary<'a> for Ident {
504504
}
505505

506506
#[ast_node("PrivateName")]
507-
#[derive(Eq, Hash, EqIgnoreSpan)]
507+
#[derive(Eq, Hash, EqIgnoreSpan, Default)]
508508
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
509509
pub struct PrivateName {
510510
pub span: Span,
@@ -528,6 +528,11 @@ impl Ident {
528528
}
529529
}
530530

531+
#[inline(never)]
532+
pub fn new_private(sym: Atom, span: Span) -> Self {
533+
Self::new(sym, span, SyntaxContext::empty().apply_mark(Mark::new()))
534+
}
535+
531536
pub const fn new_no_ctxt(sym: Atom, span: Span) -> Self {
532537
Self::new(sym, span, SyntaxContext::empty())
533538
}

‎crates/swc_ecma_transforms_proposal/src/decorator_2022_03.rs

+3-1,911
Large diffs are not rendered by default.

‎crates/swc_ecma_transforms_proposal/src/decorator_impl.rs

+1,923
Large diffs are not rendered by default.

‎crates/swc_ecma_transforms_proposal/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ pub enum DecoratorVersion {
1717

1818
#[serde(rename = "2022-03")]
1919
V202203,
20+
21+
#[serde(rename = "2023-11")]
22+
V202311,
2023
}
2124

2225
pub mod decorator_2022_03;
26+
mod decorator_impl;
2327
pub mod decorators;
2428
pub mod explicit_resource_management;
2529
mod export_default_from;

‎crates/swc_ecma_transforms_proposal/tests/decorators.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,9 @@ fn create_pass(comments: Rc<SingleThreadedComments>, input: &Path) -> Box<dyn Fo
162162
BabelPluginEntry::WithConfig(name, config) => match &**name {
163163
"proposal-decorators" => match config {
164164
BabelPluginOption::Decorator { version } => match version {
165-
// DecoratorVersion::V202311 => {
166-
// add!(decorator_2023_11());
167-
// }
165+
DecoratorVersion::V202311 => {
166+
todo!()
167+
}
168168
DecoratorVersion::V202112 => todo!(),
169169
DecoratorVersion::V202203 => {
170170
add!(decorator_2022_03());

0 commit comments

Comments
 (0)
Please sign in to comment.