Skip to content

feat(es/codegen): Introduce SpanRewriter #10349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 104 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
92fd775
chore(ci): Fix CI
kdy1 Apr 10, 2025
2d0e878
simplify
kdy1 Apr 12, 2025
880b025
with_new_span
kdy1 Apr 12, 2025
73e56e4
NodeEmitter
kdy1 Apr 14, 2025
c663c32
Patch proc-macro
kdy1 Apr 14, 2025
e89cd31
trait impl
kdy1 Apr 14, 2025
1d43f81
fixup: Box
kdy1 Apr 14, 2025
4277d92
Deref & DerefMut
kdy1 Apr 14, 2025
5041603
macro: Adjust emit_node_inner
kdy1 Apr 14, 2025
8fda8b2
Rename macro
kdy1 Apr 14, 2025
37e5850
macros
kdy1 Apr 14, 2025
728813a
macro: only_new
kdy1 Apr 14, 2025
229cb81
WIP
kdy1 Apr 14, 2025
eb3c8f8
fix more
kdy1 Apr 14, 2025
04d9e50
only_new
kdy1 Apr 14, 2025
1f3013e
Refactor emit macro & usage
kdy1 Apr 14, 2025
23e4b20
fixup
kdy1 Apr 14, 2025
fb96cda
fixup
kdy1 Apr 14, 2025
289f624
only new
kdy1 Apr 14, 2025
0ea6919
SpannedWriteJs
kdy1 Apr 14, 2025
07b7540
FIx macro: lint
kdy1 Apr 14, 2025
8c8e5fa
impl MacroNode for Number
kdy1 Apr 14, 2025
b6d37e9
just simpler
kdy1 Apr 14, 2025
3cfa242
more
kdy1 Apr 18, 2025
fab0b35
Str
kdy1 Apr 18, 2025
408c6ff
Str: done
kdy1 Apr 18, 2025
9570f38
emit bool
kdy1 Apr 18, 2025
17f05eb
JSXElement
kdy1 Apr 18, 2025
18eb0da
JSXElementName
kdy1 Apr 18, 2025
9606837
JSXAttrOrSpread
kdy1 Apr 18, 2025
57ecd7c
jsx is done
kdy1 Apr 18, 2025
8b98025
some work for object
kdy1 Apr 18, 2025
ce6e5f9
more work
kdy1 Apr 18, 2025
24e7e13
objext
kdy1 Apr 18, 2025
68755ec
pat
kdy1 Apr 18, 2025
0cfe3e6
Ok()
kdy1 Apr 18, 2025
9dff73c
pat.rs: Use `only_new!`
kdy1 Apr 18, 2025
c45c9ad
typescript
kdy1 Apr 18, 2025
d11a34c
more
kdy1 Apr 18, 2025
eb6ee6e
more ts
kdy1 Apr 18, 2025
a83a863
more ts
kdy1 Apr 18, 2025
d248e60
more ts
kdy1 Apr 18, 2025
8693bf0
more ts
kdy1 Apr 18, 2025
a8814d1
more ts
kdy1 Apr 18, 2025
93f4de2
more ts
kdy1 Apr 18, 2025
80646d3
TsType
kdy1 Apr 18, 2025
7f3dd56
fixup
kdy1 Apr 18, 2025
d3fcb90
more ts
kdy1 Apr 18, 2025
d693ee5
TsTypeElement
kdy1 Apr 18, 2025
809bad7
ts
kdy1 Apr 18, 2025
97d00f1
Program
kdy1 Apr 20, 2025
38bdd7c
more expr
kdy1 Apr 20, 2025
8d96a8c
FnExpr
kdy1 Apr 20, 2025
8a4ec2d
ThisExpr
kdy1 Apr 20, 2025
d29f26c
impl MacroNode for ClassMember
kdy1 Apr 20, 2025
33d1866
impl MacroNode for Key
kdy1 Apr 20, 2025
63f0e05
more class
kdy1 Apr 20, 2025
cb8af79
more class
kdy1 Apr 20, 2025
cce4ac0
more
kdy1 Apr 20, 2025
4a0f963
more module
kdy1 Apr 20, 2025
2bfc78d
Callee
kdy1 Apr 20, 2025
5829c70
more
kdy1 Apr 20, 2025
8b37032
Ident
kdy1 Apr 20, 2025
1bbffbb
Private
kdy1 Apr 20, 2025
fff07d7
fix class expr
kdy1 Apr 22, 2025
ea73d9b
fix more
kdy1 Apr 22, 2025
e81d58e
more
kdy1 Apr 22, 2025
97495da
more
kdy1 Apr 22, 2025
9cafbb7
more
kdy1 Apr 22, 2025
4a73a4c
Stmt
kdy1 Apr 22, 2025
9762677
Stmt
kdy1 Apr 22, 2025
fe233c3
More sttm
kdy1 Apr 22, 2025
8ef06ad
More decl
kdy1 Apr 22, 2025
e976a11
Stmt
kdy1 Apr 22, 2025
366e615
Stmt
kdy1 Apr 22, 2025
7e594f9
more module_decl
kdy1 Apr 22, 2025
0aaa936
more module_decl
kdy1 Apr 22, 2025
40b56e0
more module_decl
kdy1 Apr 22, 2025
9a84df9
more module_decl
kdy1 Apr 22, 2025
d1b6bda
impl
kdy1 Apr 22, 2025
9d6d49e
more work
kdy1 Apr 22, 2025
e7b8e7e
fix more
kdy1 Apr 22, 2025
425e4cc
fix more
kdy1 Apr 22, 2025
d84017b
fix more
kdy1 Apr 22, 2025
5764864
fix more
kdy1 Apr 22, 2025
5af2abf
more
kdy1 Apr 22, 2025
4636b82
Bo::new
kdy1 Apr 22, 2025
c9f0704
Bo::new
kdy1 Apr 22, 2025
63689d1
more work
kdy1 Apr 22, 2025
e28af6d
clippy
kdy1 Apr 22, 2025
9771363
fix clippy
kdy1 Apr 22, 2025
cd80b83
assert
kdy1 Apr 23, 2025
4b77615
SpannedWriteJs
kdy1 Apr 23, 2025
7397967
fix build of test
kdy1 Apr 23, 2025
b7d7f8d
stmt
kdy1 Apr 23, 2025
988b254
semi
kdy1 Apr 23, 2025
70a4a6c
semi!(emitter);
kdy1 Apr 23, 2025
7f9fe63
Fix exprstmt
kdy1 Apr 23, 2025
7cfe3a5
fix test
kdy1 Apr 24, 2025
679f543
Fix test
kdy1 Apr 24, 2025
a3dfe20
bytepos
kdy1 Apr 24, 2025
50439a8
assert
kdy1 Apr 24, 2025
466d128
assert_enum
kdy1 Apr 24, 2025
fb1d187
fix
kdy1 Apr 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
230 changes: 170 additions & 60 deletions crates/swc_ecma_codegen/src/class.rs

