From 38a46c86d7163002ad079eb9dd690e18b5c9da8f Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 18 Sep 2020 14:35:29 +0100 Subject: [PATCH] enhance `side_effects` (#4124) - add documentation for `merge_vars` --- README.md | 2 ++ lib/compress.js | 3 ++- test/compress/side_effects.js | 25 +++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 964faf46..6c549768 100644 --- a/README.md +++ b/README.md @@ -688,6 +688,8 @@ to be `false` and all symbol names will be omitted. - `loops` (default: `true`) -- optimizations for `do`, `while` and `for` loops when we can statically determine the condition. +- `merge_vars` (default: `true`) -- combine and reuse variables. + - `negate_iife` (default: `true`) -- negate "Immediately-Called Function Expressions" where the return value is discarded, to avoid the parens that the code generator would insert. diff --git a/lib/compress.js b/lib/compress.js index 61d6079c..cfd3e2b2 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3892,7 +3892,8 @@ merge(Compressor.prototype, { if (is_undeclared_ref(expr) && global_pure_fns[expr.name]) return true; if (expr instanceof AST_Dot && is_undeclared_ref(expr.expression)) { var static_fn = static_fns[expr.expression.name]; - return static_fn && static_fn[expr.property]; + return static_fn && (static_fn[expr.property] + || expr.expression.name == "Math" && expr.property == "random"); } } return this.pure || !compressor.pure_funcs(this); diff --git a/test/compress/side_effects.js b/test/compress/side_effects.js index 244e3881..0746a56d 100644 --- a/test/compress/side_effects.js +++ b/test/compress/side_effects.js @@ -245,6 +245,31 @@ unsafe_builtin_2: { expect_stdout: "object PASS PASS" } +unsafe_builtin_3: { + options = { + conditionals: true, + side_effects: true, + toplevel: true, + unsafe: true, + } + input: { + var o = {}; + if (42 < Math.random()) + o.p = "FAIL"; + else + o.p = "PASS"; + for (var k in o) + console.log(k, o[k]); + } + expect: { + var o = {}; + o.p = 42 < Math.random() ? "FAIL" : "PASS"; + for (var k in o) + console.log(k, o[k]); + } + expect_stdout: "p PASS" +} + unsafe_string_replace: { options = { side_effects: true, -- 2.34.1