-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Bug in compress transformation of !typeof side_effect() #1289
Comments
Likely fix: --- a/lib/compress.js
+++ b/lib/compress.js
@@ -2310,10 +2310,12 @@ merge(Compressor.prototype, {
}
break;
case "typeof":
- // typeof always returns a non-empty string, thus it's
- // always true in booleans
- compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
- return make_node(AST_True, self);
+ if (!self.expression.has_side_effects(compressor)) {
+ // typeof always returns a non-empty string, thus it's
+ // always true in booleans
+ compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
+ return make_node(AST_True, self);
+ }
}
if (e instanceof AST_Binary && self.operator == "!") {
self = best_of(self, e.negate(compressor)); After patch applied:
It's not optimal, but not incorrect. |
Different solution: diff --git a/lib/compress.js b/lib/compress.js
index 8a08572..11ea86d 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2313,6 +2313,12 @@ merge(Compressor.prototype, {
// typeof always returns a non-empty string, thus it's
// always true in booleans
compressor.warn("Boolean expression always true [{file}:{line},{col}]", self.start);
+ if (self.expression.has_side_effects(compressor)) {
+ return make_node(AST_Seq, self, {
+ car: self.expression,
+ cdr: make_node(AST_True, self)
+ });
+ }
return make_node(AST_True, self);
}
if (e instanceof AST_Binary && self.operator == "!") {
These could probably be optimized further later, but at least the operator is dropped, which is something? |
Is it worth optimizing a typeof of an expression with a side effect in a boolean context? This is so rare in practise and is almost always incorrect code. Look at the cost/benefit ratio versus the risk of an incorrect assumption. |
Except for typos, I don't believe this case would ever appear in the wild. That having been said, I don't see any reason not to optimize this case. |
@rvanvelzen I don't think it's worth the complexity for this rare case, but please feel free to fix as you see fit. |
Incorrect optimizations:
The text was updated successfully, but these errors were encountered: