Skip to content

Commit 631dd78

Browse files
authoredJan 13, 2023
feat(es/renamer): Support safari10 from the name mangler (#6801)
1 parent 75bf839 commit 631dd78

File tree

14 files changed

+130
-82
lines changed

14 files changed

+130
-82
lines changed
 

‎crates/swc/src/config/mod.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,10 @@ impl Options {
503503
.hygiene(if self.disable_hygiene {
504504
None
505505
} else {
506-
Some(hygiene::Config { keep_class_names })
506+
Some(hygiene::Config {
507+
keep_class_names,
508+
..Default::default()
509+
})
507510
})
508511
.fixer(!self.disable_fixer)
509512
.preset_env(cfg.env)

‎crates/swc_ecma_minifier/src/pass/mangle_names/mod.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ pub(crate) fn name_mangler(
2323
n: Default::default(),
2424
},
2525
self::private_name::private_name_mangler(options.keep_private_props, chars),
26-
renamer(Default::default(), ManglingRenamer { chars, preserved })
26+
renamer(
27+
swc_ecma_transforms_base::hygiene::Config {
28+
keep_class_names: options.keep_class_names,
29+
safari_10: options.safari10,
30+
},
31+
ManglingRenamer { chars, preserved }
32+
)
2733
)
2834
}
2935

‎crates/swc_ecma_minifier/tests/TODO.txt

-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ harmony/classes_extending_classes_out_of_pure_iifes/input.js
193193
harmony/default_assign/input.js
194194
harmony/expansion/input.js
195195
harmony/inline_arrow_using_arguments/input.js
196-
harmony/issue_1753_disable/input.js
197196
harmony/issue_2349b/input.js
198197
harmony/issue_2794_1/input.js
199198
harmony/issue_2794_2/input.js

‎crates/swc_ecma_minifier/tests/passing.txt

+2
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ harmony/import_statement/input.js
689689
harmony/import_statement_mangling/input.js
690690
harmony/issue_1613/input.js
691691
harmony/issue_1753/input.js
692+
harmony/issue_1753_disable/input.js
692693
harmony/issue_1898/input.js
693694
harmony/issue_2028/input.js
694695
harmony/issue_2345/input.js
@@ -766,6 +767,7 @@ identity/inline_identity_regression/input.js
766767
identity/inline_identity_undefined/input.js
767768
ie8/do_screw/input.js
768769
ie8/do_screw_constants/input.js
770+
ie8/do_screw_try_catch/input.js
769771
ie8/dont_screw/input.js
770772
ie8/dont_screw_constants/input.js
771773
ie8/issue_1586_2/input.js

‎crates/swc_ecma_minifier/tests/postponed.txt

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ hoist_props/toplevel_const/input.js
4646
hoist_props/toplevel_let/input.js
4747
hoist_props/toplevel_var/input.js
4848
hoist_props/undefined_key/input.js
49-
ie8/do_screw_try_catch/input.js
5049
ie8/do_screw_try_catch_undefined/input.js
5150
ie8/dont_screw_try_catch/input.js
5251
ie8/dont_screw_try_catch_undefined/input.js
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function x() {
22
(class Baz {
33
});
4-
class Foo {
5-
}
4+
let Foo = class Foo {
5+
};
66
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
function foo() {
2-
class Bar {}
2+
let Bar = class Bar {
3+
};
34
}

‎crates/swc_ecma_minifier/tests/terser/compress/try_catch/broken_safari_catch_scope/output.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ new class {
66
throw {
77
m: "PASS"
88
};
9-
} catch ({ m: A }) {
10-
console.log(A);
9+
} catch ({ m: B }) {
10+
console.log(B);
1111
}
1212
}
1313
}().f();
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"AAAAAAAA";
22
"BBBBBBB";
3-
new (class {
3+
new class {
44
f(A) {
55
try {
6-
throw { m: "PASS" };
7-
} catch ({ m: A }) {
8-
console.log(A);
6+
throw {
7+
m: "PASS"
8+
};
9+
} catch ({ m: B }) {
10+
console.log(B);
911
}
1012
}
11-
})().f();
13+
}().f();

‎crates/swc_ecma_transforms_base/src/hygiene/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ mod tests;
1212
pub struct Config {
1313
/// If true, the `hygiene` pass will preserve class names.
1414
pub keep_class_names: bool,
15+
16+
/// If true, the bug of safari 10 is avoided.
17+
pub safari_10: bool,
1518
}
1619

1720
/// See [hygiene_with_config] for doc. Creates a `hygiene` pass with default

‎crates/swc_ecma_transforms_base/src/hygiene/tests.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,7 @@ fn issue_1279() {
12471247
",
12481248
Config {
12491249
keep_class_names: true,
1250+
..Default::default()
12501251
},
12511252
);
12521253
}
@@ -1287,6 +1288,7 @@ fn issue_1507() {
12871288
",
12881289
Config {
12891290
keep_class_names: true,
1291+
..Default::default()
12901292
},
12911293
);
12921294
}

‎crates/swc_ecma_transforms_base/src/rename/analyzer/mod.rs

+95-68
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ pub(super) mod scope;
88

99
#[derive(Debug, Default)]
1010
pub(super) struct Analyzer {
11+
pub safari_10: bool,
12+
1113
pub is_pat_decl: bool,
1214
pub var_belong_to_fn_scope: bool,
1315
pub in_catch_params: bool,
@@ -42,6 +44,8 @@ impl Analyzer {
4244
{
4345
{
4446
let mut v = Analyzer {
47+
safari_10: self.safari_10,
48+
4549
scope: Scope {
4650
kind,
4751
..Default::default()
@@ -121,21 +125,56 @@ impl Visit for Analyzer {
121125
}
122126
}
123127

128+
fn visit_block_stmt(&mut self, n: &BlockStmt) {
129+
self.with_scope(ScopeKind::Block, |v| n.visit_children_with(v))
130+
}
131+
132+
fn visit_block_stmt_or_expr(&mut self, n: &BlockStmtOrExpr) {
133+
match n {
134+
// This avoid crating extra block scope for arrow function
135+
BlockStmtOrExpr::BlockStmt(n) => n.visit_children_with(self),
136+
BlockStmtOrExpr::Expr(n) => n.visit_with(self),
137+
}
138+
}
139+
124140
fn visit_catch_clause(&mut self, n: &CatchClause) {
125-
self.with_scope(ScopeKind::Block, |v| {
126-
let old = v.is_pat_decl;
127-
let old_in_catch_params = v.in_catch_params;
141+
if self.safari_10 {
142+
let old_is_pat_decl = self.is_pat_decl;
143+
let old_in_catch_params = self.in_catch_params;
128144

129-
v.is_pat_decl = false;
130-
n.body.visit_children_with(v);
145+
self.is_pat_decl = true;
146+
self.in_catch_params = true;
147+
n.param.visit_with(self);
131148

132-
v.is_pat_decl = true;
133-
v.in_catch_params = true;
134-
n.param.visit_with(v);
149+
self.in_catch_params = old_in_catch_params;
150+
self.is_pat_decl = old_is_pat_decl;
135151

136-
v.is_pat_decl = old;
137-
v.in_catch_params = old_in_catch_params;
138-
})
152+
self.with_scope(ScopeKind::Block, |v| {
153+
let old = v.is_pat_decl;
154+
let old_in_catch_params = v.in_catch_params;
155+
156+
v.is_pat_decl = false;
157+
n.body.visit_children_with(v);
158+
159+
v.is_pat_decl = old;
160+
v.in_catch_params = old_in_catch_params;
161+
})
162+
} else {
163+
self.with_scope(ScopeKind::Block, |v| {
164+
let old = v.is_pat_decl;
165+
let old_in_catch_params = v.in_catch_params;
166+
167+
v.is_pat_decl = false;
168+
n.body.visit_children_with(v);
169+
170+
v.is_pat_decl = true;
171+
v.in_catch_params = true;
172+
n.param.visit_with(v);
173+
174+
v.is_pat_decl = old;
175+
v.in_catch_params = old_in_catch_params;
176+
})
177+
}
139178
}
140179

141180
fn visit_class_decl(&mut self, c: &ClassDecl) {
@@ -251,6 +290,40 @@ impl Visit for Analyzer {
251290
}
252291
}
253292

293+
fn visit_for_in_stmt(&mut self, n: &ForInStmt) {
294+
self.with_scope(ScopeKind::Block, |v| {
295+
n.left.visit_with(v);
296+
n.right.visit_with(v);
297+
298+
v.with_scope(ScopeKind::Block, |v| {
299+
v.visit_for_body_within_same_scope(&n.body);
300+
})
301+
});
302+
}
303+
304+
fn visit_for_of_stmt(&mut self, n: &ForOfStmt) {
305+
self.with_scope(ScopeKind::Block, |v| {
306+
n.left.visit_with(v);
307+
n.right.visit_with(v);
308+
309+
v.with_scope(ScopeKind::Block, |v| {
310+
v.visit_for_body_within_same_scope(&n.body);
311+
})
312+
});
313+
}
314+
315+
fn visit_for_stmt(&mut self, n: &ForStmt) {
316+
self.with_scope(ScopeKind::Block, |v| {
317+
n.init.visit_with(v);
318+
n.test.visit_with(v);
319+
n.update.visit_with(v);
320+
321+
v.with_scope(ScopeKind::Block, |v| {
322+
v.visit_for_body_within_same_scope(&n.body);
323+
})
324+
});
325+
}
326+
254327
fn visit_import_default_specifier(&mut self, n: &ImportDefaultSpecifier) {
255328
self.add_decl(n.local.to_id(), true);
256329
}
@@ -326,77 +399,31 @@ impl Visit for Analyzer {
326399
}
327400
}
328401

402+
fn visit_static_block(&mut self, n: &StaticBlock) {
403+
self.with_fn_scope(|v| n.body.visit_children_with(v))
404+
}
405+
329406
fn visit_super_prop_expr(&mut self, e: &SuperPropExpr) {
330407
if let SuperProp::Computed(c) = &e.prop {
331408
c.visit_with(self);
332409
}
333410
}
334411

335-
fn visit_var_declarator(&mut self, v: &VarDeclarator) {
336-
let old = self.is_pat_decl;
337-
self.is_pat_decl = true;
338-
v.name.visit_with(self);
339-
340-
self.is_pat_decl = false;
341-
v.init.visit_with(self);
342-
343-
self.is_pat_decl = old;
344-
}
345-
346412
fn visit_var_decl(&mut self, n: &VarDecl) {
347413
let old_need_hoisted = self.var_belong_to_fn_scope;
348414
self.var_belong_to_fn_scope = n.kind == VarDeclKind::Var;
349415
n.visit_children_with(self);
350416
self.var_belong_to_fn_scope = old_need_hoisted;
351417
}
352418

353-
fn visit_block_stmt(&mut self, n: &BlockStmt) {
354-
self.with_scope(ScopeKind::Block, |v| n.visit_children_with(v))
355-
}
356-
357-
fn visit_block_stmt_or_expr(&mut self, n: &BlockStmtOrExpr) {
358-
match n {
359-
// This avoid crating extra block scope for arrow function
360-
BlockStmtOrExpr::BlockStmt(n) => n.visit_children_with(self),
361-
BlockStmtOrExpr::Expr(n) => n.visit_with(self),
362-
}
363-
}
364-
365-
fn visit_for_in_stmt(&mut self, n: &ForInStmt) {
366-
self.with_scope(ScopeKind::Block, |v| {
367-
n.left.visit_with(v);
368-
n.right.visit_with(v);
369-
370-
v.with_scope(ScopeKind::Block, |v| {
371-
v.visit_for_body_within_same_scope(&n.body);
372-
})
373-
});
374-
}
375-
376-
fn visit_for_of_stmt(&mut self, n: &ForOfStmt) {
377-
self.with_scope(ScopeKind::Block, |v| {
378-
n.left.visit_with(v);
379-
n.right.visit_with(v);
380-
381-
v.with_scope(ScopeKind::Block, |v| {
382-
v.visit_for_body_within_same_scope(&n.body);
383-
})
384-
});
385-
}
386-
387-
fn visit_for_stmt(&mut self, n: &ForStmt) {
388-
self.with_scope(ScopeKind::Block, |v| {
389-
n.init.visit_with(v);
390-
n.test.visit_with(v);
391-
n.update.visit_with(v);
419+
fn visit_var_declarator(&mut self, v: &VarDeclarator) {
420+
let old = self.is_pat_decl;
421+
self.is_pat_decl = true;
422+
v.name.visit_with(self);
392423

393-
v.with_scope(ScopeKind::Block, |v| {
394-
v.visit_for_body_within_same_scope(&n.body);
395-
})
396-
});
397-
}
424+
self.is_pat_decl = false;
425+
v.init.visit_with(self);
398426

399-
fn visit_static_block(&mut self, n: &StaticBlock) {
400-
self.with_fn_scope(|v| n.body.visit_children_with(v))
427+
self.is_pat_decl = old;
401428
}
402429
}

‎crates/swc_ecma_transforms_base/src/rename/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ where
113113
{
114114
let mut scope = {
115115
let mut v = Analyzer {
116+
safari_10: self.config.safari_10,
116117
..Default::default()
117118
};
118119
if skip_one {

‎crates/swc_ecma_transforms_base/src/resolver/tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ fn issue_1279_1() {
130130
",
131131
Config {
132132
keep_class_names: true,
133+
..Default::default()
133134
},
134135
);
135136
}
@@ -163,6 +164,7 @@ fn issue_1279_2() {
163164
",
164165
Config {
165166
keep_class_names: true,
167+
..Default::default()
166168
},
167169
);
168170
}
@@ -182,6 +184,7 @@ fn issue_2516() {
182184
",
183185
Config {
184186
keep_class_names: true,
187+
..Default::default()
185188
},
186189
);
187190
}

0 commit comments

Comments
 (0)
Please sign in to comment.