fix(es/minifier): Preserve this of tagged template literals (#6165)

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/6146
This commit is contained in:
Donny/강동윤 2022-10-15 18:11:20 +09:00 committed by GitHub
parent ba6d714fe7
commit aec5cdacc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 4 deletions

View File

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

View File

@ -0,0 +1,6 @@
let o = {
f() {
assert.ok(this !== o);
}
};
(1, o.f)``;

View File

@ -0,0 +1,6 @@
let o = {
f () {
assert.ok(this !== o);
}
};
(0, o.f)``;

View File

@ -9,7 +9,7 @@ import _JSXStyle from "styled-jsx/style";
top ? "column" : "column-reverse"
]
]
]), _JSXStyle.dynamic([
]), Sidebar, _JSXStyle.dynamic([
[
"4507deac72c40d6c",
[

View File

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

View File

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