From 2cab34834191ea4fb48059e5725b631ff2752aa4 Mon Sep 17 00:00:00 2001 From: kzc Date: Sat, 13 Jan 2018 12:40:51 -0500 Subject: [PATCH 1/3] improve SymbolDef info in `--output ast` (#2778) * SymbolDef info (a.k.a. `thedef`) is now represented as a string containing `"ID name [mangled_name]"`. * Enhance display of `globals`, `variables`, `functions` and `enclosed`. * `SymbolDef.next_id` starts at `1` and the `id` is adjusted for `-o ast` display. --- bin/uglifyjs | 27 ++++++++++----------------- lib/scope.js | 2 +- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index 99248f1b..9a3257e7 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -227,28 +227,15 @@ function run() { result.ast.figure_out_scope({}); } print(JSON.stringify(result.ast, function(key, value) { - switch (key) { + if (value) switch (key) { case "thedef": - if (typeof value == "object" && typeof value.id == "number") { - return value.id; - } - return; + return symdef(value); case "enclosed": - return value.map(function(sym){ - return sym.id; - }); + return value.length ? value.map(symdef) : undefined; case "variables": case "functions": case "globals": - if (value && value.size()) { - var ret = {}; - value.each(function(val, key) { - // key/val inverted for readability. - ret[val.id] = key; - }); - return ret; - } - return; + return value.size() ? value.map(symdef) : undefined; } if (skip_key(key)) return; if (value instanceof UglifyJS.AST_Token) return; @@ -403,6 +390,12 @@ function skip_key(key) { return skip_keys.indexOf(key) >= 0; } +function symdef(def) { + var ret = (1e6 + def.id) + " " + def.name; + if (def.mangled_name) ret += " " + def.mangled_name; + return ret; +} + function format_object(obj) { var lines = []; var padding = ""; diff --git a/lib/scope.js b/lib/scope.js index 55b3ddbb..af852bb1 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -57,7 +57,7 @@ function SymbolDef(scope, orig, init) { this.id = SymbolDef.next_id++; }; -SymbolDef.next_id = 1e6; +SymbolDef.next_id = 1; SymbolDef.prototype = { unmangleable: function(options) { From 62a66dfff4fdd05b760d3e45cab4a2815a23d9ff Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 14 Jan 2018 17:11:31 +0800 Subject: [PATCH 2/3] fix & extend `join_vars` for object assigments (#2781) --- lib/compress.js | 21 ++++++- test/compress/properties.js | 110 ++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index b35e453c..321a1340 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1712,6 +1712,7 @@ merge(Compressor.prototype, { do { var node = exprs[0]; if (!(node instanceof AST_Assign)) break; + if (node.operator != "=") break; if (!(node.left instanceof AST_PropAccess)) break; var sym = node.left.expression; if (!(sym instanceof AST_SymbolRef)) break; @@ -1748,8 +1749,26 @@ merge(Compressor.prototype, { statements[++j] = stat; defs = stat; } + } else if (stat instanceof AST_Exit) { + var exprs = join_object_assignments(prev, stat.value); + if (exprs) { + CHANGED = true; + if (exprs.length) { + stat.value = make_sequence(stat.value, exprs); + } else if (stat.value instanceof AST_Sequence) { + stat.value = stat.value.tail_node().left; + } else { + stat.value = stat.value.left; + } + } + statements[++j] = stat; } else if (stat instanceof AST_For) { - if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) { + var exprs = join_object_assignments(prev, stat.init); + if (exprs) { + CHANGED = true; + stat.init = exprs.length ? make_sequence(stat.init, exprs) : null; + statements[++j] = stat; + } else if (prev instanceof AST_Var && (!stat.init || stat.init.TYPE == prev.TYPE)) { if (stat.init) { prev.definitions = prev.definitions.concat(stat.init.definitions); } diff --git a/test/compress/properties.js b/test/compress/properties.js index 6b368e64..7df53d39 100644 --- a/test/compress/properties.js +++ b/test/compress/properties.js @@ -1188,3 +1188,113 @@ join_object_assignments_3: { } expect_stdout: "PASS" } + +join_object_assignments_4: { + options = { + join_vars: true, + } + input: { + console.log(function() { + var o = { + p: 3 + }; + return o.q = "foo"; + }()); + } + expect: { + console.log(function() { + var o = { + p: 3, + q: "foo" + }; + return o.q; + }()); + } + expect_stdout: "foo" +} + +join_object_assignments_5: { + options = { + join_vars: true, + } + input: { + console.log(function() { + var o = { + p: 3 + }; + return o.q = /foo/, + o.r = "bar"; + }()); + } + expect: { + console.log(function() { + var o = { + p: 3, + q: /foo/, + r: "bar" + }; + return o.r; + }()); + } + expect_stdout: "bar" +} + +join_object_assignments_6: { + options = { + join_vars: true, + } + input: { + console.log(function() { + var o = { + p: 3 + }; + return o.q = "foo", + o.p += "", + console.log(o.q), + o.p; + }()); + } + expect: { + console.log(function() { + var o = { + p: 3, + q: "foo" + }; + return o.p += "", + console.log(o.q), + o.p; + }()); + } + expect_stdout: [ + "foo", + "3", + ] +} + +join_object_assignments_7: { + options = { + join_vars: true, + } + input: { + console.log(function() { + var o = { + p: 3 + }; + for (o.q = "foo"; console.log(o.q);); + return o.p; + }()); + } + expect: { + console.log(function() { + for (var o = { + p: 3, + q: "foo" + }; console.log(o.q);); + return o.p; + }()); + } + expect_stdout: [ + "foo", + "3", + ] +} From 7c0c92943f08e89118ed32019c014a44192f9dfc Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 14 Jan 2018 09:13:26 +0000 Subject: [PATCH 3/3] v3.3.7 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dffcf03e..aaf6370b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "homepage": "http://lisperator.net/uglifyjs", "author": "Mihai Bazon (http://lisperator.net/)", "license": "BSD-2-Clause", - "version": "3.3.6", + "version": "3.3.7", "engines": { "node": ">=0.8.0" },