Skip to content

Commit

Permalink
feat(es/renamer): Support safari10 from the name mangler (#6801)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Jan 13, 2023
1 parent 75bf839 commit 631dd78
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 82 deletions.
5 changes: 4 additions & 1 deletion crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,10 @@ impl Options {
.hygiene(if self.disable_hygiene {
None
} else {
Some(hygiene::Config { keep_class_names })
Some(hygiene::Config {
keep_class_names,
..Default::default()
})
})
.fixer(!self.disable_fixer)
.preset_env(cfg.env)
Expand Down
8 changes: 7 additions & 1 deletion crates/swc_ecma_minifier/src/pass/mangle_names/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ pub(crate) fn name_mangler(
n: Default::default(),
},
self::private_name::private_name_mangler(options.keep_private_props, chars),
renamer(Default::default(), ManglingRenamer { chars, preserved })
renamer(
swc_ecma_transforms_base::hygiene::Config {
keep_class_names: options.keep_class_names,
safari_10: options.safari10,
},
ManglingRenamer { chars, preserved }
)
)
}

Expand Down
1 change: 0 additions & 1 deletion crates/swc_ecma_minifier/tests/TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ harmony/classes_extending_classes_out_of_pure_iifes/input.js
harmony/default_assign/input.js
harmony/expansion/input.js
harmony/inline_arrow_using_arguments/input.js
harmony/issue_1753_disable/input.js
harmony/issue_2349b/input.js
harmony/issue_2794_1/input.js
harmony/issue_2794_2/input.js
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_minifier/tests/passing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ harmony/import_statement/input.js
harmony/import_statement_mangling/input.js
harmony/issue_1613/input.js
harmony/issue_1753/input.js
harmony/issue_1753_disable/input.js
harmony/issue_1898/input.js
harmony/issue_2028/input.js
harmony/issue_2345/input.js
Expand Down Expand Up @@ -766,6 +767,7 @@ identity/inline_identity_regression/input.js
identity/inline_identity_undefined/input.js
ie8/do_screw/input.js
ie8/do_screw_constants/input.js
ie8/do_screw_try_catch/input.js
ie8/dont_screw/input.js
ie8/dont_screw_constants/input.js
ie8/issue_1586_2/input.js
Expand Down
1 change: 0 additions & 1 deletion crates/swc_ecma_minifier/tests/postponed.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ hoist_props/toplevel_const/input.js
hoist_props/toplevel_let/input.js
hoist_props/toplevel_var/input.js
hoist_props/undefined_key/input.js
ie8/do_screw_try_catch/input.js
ie8/do_screw_try_catch_undefined/input.js
ie8/dont_screw_try_catch/input.js
ie8/dont_screw_try_catch_undefined/input.js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function x() {
(class Baz {
});
class Foo {
}
let Foo = class Foo {
};
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
function foo() {
class Bar {}
let Bar = class Bar {
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ new class {
throw {
m: "PASS"
};
} catch ({ m: A }) {
console.log(A);
} catch ({ m: B }) {
console.log(B);
}
}
}().f();
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"AAAAAAAA";
"BBBBBBB";
new (class {
new class {
f(A) {
try {
throw { m: "PASS" };
} catch ({ m: A }) {
console.log(A);
throw {
m: "PASS"
};
} catch ({ m: B }) {
console.log(B);
}
}
})().f();
}().f();
3 changes: 3 additions & 0 deletions crates/swc_ecma_transforms_base/src/hygiene/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ mod tests;
pub struct Config {
/// If true, the `hygiene` pass will preserve class names.
pub keep_class_names: bool,

/// If true, the bug of safari 10 is avoided.
pub safari_10: bool,
}

/// See [hygiene_with_config] for doc. Creates a `hygiene` pass with default
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_transforms_base/src/hygiene/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1247,6 +1247,7 @@ fn issue_1279() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}
Expand Down Expand Up @@ -1287,6 +1288,7 @@ fn issue_1507() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}
Expand Down
163 changes: 95 additions & 68 deletions crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub(super) mod scope;