Large diffs are not rendered by default.

113 changes: 92 additions & 21 deletions crates/swc_ecma_codegen/src/decl.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use swc_common::{SourceMapper, Spanned};
use swc_common::{SourceMapper, Span, Spanned};
use swc_ecma_ast::*;
use swc_ecma_codegen_macros::node_impl;

Expand Down Expand Up @@ -26,7 +26,7 @@ where

if !skip_decorators {
for dec in &node.class.decorators {
emit!(self, dec);
emit!(true, self, dec);
}
}

Expand All @@ -37,8 +37,8 @@ where

keyword!(self, "class");
space!(self);
emit!(self, node.ident);
emit!(self, node.class.type_params);
emit!(true, self, node.ident);
emit!(true, self, node.class.type_params);

self.emit_class_trailing(&node.class)?;

Expand Down Expand Up @@ -86,29 +86,68 @@ where
impl MacroNode for Decl {
fn emit(&mut self, emitter: &mut Macro) -> Result {
match self {
Decl::Class(n) => emit!(n),
Decl::Fn(n) => emit!(n),
Decl::Class(n) => {
let n = emit!(emitter, n);

Ok(only_new!(Decl::Class(n)))
}
Decl::Fn(n) => {
let n = emit!(emitter, n);

Ok(only_new!(Decl::Fn(n)))
}
Decl::Var(n) => {
emitter.emit_var_decl_inner(n)?;
formatting_semi!(emitter);
srcmap!(emitter, self, false);

Ok(only_new!(Decl::Var(n.clone())))
}
Decl::Using(n) => emit!(n),
Decl::TsEnum(n) => emit!(n),
Decl::TsInterface(n) => emit!(n),
Decl::TsModule(n) => emit!(n),
Decl::TsTypeAlias(n) => emit!(n),
}
Decl::Using(n) => {
let n = emit!(emitter, n);

Ok(())
Ok(only_new!(Decl::Using(n)))
}
Decl::TsEnum(n) => {
let n = emit!(emitter, n);

Ok(only_new!(Decl::TsEnum(n)))
}
Decl::TsInterface(n) => {
let n = emit!(emitter, n);

Ok(only_new!(Decl::TsInterface(n)))
}
Decl::TsModule(n) => {
let n = emit!(emitter, n);

Ok(only_new!(Decl::TsModule(n)))
}
Decl::TsTypeAlias(n) => {
let n = emit!(emitter, n);

Ok(only_new!(Decl::TsTypeAlias(n)))
}
}
}
}

