Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Preserve this in more cases #6226

Merged
merged 9 commits into from Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/swc_ecma_ast/Cargo.toml
Expand Up @@ -33,7 +33,7 @@ rkyv-bytecheck-impl = [
arbitrary = { version = "1", optional = true, features = ["derive"] }
bitflags = "1"
bytecheck = { version = "0.6.9", optional = true }
is-macro = "0.2.0"
is-macro = "0.2.1"
Copy link
Member Author

Choose a reason for hiding this comment

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

This is to fix #6137

num-bigint = { version = "0.4", features = ["serde"] }
rkyv = { package = "rkyv", version = "=0.7.37", optional = true }
# This is to avoid cargo version selection conflict between rkyv=0.7.37 and other versions, as it is strictly pinned
Expand Down
13 changes: 12 additions & 1 deletion crates/swc_ecma_ast/src/expr.rs
Expand Up @@ -8,7 +8,7 @@ use serde::{
Deserialize, Deserializer, Serialize,
};
use string_enum::StringEnum;
use swc_atoms::Atom;
use swc_atoms::{js_word, Atom};
use swc_common::{ast_node, util::take::Take, BytePos, EqIgnoreSpan, Span, Spanned, DUMMY_SP};

use crate::{
Expand Down Expand Up @@ -215,6 +215,17 @@ impl Expr {
}))
}
}

/// Returns true for `eval` and member expressions.
pub fn directness_maters(&self) -> bool {
matches!(
self,
Expr::Ident(Ident {
sym: js_word!("eval"),
..
}) | Expr::Member(..)
)
}
}

// Implement Clone without inline to avoid multiple copies of the
Expand Down
11 changes: 11 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/6175/1/input.js
@@ -0,0 +1,11 @@
let o = {
f() {
assert.ok(this !== o);
}
};
(1, o.f)``;
(true ? o.f : false)``;
(true && o.f)``;

let a;
(a = o.f)``;
@@ -0,0 +1,6 @@
let o = {
f () {
assert.ok(this !== o);
}
};
(0, o.f)``, (0, o.f)``, (0, o.f)``;
Expand Up @@ -483,7 +483,7 @@ impl SimplifyExpr {
self.changed = true;

// 0 && $right
*expr = *(left.take());
*expr = *left.take();
return;
}
} else if val {
Expand All @@ -500,7 +500,14 @@ impl SimplifyExpr {
if !left.may_have_side_effects(&self.expr_ctx) {
self.changed = true;

*expr = *node.take();
if node.directness_maters() {
*expr = Expr::Seq(SeqExpr {
span: node.span(),
exprs: vec![0.into(), node.take()],
});
} else {
*expr = *node.take();
}
} else {
self.changed = true;

Expand Down Expand Up @@ -1330,7 +1337,14 @@ impl VisitMut for SimplifyExpr {

let expr_value = if val { cons } else { alt };
*expr = if p.is_pure() {
*(expr_value.take())
if expr_value.directness_maters() {
Expr::Seq(SeqExpr {
span: *span,
exprs: vec![0.into(), expr_value.take()],
})
} else {
*expr_value.take()
}
} else {
Expr::Seq(SeqExpr {
span: *span,
Expand Down