Skip to content

Commit

Permalink
refactor(es/compat): Use dummy span instead of passing `static_blocks…
Browse files Browse the repository at this point in the history
…_mark`
  • Loading branch information
magic-akari committed Nov 9, 2024
1 parent 1606d08 commit 3cba25b
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 46 deletions.
1 change: 0 additions & 1 deletion crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ impl<'a, 'b, P: Pass> PassBuilder<'a, 'b, P> {
constant_super: assumptions.constant_super,
set_public_fields: assumptions.set_public_class_fields,
no_document_all: assumptions.no_document_all,
static_blocks_mark: Mark::new(),
pure_getter: assumptions.pure_getters,
},
},
Expand Down
5 changes: 2 additions & 3 deletions crates/swc_ecma_compat_es2022/src/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ pub struct Config {
pub constant_super: bool,
pub no_document_all: bool,
pub pure_getter: bool,
pub static_blocks_mark: Mark,
}

impl Default for Config {
Expand All @@ -67,7 +66,6 @@ impl Default for Config {
constant_super: false,
no_document_all: false,
pure_getter: false,
static_blocks_mark: Mark::new(),
}
}
}
Expand Down Expand Up @@ -734,7 +732,8 @@ impl ClassProperties {

let value = prop.value.unwrap_or_else(|| Expr::undefined(prop_span));

if prop.is_static && prop.ctxt.has_mark(self.c.static_blocks_mark) {
// Hack: dummy span is mark for generated private names.
if prop.is_static && prop.key.span.is_dummy() {
let init = MemberInit::StaticBlock(value);
extra_inits.push(init);
continue;
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_compat_es2022/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn es2022(config: Config, unresolved_mark: Mark) -> impl Pass {
unicode_regex: false,
unicode_sets_regex: false,
}),
static_blocks(config.class_properties.static_blocks_mark),
static_blocks(),
class_properties(config.class_properties, unresolved_mark),
private_in_object(),
)
Expand Down
25 changes: 5 additions & 20 deletions crates/swc_ecma_compat_es2022/src/static_blocks.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use swc_atoms::JsWord;
use swc_common::{collections::AHashSet, util::take::Take, Mark, SyntaxContext, DUMMY_SP};
use swc_common::{collections::AHashSet, util::take::Take, DUMMY_SP};
use swc_ecma_ast::*;
use swc_ecma_utils::ExprFactory;
use swc_ecma_visit::{noop_visit_mut_type, visit_mut_pass, VisitMut, VisitMutWith};
use swc_trace_macro::swc_trace;

struct ClassStaticBlock {
static_block_mark: Mark,
}
struct ClassStaticBlock;

pub fn static_blocks(static_block_mark: Mark) -> impl Pass {
visit_mut_pass(ClassStaticBlock { static_block_mark })
pub fn static_blocks() -> impl Pass {
visit_mut_pass(ClassStaticBlock)
}

#[swc_trace]
Expand All @@ -31,17 +29,11 @@ impl ClassStaticBlock {
static_block.body.stmts = stmts;

let expr = CallExpr {
span: DUMMY_SP,
callee: ArrowExpr {
span: DUMMY_SP,
params: Vec::new(),
is_async: false,
is_generator: false,
body: Box::new(BlockStmtOrExpr::BlockStmt(static_block.body)),
..Default::default()
}
.as_callee(),
args: Vec::new(),
..Default::default()
}
.into();
Expand All @@ -52,19 +44,12 @@ impl ClassStaticBlock {
PrivateProp {
span,
is_static: true,
is_optional: false,
is_override: false,
readonly: false,
type_ann: None,
decorators: Vec::new(),
accessibility: None,
key: PrivateName {
span: DUMMY_SP,
name: private_id,
},
value,
definite: false,
ctxt: SyntaxContext::empty().apply_mark(self.static_block_mark),
..Default::default()
}
}
}
Expand Down
9 changes: 2 additions & 7 deletions crates/swc_ecma_preset_env/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,8 @@ where
// ES2022
// static block needs to be placed before class property
// because it transforms into private static property
let static_blocks_mark = Mark::new();
let pass = add!(
pass,
ClassStaticBlock,
es2022::static_blocks(static_blocks_mark)
);

let pass = add!(pass, ClassStaticBlock, es2022::static_blocks());
let pass = add!(
pass,
ClassProperties,
Expand All @@ -148,7 +144,6 @@ where
set_public_fields: loose || assumptions.set_public_class_fields,
constant_super: loose || assumptions.constant_super,
no_document_all: loose || assumptions.no_document_all,
static_blocks_mark,
pure_getter: loose || assumptions.pure_getters,
},
unresolved_mark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ fn get_passes(_: &Tester, plugins: &[PluginConfig]) -> Box<dyn Pass> {
set_public_fields: loose,
private_as_properties: loose,
no_document_all: loose,
static_blocks_mark: Mark::new(),
pure_getter: loose,
},
unresolved_mark,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ fn fixture(input: PathBuf) {
no_document_all: loose,
private_as_properties: loose,
pure_getter: loose,
static_blocks_mark: Mark::new(),
},
unresolved_mark,
),
Expand All @@ -84,7 +83,6 @@ fn fixture(input: PathBuf) {
no_document_all: loose,
private_as_properties: loose,
pure_getter: loose,
static_blocks_mark: Mark::new(),
},
unresolved_mark,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ fn fixture(input: PathBuf) {
let pass: Box<dyn Pass> = if input.to_string_lossy().contains("class-properties") {
Box::new((
resolver(unresolved_mark, top_level_mark, false),
static_blocks(config.static_blocks_mark),
static_blocks(),
class_properties(config, unresolved_mark),
))
} else {
Box::new((
resolver(unresolved_mark, top_level_mark, false),
static_blocks(config.static_blocks_mark),
static_blocks(),
))
};
pass
Expand Down
8 changes: 2 additions & 6 deletions crates/swc_ecma_transforms_proposal/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ fn create_pass(comments: Rc<SingleThreadedComments>, input: &Path) -> Box<dyn Pa
match plugin {
BabelPluginEntry::NameOnly(name) => match &**name {
"proposal-class-properties" => {
add!(swc_ecma_transforms_compat::es2022::static_blocks(
static_block_mark
));
add!(swc_ecma_transforms_compat::es2022::static_blocks());
add!(swc_ecma_transforms_compat::es2022::class_properties(
Default::default(),
unresolved_mark
Expand All @@ -152,9 +150,7 @@ fn create_pass(comments: Rc<SingleThreadedComments>, input: &Path) -> Box<dyn Pa
}

"proposal-class-static-block" => {
add!(swc_ecma_transforms_compat::es2022::static_blocks(
static_block_mark
));
add!(swc_ecma_transforms_compat::es2022::static_blocks());
continue;
}
_ => {}
Expand Down
4 changes: 1 addition & 3 deletions crates/swc_ecma_transforms_typescript/tests/strip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,14 @@ fn tsxr(t: &Tester) -> impl Pass {
}

fn properties(_: &Tester, loose: bool) -> impl Pass {
let static_blocks_mark = Mark::new();
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();

(
resolver(unresolved_mark, top_level_mark, false),
static_blocks(static_blocks_mark),
static_blocks(),
class_properties(
class_properties::Config {
static_blocks_mark,
set_public_fields: loose,
..Default::default()
},
Expand Down

0 comments on commit 3cba25b

Please sign in to comment.