aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2017-10-01 03:01:50 +0800
committerGitHub <noreply@github.com>2017-10-01 03:01:50 +0800
commitdd71639264f201902e08d078fcef1946eaad2ef3 (patch)
tree0a36136a11b3e0239cffaff9cf80e969e8068388
parent2dcc552ce0404db3cdabb1dd02c8fff5c8dfc4f9 (diff)
downloadtracifyjs-dd71639264f201902e08d078fcef1946eaad2ef3.tar.gz
tracifyjs-dd71639264f201902e08d078fcef1946eaad2ef3.zip
enhance `reduce_vars` for `AST_Accessor` (#2339)
fixes #2336
-rw-r--r--lib/compress.js9
-rw-r--r--test/compress/reduce_vars.js29
2 files changed, 34 insertions, 4 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 9e516a89..d4a72d74 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -285,6 +285,10 @@ merge(Compressor.prototype, {
AST_Node.DEFMETHOD("reset_opt_flags", function(compressor, rescan) {
var reduce_vars = rescan && compressor.option("reduce_vars");
+ // Stack of look-up tables to keep track of whether a `SymbolDef` has been
+ // properly assigned before use:
+ // - `push()` & `pop()` when visiting conditional branches
+ // - backup & restore via `save_ids` when visiting out-of-order sections
var safe_ids = Object.create(null);
var suppressor = new TreeWalker(function(node) {
if (!(node instanceof AST_Symbol)) return;
@@ -391,10 +395,9 @@ merge(Compressor.prototype, {
return true;
}
if (node instanceof AST_Accessor) {
- var save_ids = safe_ids;
- safe_ids = Object.create(null);
+ push();
descend();
- safe_ids = save_ids;
+ pop();
return true;
}
if (node instanceof AST_Binary
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index 556bcad2..4e096d9d 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -2549,7 +2549,7 @@ issue_1922_2: {
expect_stdout: "1"
}
-accessor: {
+accessor_1: {
options = {
evaluate: true,
reduce_vars: true,
@@ -2578,6 +2578,33 @@ accessor: {
expect_stdout: "1 1"
}
+accessor_2: {
+ options = {
+ collapse_vars: true,
+ evaluate: true,
+ reduce_vars: true,
+ toplevel: true,
+ unused: true,
+ }
+ input: {
+ var A = 1;
+ var B = {
+ get c() {
+ console.log(A);
+ }
+ };
+ B.c;
+ }
+ expect: {
+ ({
+ get c() {
+ console.log(1);
+ }
+ }).c;
+ }
+ expect_stdout: "1"
+}
+
for_in_prop: {
options = {
reduce_vars: true,