Skip to content

Commit 70c5729

Browse files
committed
a small js minification improvement
1 parent ee8e0dd commit 70c5729

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

internal/js_parser/js_parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8117,7 +8117,7 @@ func (p *parser) mangleStmts(stmts []js_ast.Stmt, kind stmtsKind) []js_ast.Stmt
81178117
bodyLoc = body[0].Loc
81188118
}
81198119
return p.mangleIf(result, stmt.Loc, &js_ast.SIf{
8120-
Test: js_ast.Not(s.Test),
8120+
Test: js_ast.SimplifyBooleanExpr(js_ast.Not(s.Test)),
81218121
Yes: stmtsToSingleStmt(bodyLoc, body),
81228122
})
81238123
}

internal/js_parser/js_parser_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2987,7 +2987,7 @@ func TestMangleUndefined(t *testing.T) {
29872987
expectPrintedNormalAndMangle(t, "const x = undefined", "const x = void 0;\n", "const x = void 0;\n")
29882988
expectPrintedNormalAndMangle(t, "let x = undefined", "let x = void 0;\n", "let x;\n")
29892989
expectPrintedNormalAndMangle(t, "var x = undefined", "var x = void 0;\n", "var x = void 0;\n")
2990-
expectPrintedNormalAndMangle(t, "function foo(a) { if (!a) return undefined; a() }", "function foo(a) {\n if (!a)\n return void 0;\n a();\n}\n", "function foo(a) {\n !a || a();\n}\n")
2990+
expectPrintedNormalAndMangle(t, "function foo(a) { if (!a) return undefined; a() }", "function foo(a) {\n if (!a)\n return void 0;\n a();\n}\n", "function foo(a) {\n a && a();\n}\n")
29912991

29922992
// These should not be transformed
29932993
expectPrintedNormalAndMangle(t, "delete undefined", "delete undefined;\n", "delete undefined;\n")
@@ -3620,6 +3620,8 @@ func TestMangleReturn(t *testing.T) {
36203620
"function x() {\n y && z;\n}\n")
36213621
expectPrintedMangle(t, "function x() { if (y) { if (z) return; w(); } }",
36223622
"function x() {\n if (y) {\n if (z)\n return;\n w();\n }\n}\n")
3623+
expectPrintedMangle(t, "function foo(x) { if (!x.y) {} else return x }", "function foo(x) {\n if (x.y)\n return x;\n}\n")
3624+
expectPrintedMangle(t, "function foo(x) { if (!x.y) return undefined; return x }", "function foo(x) {\n if (x.y)\n return x;\n}\n")
36233625

36243626
// Do not optimize implicit return for statements that care about scope
36253627
expectPrintedMangle(t, "function x() { if (y) return; function y() {} }", "function x() {\n if (y)\n return;\n function y() {\n }\n}\n")

0 commit comments

Comments
 (0)