aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2021-03-02 07:27:55 +0000
committerGitHub <noreply@github.com>2021-03-02 15:27:55 +0800
commit74dee5c445b1424866ef78ac9b06c507ef92959c (patch)
tree3c0384836b7113620b937925472f48faccaea7da
parentee27d87a08480f1fb8c08fef5224bc88b3586c53 (diff)
downloadtracifyjs-74dee5c445b1424866ef78ac9b06c507ef92959c.tar.gz
tracifyjs-74dee5c445b1424866ef78ac9b06c507ef92959c.zip
enhance `reduce_vars` & `varify` (#4714)
-rw-r--r--lib/compress.js9
-rw-r--r--test/compress/let.js22
-rw-r--r--test/compress/varify.js28
3 files changed, 56 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 2d826697..393e3197 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -1259,10 +1259,13 @@ merge(Compressor.prototype, {
});
def(AST_VarDef, function(tw, descend, compressor) {
var node = this;
- if (!node.value) return;
- node.value.walk(tw);
+ if (node.value) {
+ node.value.walk(tw);
+ } else if (!(tw.parent() instanceof AST_Let)) {
+ return;
+ }
scan_declaration(tw, compressor, node.name, function() {
- return node.value;
+ return node.value || make_node(AST_Undefined, node);
}, function(name, fixed) {
var d = name.definition();
if (fixed && safe_to_assign(tw, d, true)) {
diff --git a/test/compress/let.js b/test/compress/let.js
index 04002e9a..53956c48 100644
--- a/test/compress/let.js
+++ b/test/compress/let.js
@@ -610,6 +610,28 @@ drop_unused: {
node_version: ">=4"
}
+default_init: {
+ options = {
+ evaluate: true,
+ reduce_vars: true,
+ sequences: true,
+ toplevel: true,
+ unused: true,
+ }
+ input: {
+ "use strict";
+ let a;
+ a = "PASS";
+ console.log(a);
+ }
+ expect: {
+ "use strict";
+ console.log("PASS");
+ }
+ expect_stdout: "PASS"
+ node_version: ">=4"
+}
+
issue_4191: {
options = {
functions: true,
diff --git a/test/compress/varify.js b/test/compress/varify.js
index 4d2a6b5a..ab212842 100644
--- a/test/compress/varify.js
+++ b/test/compress/varify.js
@@ -409,3 +409,31 @@ drop_forin_let: {
expect_stdout: "PASS"
node_version: ">=4"
}
+
+default_init: {
+ options = {
+ join_vars: true,
+ reduce_vars: true,
+ unused: true,
+ varify: true,
+ }
+ input: {
+ A = "PASS";
+ (function() {
+ "use strict";
+ let a;
+ a = A;
+ console.log(a);
+ })();
+ }
+ expect: {
+ A = "PASS";
+ (function() {
+ "use strict";
+ var a = A;
+ console.log(a);
+ })();
+ }
+ expect_stdout: "PASS"
+ node_version: ">=4"
+}