Skip to content

Commit

Permalink
refactor(es/compat): Use special span instead of passing `static_bloc…
Browse files Browse the repository at this point in the history
…ks_mark` (#9725)

**Description:**

Let's make an assumption in the code: the use of dummy span as a private class field name is generated by us, it is there simply because it is necessary to maintain the order of side effect execution, its name is not referenced elsewhere, so we can safely remove its name.

This is a breaking change for Rust users.
  • Loading branch information
magic-akari authored Nov 14, 2024
1 parent 9f8c14f commit 6ad0735
Show file tree
Hide file tree
Showing 50 changed files with 638 additions and 905 deletions.
6 changes: 6 additions & 0 deletions .changeset/smart-cherries-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_common: patch
swc_ecma_compat_es2022: major
---

refactor(es/compat): Use dummy span instead of passing `static_blocks_mark`
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
20 changes: 20 additions & 0 deletions crates/swc_common/src/syntax_pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,18 @@ pub const DUMMY_SP: Span = Span {
hi: BytePos::DUMMY,
};

/// PURE span, will emit `/* #__PURE__ */` comment in codegen.
pub const PURE_SP: Span = Span {
lo: BytePos::PURE,
hi: BytePos::PURE,
};

/// Used for some special cases. e.g. mark the generated AST.
pub const PLACEHOLDER_SP: Span = Span {
lo: BytePos::PLACEHOLDER,
hi: BytePos::PLACEHOLDER,
};

pub struct Globals {
hygiene_data: Mutex<hygiene::HygieneData>,
#[allow(unused)]
Expand Down Expand Up @@ -408,6 +415,11 @@ impl Span {
self.lo.is_pure()
}

#[inline]
pub fn is_placeholder(self) -> bool {
self.lo.is_placeholder()
}

/// Returns `true` if this is a dummy span with any hygienic context.
#[inline]
pub fn is_dummy_ignoring_cmt(self) -> bool {
Expand Down Expand Up @@ -1032,6 +1044,10 @@ impl BytePos {
/// Dummy position. This is reserved for synthesized spans.
pub const DUMMY: Self = BytePos(0);
const MIN_RESERVED: Self = BytePos(DUMMY_RESERVE);
/// Placeholders, commonly used where names are required, but the names are
/// not referenced elsewhere.
pub const PLACEHOLDER: Self = BytePos(u32::MAX - 2);
/// Reserved for PURE comments. e.g. `/* #__PURE__ */`
pub const PURE: Self = BytePos(u32::MAX - 1);
/// Synthesized, but should be stored in a source map.
pub const SYNTHESIZED: Self = BytePos(u32::MAX);
Expand All @@ -1050,6 +1066,10 @@ impl BytePos {
self.0 == Self::PURE.0
}

pub const fn is_placeholder(self) -> bool {
self.0 == Self::PLACEHOLDER.0
}

/// Returns `true`` if this is explicitly synthesized or has relevant input
/// source so can have a comment.
pub const fn can_have_comment(self) -> bool {
Expand Down
18 changes: 2 additions & 16 deletions crates/swc_ecma_compat_es2022/src/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,13 @@ pub fn class_properties(config: Config, unresolved_mark: Mark) -> impl Pass {
})
}

#[derive(Debug, Clone, Copy)]
#[derive(Debug, Default, Clone, Copy)]
pub struct Config {
pub private_as_properties: bool,
pub set_public_fields: bool,
pub constant_super: bool,
pub no_document_all: bool,
pub pure_getter: bool,
pub static_blocks_mark: Mark,
}

impl Default for Config {
fn default() -> Self {
Self {
private_as_properties: false,
set_public_fields: false,
constant_super: false,
no_document_all: false,
pure_getter: false,
static_blocks_mark: Mark::new(),
}
}
}

struct ClassProperties {
Expand Down Expand Up @@ -734,7 +720,7 @@ 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) {
if prop.is_static && prop.key.span.is_placeholder() {
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
27 changes: 6 additions & 21 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, source_map::PLACEHOLDER_SP, util::take::Take};
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,
span: PLACEHOLDER_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
2 changes: 1 addition & 1 deletion crates/swc_ecma_transforms_proposal/src/decorator_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl DecoratorPass {
}
},
_ => {
let key_ident = private_ident!("_computedKey");
let key_ident = private_ident!(name.span(), "_computedKey");
self.extra_vars.push(VarDeclarator {
span: DUMMY_SP,
name: key_ident.clone().into(),
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
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,30 @@ class Foo {
});
}
}
var __ = {
writable: true,
value: { e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initProto] } = _apply_decs_2203_r(Foo, [
[
dec,
1,
"a",
function() {
return _class_private_field_get(this, ___a_1);
},
function(_v) {
_class_private_field_set(this, ___a_1, _v);
}
],
[
dec,
1,
"b",
function() {
return _class_private_field_get(this, ___b_2);
},
function(_v) {
_class_private_field_set(this, ___b_2, _v);
}
]
], [])
};
({ e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initProto] } = _apply_decs_2203_r(Foo, [
[
dec,
1,
"a",
function() {
return _class_private_field_get(this, ___a_1);
},
function(_v) {
_class_private_field_set(this, ___a_1, _v);
}
],
[
dec,
1,
"b",
function() {
return _class_private_field_get(this, ___b_2);
},
function(_v) {
_class_private_field_set(this, ___b_2, _v);
}
]
], []));
function get_a() {
return _get___a(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,20 @@ class Foo {
});
}
}
var __ = {
writable: true,
value: { e: [_init_a, _init_b, _init_computedKey, _initProto] } = _apply_decs_2203_r(Foo, [
[
dec,
1,
"a"
],
[
dec,
1,
"b"
],
[
dec,
1,
_computedKey
]
], [])
};
({ e: [_init_a, _init_b, _init_computedKey, _initProto] } = _apply_decs_2203_r(Foo, [
[
dec,
1,
"a"
],
[
dec,
1,
"b"
],
[
dec,
1,
_computedKey
]
], []));
Loading

0 comments on commit 6ad0735

Please sign in to comment.