#[derive(Debug, Default)]
pub(super) struct Analyzer {
pub safari_10: bool,

pub is_pat_decl: bool,
pub var_belong_to_fn_scope: bool,
pub in_catch_params: bool,
Expand Down Expand Up @@ -42,6 +44,8 @@ impl Analyzer {
{
{
let mut v = Analyzer {
safari_10: self.safari_10,

scope: Scope {
kind,
..Default::default()
Expand Down Expand Up @@ -121,21 +125,56 @@ impl Visit for Analyzer {
}
}

fn visit_block_stmt(&mut self, n: &BlockStmt) {
self.with_scope(ScopeKind::Block, |v| n.visit_children_with(v))
}

fn visit_block_stmt_or_expr(&mut self, n: &BlockStmtOrExpr) {
match n {
// This avoid crating extra block scope for arrow function
BlockStmtOrExpr::BlockStmt(n) => n.visit_children_with(self),
BlockStmtOrExpr::Expr(n) => n.visit_with(self),
}
}

fn visit_catch_clause(&mut self, n: &CatchClause) {
self.with_scope(ScopeKind::Block, |v| {
let old = v.is_pat_decl;
let old_in_catch_params = v.in_catch_params;
if self.safari_10 {
let old_is_pat_decl = self.is_pat_decl;
let old_in_catch_params = self.in_catch_params;

v.is_pat_decl = false;
n.body.visit_children_with(v);
self.is_pat_decl = true;
self.in_catch_params = true;
n.param.visit_with(self);

v.is_pat_decl = true;
v.in_catch_params = true;
n.param.visit_with(v);
self.in_catch_params = old_in_catch_params;
self.is_pat_decl = old_is_pat_decl;

v.is_pat_decl = old;
v.in_catch_params = old_in_catch_params;
})
self.with_scope(ScopeKind::Block, |v| {
let old = v.is_pat_decl;
let old_in_catch_params = v.in_catch_params;

v.is_pat_decl = false;
n.body.visit_children_with(v);

v.is_pat_decl = old;
v.in_catch_params = old_in_catch_params;
})
} else {
self.with_scope(ScopeKind::Block, |v| {
let old = v.is_pat_decl;
let old_in_catch_params = v.in_catch_params;

v.is_pat_decl = false;
n.body.visit_children_with(v);

v.is_pat_decl = true;
v.in_catch_params = true;
n.param.visit_with(v);

v.is_pat_decl = old;
v.in_catch_params = old_in_catch_params;
})
}
}

fn visit_class_decl(&mut self, c: &ClassDecl) {
Expand Down Expand Up @@ -251,6 +290,40 @@ impl Visit for Analyzer {
}
}

fn visit_for_in_stmt(&mut self, n: &ForInStmt) {
self.with_scope(ScopeKind::Block, |v| {
n.left.visit_with(v);
n.right.visit_with(v);

v.with_scope(ScopeKind::Block, |v| {
v.visit_for_body_within_same_scope(&n.body);
})
});
}

fn visit_for_of_stmt(&mut self, n: &ForOfStmt) {
self.with_scope(ScopeKind::Block, |v| {
n.left.visit_with(v);
n.right.visit_with(v);

v.with_scope(ScopeKind::Block, |v| {
v.visit_for_body_within_same_scope(&n.body);
})
});
}

fn visit_for_stmt(&mut self, n: &ForStmt) {
self.with_scope(ScopeKind::Block, |v| {
n.init.visit_with(v);
n.test.visit_with(v);
n.update.visit_with(v);

v.with_scope(ScopeKind::Block, |v| {
v.visit_for_body_within_same_scope(&n.body);
})
});
}

fn visit_import_default_specifier(&mut self, n: &ImportDefaultSpecifier) {
self.add_decl(n.local.to_id(), true);
}
Expand Down Expand Up @@ -326,77 +399,31 @@ impl Visit for Analyzer {
}
}

fn visit_static_block(&mut self, n: &StaticBlock) {
self.with_fn_scope(|v| n.body.visit_children_with(v))
}

fn visit_super_prop_expr(&mut self, e: &SuperPropExpr) {
if let SuperProp::Computed(c) = &e.prop {
c.visit_with(self);
}
}

fn visit_var_declarator(&mut self, v: &VarDeclarator) {
let old = self.is_pat_decl;
self.is_pat_decl = true;
v.name.visit_with(self);

self.is_pat_decl = false;
v.init.visit_with(self);

self.is_pat_decl = old;
}

fn visit_var_decl(&mut self, n: &VarDecl) {
let old_need_hoisted = self.var_belong_to_fn_scope;
self.var_belong_to_fn_scope = n.kind == VarDeclKind::Var;
n.visit_children_with(self);
self.var_belong_to_fn_scope = old_need_hoisted;
}

fn visit_block_stmt(&mut self, n: &BlockStmt) {
self.with_scope(ScopeKind::Block, |v| n.visit_children_with(v))
}

fn visit_block_stmt_or_expr(&mut self, n: &BlockStmtOrExpr) {
match n {
// This avoid crating extra block scope for arrow function
BlockStmtOrExpr::BlockStmt(n) => n.visit_children_with(self),
BlockStmtOrExpr::Expr(n) => n.visit_with(self),
}
}

fn visit_for_in_stmt(&mut self, n: &ForInStmt) {
self.with_scope(ScopeKind::Block, |v| {
n.left.visit_with(v);
n.right.visit_with(v);

v.with_scope(ScopeKind::Block, |v| {
v.visit_for_body_within_same_scope(&n.body);
})
});
}

fn visit_for_of_stmt(&mut self, n: &ForOfStmt) {
self.with_scope(ScopeKind::Block, |v| {
n.left.visit_with(v);
n.right.visit_with(v);

v.with_scope(ScopeKind::Block, |v| {
v.visit_for_body_within_same_scope(&n.body);
})
});
}

fn visit_for_stmt(&mut self, n: &ForStmt) {
self.with_scope(ScopeKind::Block, |v| {
n.init.visit_with(v);
n.test.visit_with(v);
n.update.visit_with(v);
fn visit_var_declarator(&mut self, v: &VarDeclarator) {
let old = self.is_pat_decl;
self.is_pat_decl = true;
v.name.visit_with(self);

v.with_scope(ScopeKind::Block, |v| {
v.visit_for_body_within_same_scope(&n.body);
})
});
}
self.is_pat_decl = false;
v.init.visit_with(self);

fn visit_static_block(&mut self, n: &StaticBlock) {
self.with_fn_scope(|v| n.body.visit_children_with(v))
self.is_pat_decl = old;
}
}
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_base/src/rename/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ where
{
let mut scope = {
let mut v = Analyzer {
safari_10: self.config.safari_10,
..Default::default()
};
if skip_one {
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_ecma_transforms_base/src/resolver/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ fn issue_1279_1() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}
Expand Down Expand Up @@ -163,6 +164,7 @@ fn issue_1279_2() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}
Expand All @@ -182,6 +184,7 @@ fn issue_2516() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}

0 comments on commit 631dd78

Please sign in to comment.