#[node_impl]
impl MacroNode for ClassDecl {
fn emit(&mut self, emitter: &mut Macro) -> Result {
let lo = only_new!(emitter.wr.get_pos());

emitter.emit_class_decl_inner(self, false)?;
Ok(())

let hi = only_new!(emitter.wr.get_pos());

Ok(only_new!(ClassDecl {
class: Box::new(Class {
span: Span::new(lo, hi),
..*self.class.clone()
}),
..self.clone()
}))
}
}

Expand All @@ -117,6 +156,8 @@ impl MacroNode for UsingDecl {
fn emit(&mut self, emitter: &mut Macro) -> Result {
emitter.emit_leading_comments_of_span(self.span(), false)?;

let lo = only_new!(emitter.wr.get_pos());

if self.is_await {
keyword!(emitter, "await");
space!(emitter);
Expand All @@ -131,7 +172,12 @@ impl MacroNode for UsingDecl {
ListFormat::VariableDeclarationList,
)?;

Ok(())
let hi = only_new!(emitter.wr.get_pos());

Ok(only_new!(UsingDecl {
span: Span::new(lo, hi),
..self.clone()
}))
}
}

Expand All @@ -142,6 +188,8 @@ impl MacroNode for FnDecl {

emitter.wr.commit_pending_semi()?;

let lo = only_new!(emitter.wr.get_pos());

srcmap!(emitter, self, true);

if self.declare {
Expand All @@ -162,19 +210,35 @@ impl MacroNode for FnDecl {
space!(emitter);
}

emit!(self.ident);
emit!(emitter, self.ident);

emitter.emit_fn_trailing(&self.function)?;

Ok(())
let hi = only_new!(emitter.wr.get_pos());

Ok(only_new!(FnDecl {
function: Box::new(Function {
span: Span::new(lo, hi),
..*self.function.clone()
}),
..self.clone()
}))
}
}

#[node_impl]
impl MacroNode for VarDecl {
fn emit(&mut self, emitter: &mut Macro) -> Result {
let lo = only_new!(emitter.wr.get_pos());

emitter.emit_var_decl_inner(self)?;
Ok(())

let hi = only_new!(emitter.wr.get_pos());

Ok(only_new!(VarDecl {
span: Span::new(lo, hi),
..self.clone()
}))
}
}

Expand All @@ -183,18 +247,25 @@ impl MacroNode for VarDeclarator {
fn emit(&mut self, emitter: &mut Macro) -> Result {
emitter.emit_leading_comments_of_span(self.span(), false)?;

let lo = only_new!(emitter.wr.get_pos());

srcmap!(emitter, self, true);

emit!(self.name);
emit!(emitter, self.name);

if let Some(ref init) = self.init {
formatting_space!(emitter);
punct!(emitter, "=");
formatting_space!(emitter);
emit!(init);
emit!(emitter, init);
}

Ok(())
let hi = only_new!(emitter.wr.get_pos());

Ok(only_new!(VarDeclarator {
span: Span::new(lo, hi),
..self.clone()
}))
}
}

Expand Down
Loading
Loading