-
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
Negated IIFE in if body produces superfluous ! #1288
Comments
Luckily the code produced still functions correctly. Seems simple enough to remedy. The IIFE |
I see what's going on. The IIFE
So any unary could be stripped only if it is on the right side of a |
To be more specific: some
|
At first I was thinking about ways to skip adding the ! at all, but soon came up with the same idea. In locations where the result of such an expression is not used, those prefix operators could be dropped. This has a some complications (e.g. which locations?) but is entirely tractable. |
Edit: patch was corrected. --- a/lib/compress.js
+++ b/lib/compress.js
@@ -2430,6 +2430,14 @@ merge(Compressor.prototype, {
break;
}
if (compressor.option("conditionals")) {
+ if (compressor.parent() instanceof AST_SimpleStatement
+ && (self.operator == "&&" || self.operator == "||")
+ && self.right instanceof AST_UnaryPrefix
+ && self.right.operator != "delete") {
+ // x && +y; ---> x && y;
+ // x || +y; ---> x || y;
+ self.right = self.right.expression;
+ }
if (self.operator == "&&") {
var ll = self.left.evaluate(compressor);
if (ll.length > 1) { Seems to work:
|
@rvanvelzen The fix probably belongs in If you want to put together a PR with the test cases above, go ahead. |
I missed the fact that operators "--" and "++" modify the expression. Corrected patch: --- a/lib/compress.js
+++ b/lib/compress.js
@@ -2355,6 +2355,7 @@ merge(Compressor.prototype, {
});
var commutativeOperators = makePredicate("== === != !== * & | ^");
+ var UNARY_PREFIX_WITHOUT_SIDE_EFFECTS_ON_EXPRESSION = makePredicate("! ~ - + void typeof");
OPT(AST_Binary, function(self, compressor){
function reverse(op, force) {
@@ -2430,6 +2431,14 @@ merge(Compressor.prototype, {
break;
}
if (compressor.option("conditionals")) {
+ if (compressor.parent() instanceof AST_SimpleStatement
+ && (self.operator == "&&" || self.operator == "||")
+ && self.right instanceof AST_UnaryPrefix
+ && UNARY_PREFIX_WITHOUT_SIDE_EFFECTS_ON_EXPRESSION(self.right.operator)) {
+ // x && +y; ---> x && y;
+ // x || +y; ---> x || y;
+ self.right = self.right.expression;
+ }
if (self.operator == "&&") {
var ll = self.left.evaluate(compressor);
if (ll.length > 1) { New test cases:
|
Strictly speaking, |
You're right of course, but I'll just point to the assumptions that Google Closure makes: https://github.com/google/closure-compiler/wiki/toString-and-valueOf https://github.com/google/closure-compiler/wiki/Compiler-Assumptions |
Just so there's no confusion, I'm not putting together a PR. |
Off topic... Regarding the |
- remove extra tree scanning phase for `negate_iife` - `negate_iife` now only deals with the narrowest form, i.e. IIFE sitting directly under `AST_SimpleStatement` - `booleans`, `conditionals` etc. will now take care the rest via more accurate accounting fixes mishoo#1288
- remove extra tree scanning phase for `negate_iife` - `negate_iife` now only deals with the narrowest form, i.e. IIFE sitting directly under `AST_SimpleStatement` - `booleans`, `conditionals` etc. will now take care the rest via more accurate accounting - `a(); void b();` => `a(); b();` fixes mishoo#1288
- remove extra tree scanning phase for `negate_iife` - `negate_iife` now only deals with the narrowest form, i.e. IIFE sitting directly under `AST_SimpleStatement` - `booleans`, `conditionals` etc. will now take care the rest via more accurate accounting - `a(); void b();` => `a(); b();` fixes mishoo#1288
- remove extra tree scanning phase for `negate_iife` - `negate_iife` now only deals with the narrowest form, i.e. IIFE sitting directly under `AST_SimpleStatement` - `booleans`, `conditionals` etc. will now take care the rest via more accurate accounting - `a(); void b();` => `a(); b();` fixes mishoo#1288
- remove extra tree scanning phase for `negate_iife` - `negate_iife` now only deals with the narrowest form, i.e. IIFE sitting directly under `AST_SimpleStatement` - `booleans`, `conditionals` etc. will now take care the rest via more accurate accounting - `a(); void b();` => `a(); b();` fixes mishoo#1288 closes mishoo#1451
Test case (that currently fails):
The text was updated successfully, but these errors were encountered: