Skip to content

Commit

Permalink
fix(es/minifier): Preserve this of tagged template literals (#6165)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #6146
  • Loading branch information
kdy1 committed Oct 15, 2022
1 parent ba6d714 commit aec5cda
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
12 changes: 11 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/mod.rs
Expand Up @@ -2263,12 +2263,19 @@ where
.enumerate()
.identify_last()
.filter_map(|(last, (idx, expr))| {
#[cfg(feature = "debug")]
let _span =
tracing::span!(tracing::Level::ERROR, "seq_expr_with_children").entered();

expr.visit_mut_with(&mut *self.with_ctx(ctx));
let is_injected_zero = match &**expr {
Expr::Lit(Lit::Num(v)) => v.span.is_dummy(),
_ => false,
};

#[cfg(feature = "debug")]
let _span = tracing::span!(tracing::Level::ERROR, "seq_expr").entered();

let can_remove = !last
&& (idx != 0
|| !is_injected_zero
Expand Down Expand Up @@ -2580,7 +2587,10 @@ where
/// We don't optimize [Tpl] contained in [TaggedTpl].
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
fn visit_mut_tagged_tpl(&mut self, n: &mut TaggedTpl) {
n.tag.visit_mut_with(self);
n.tag.visit_mut_with(&mut *self.with_ctx(Ctx {
is_this_aware_callee: true,
..self.ctx
}));

n.tpl.exprs.visit_mut_with(self);
}
Expand Down
6 changes: 6 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/6146/input.js
@@ -0,0 +1,6 @@
let o = {
f() {
assert.ok(this !== o);
}
};
(1, o.f)``;
6 changes: 6 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/6146/output.js
@@ -0,0 +1,6 @@
let o = {
f () {
assert.ok(this !== o);
}
};
(0, o.f)``;
Expand Up @@ -9,7 +9,7 @@ import _JSXStyle from "styled-jsx/style";
top ? "column" : "column-reverse"
]
]
]), _JSXStyle.dynamic([
]), Sidebar, _JSXStyle.dynamic([
[
"4507deac72c40d6c",
[
Expand Down
@@ -1,3 +1,3 @@
console.log(({
y: ()=>String.raw
}).y()`\4321\u\x`), console.log(String.raw`\4321\u\x`);
}).y()`\4321\u\x`), console.log((0, String.raw)`\4321\u\x`);
16 changes: 15 additions & 1 deletion crates/swc_ecma_transforms_optimization/src/simplify/expr/mod.rs
Expand Up @@ -1504,7 +1504,9 @@ impl VisitMut for SimplifyExpr {
}
}

Expr::Lit(..) | Expr::Ident(..) if self.in_callee => {
Expr::Lit(..) | Expr::Ident(..)
if self.in_callee && !expr.may_have_side_effects(&self.expr_ctx) =>
{
if exprs.is_empty() {
self.changed = true;

Expand Down Expand Up @@ -1584,6 +1586,18 @@ impl VisitMut for SimplifyExpr {
self.is_modifying = old;
}

fn visit_mut_tagged_tpl(&mut self, n: &mut TaggedTpl) {
let old = self.in_callee;
self.in_callee = true;

n.tag.visit_mut_with(self);

self.in_callee = false;
n.tpl.visit_mut_with(self);

self.in_callee = old;
}

fn visit_mut_with_stmt(&mut self, n: &mut WithStmt) {
n.obj.visit_mut_with(self);
}
Expand Down

1 comment on commit aec5cda

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: aec5cda Previous: 0f38e38 Ratio
es/full/minify/libraries/antd 1796779296 ns/iter (± 27690436) 1914444605 ns/iter (± 107549417) 0.94
es/full/minify/libraries/d3 371398457 ns/iter (± 11683458) 430217137 ns/iter (± 14323961) 0.86
es/full/minify/libraries/echarts 1525416613 ns/iter (± 13829625) 1582893392 ns/iter (± 73005719) 0.96
es/full/minify/libraries/jquery 99955681 ns/iter (± 3165124) 108417826 ns/iter (± 7090934) 0.92
es/full/minify/libraries/lodash 109311782 ns/iter (± 3433897) 115140595 ns/iter (± 4076471) 0.95
es/full/minify/libraries/moment 57023419 ns/iter (± 1278245) 67383821 ns/iter (± 3553303) 0.85
es/full/minify/libraries/react 20284427 ns/iter (± 752484) 28080496 ns/iter (± 17570352) 0.72
es/full/minify/libraries/terser 290868163 ns/iter (± 7579934) 327031846 ns/iter (± 7704815) 0.89
es/full/minify/libraries/three 547673794 ns/iter (± 12193168) 568710094 ns/iter (± 16681015) 0.96
es/full/minify/libraries/typescript 3354599711 ns/iter (± 11333904) 3608956218 ns/iter (± 148304921) 0.93
es/full/minify/libraries/victory 834606891 ns/iter (± 17071017) 858509768 ns/iter (± 9164333) 0.97
es/full/minify/libraries/vue 161126865 ns/iter (± 6154641) 169582844 ns/iter (± 14947793) 0.95
es/full/codegen/es3 34658 ns/iter (± 3335) 35854 ns/iter (± 2544) 0.97
es/full/codegen/es5 34903 ns/iter (± 2088) 35456 ns/iter (± 3674) 0.98
es/full/codegen/es2015 35994 ns/iter (± 3098) 34893 ns/iter (± 1385) 1.03
es/full/codegen/es2016 34550 ns/iter (± 1766) 35192 ns/iter (± 1310) 0.98
es/full/codegen/es2017 35093 ns/iter (± 2447) 35378 ns/iter (± 2322) 0.99
es/full/codegen/es2018 34783 ns/iter (± 2754) 34661 ns/iter (± 841) 1.00
es/full/codegen/es2019 33709 ns/iter (± 4616) 34990 ns/iter (± 1626) 0.96
es/full/codegen/es2020 34207 ns/iter (± 2743) 34756 ns/iter (± 1223) 0.98
es/full/all/es3 197886393 ns/iter (± 10333074) 225981851 ns/iter (± 23718760) 0.88
es/full/all/es5 204744908 ns/iter (± 24610933) 220831314 ns/iter (± 27960088) 0.93
es/full/all/es2015 152371572 ns/iter (± 10512176) 193600112 ns/iter (± 22578234) 0.79
es/full/all/es2016 148635311 ns/iter (± 9940440) 177571546 ns/iter (± 14406823) 0.84
es/full/all/es2017 144970310 ns/iter (± 6518546) 176926235 ns/iter (± 20711515) 0.82
es/full/all/es2018 141979377 ns/iter (± 5151504) 175353587 ns/iter (± 20991811) 0.81
es/full/all/es2019 140785335 ns/iter (± 4042387) 172146189 ns/iter (± 17618816) 0.82
es/full/all/es2020 135544824 ns/iter (± 4708792) 162769040 ns/iter (± 10103792) 0.83
es/full/parser 725112 ns/iter (± 59993) 752268 ns/iter (± 48247) 0.96
es/full/base/fixer 26761 ns/iter (± 1917) 26986 ns/iter (± 1786) 0.99
es/full/base/resolver_and_hygiene 93787 ns/iter (± 5869) 95519 ns/iter (± 5639) 0.98
serialization of ast node 211 ns/iter (± 26) 208 ns/iter (± 8) 1.01
serialization of serde 218 ns/iter (± 34) 229 ns/iter (± 9) 0.95

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.