feat(es/renamer): Support safari10 from the name mangler (#6801)

This commit is contained in:
Donny/강동윤 2023-01-13 14:00:13 +09:00 committed by GitHub
parent 75bf8397ba
commit 631dd7872b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 130 additions and 82 deletions

View File

@ -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)

View File

@ -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 }
)
)
}

View File

@ -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

View File

@ -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
@ -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

View File

@ -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

View File

@ -1,6 +1,6 @@
function x() {
(class Baz {
});
class Foo {
}
let Foo = class Foo {
};
}

View File

@ -1,3 +1,4 @@
function foo() {
class Bar {}
let Bar = class Bar {
};
}

View File

@ -6,8 +6,8 @@ new class {
throw {
m: "PASS"
};
} catch ({ m: A }) {
console.log(A);
} catch ({ m: B }) {
console.log(B);
}
}
}().f();

View File

@ -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();

View File

@ -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

View File

@ -1247,6 +1247,7 @@ fn issue_1279() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}
@ -1287,6 +1288,7 @@ fn issue_1507() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}

View File

@ -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,
@ -42,6 +44,8 @@ impl Analyzer {
{
{
let mut v = Analyzer {
safari_10: self.safari_10,
scope: Scope {
kind,
..Default::default()
@ -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) {
@ -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);
}
@ -326,12 +399,23 @@ 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_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_var_declarator(&mut self, v: &VarDeclarator) {
let old = self.is_pat_decl;
self.is_pat_decl = true;
@ -342,61 +426,4 @@ impl Visit for Analyzer {
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);
v.with_scope(ScopeKind::Block, |v| {
v.visit_for_body_within_same_scope(&n.body);
})
});
}
fn visit_static_block(&mut self, n: &StaticBlock) {
self.with_fn_scope(|v| n.body.visit_children_with(v))
}
}

View File

@ -113,6 +113,7 @@ where
{
let mut scope = {
let mut v = Analyzer {
safari_10: self.config.safari_10,
..Default::default()
};
if skip_one {

View File

@ -130,6 +130,7 @@ fn issue_1279_1() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}
@ -163,6 +164,7 @@ fn issue_1279_2() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}
@ -182,6 +184,7 @@ fn issue_2516() {
",
Config {
keep_class_names: true,
..Default::default()
},
);
}