export missing API for AST manipulation (#3707)
authorAlex Lam S.L <alexlamsl@gmail.com>
Thu, 6 Feb 2020 18:46:25 +0000 (18:46 +0000)
committerGitHub <noreply@github.com>
Thu, 6 Feb 2020 18:46:25 +0000 (18:46 +0000)
lib/ast.js
lib/compress.js
lib/transform.js
lib/utils.js
test/exports.js
test/reduce.js
tools/exports.js

index f205235..99e2353 100644 (file)
@@ -351,7 +351,7 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
             filename: "wrap=" + JSON.stringify(name)
         }).transform(new TreeTransformer(function(node) {
             if (node instanceof AST_Directive && node.value == "$ORIG") {
-                return MAP.splice(body);
+                return List.splice(body);
             }
         }));
     },
@@ -370,7 +370,7 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
             filename: "enclose=" + JSON.stringify(args_values)
         }).transform(new TreeTransformer(function(node) {
             if (node instanceof AST_Directive && node.value == "$ORIG") {
-                return MAP.splice(body);
+                return List.splice(body);
             }
         }));
     }
index b190ef9..0fcd199 100644 (file)
@@ -1230,7 +1230,7 @@ merge(Compressor.prototype, {
                         var parent = multi_replacer.parent();
                         if (parent instanceof AST_Sequence && parent.tail_node() !== node) {
                             value_def.replaced++;
-                            return MAP.skip;
+                            return List.skip;
                         }
                         return get_rvalue(candidate);
                       case 1:
@@ -1863,7 +1863,7 @@ merge(Compressor.prototype, {
                         node.value = null;
                         return node;
                     }
-                    return in_list ? MAP.skip : null;
+                    return in_list ? List.skip : null;
                 }, patch_sequence));
             }
 
@@ -4017,7 +4017,7 @@ merge(Compressor.prototype, {
                         if (value) props.push(value);
                         switch (props.length) {
                           case 0:
-                            return MAP.skip;
+                            return List.skip;
                           case 1:
                             return maintain_this_binding(compressor, parent, node, props[0].transform(tt));
                           default:
@@ -4159,11 +4159,11 @@ merge(Compressor.prototype, {
                 }
                 switch (body.length) {
                   case 0:
-                    return in_list ? MAP.skip : make_node(AST_EmptyStatement, node);
+                    return in_list ? List.skip : make_node(AST_EmptyStatement, node);
                   case 1:
                     return body[0];
                   default:
-                    return in_list ? MAP.splice(body) : make_node(AST_BlockStatement, node, {
+                    return in_list ? List.splice(body) : make_node(AST_BlockStatement, node, {
                         body: body
                     });
                 }
@@ -4177,7 +4177,7 @@ merge(Compressor.prototype, {
                     var block = node.body;
                     node.body = block.body.pop();
                     block.body.push(node);
-                    return in_list ? MAP.splice(block.body) : block;
+                    return in_list ? List.splice(block.body) : block;
                 }
                 return node;
             }
@@ -4214,7 +4214,7 @@ merge(Compressor.prototype, {
                 } else if (is_empty(node.init)) {
                     node.init = null;
                 }
-                return !block ? node : in_list ? MAP.splice(block.body) : block;
+                return !block ? node : in_list ? List.splice(block.body) : block;
             } else if (node instanceof AST_ForIn) {
                 if (!drop_vars || !compressor.option("loops")) return;
                 if (!(node.init instanceof AST_Definitions)) return;
@@ -4229,7 +4229,7 @@ merge(Compressor.prototype, {
                         body: value
                     });
                 }
-                return in_list ? MAP.skip : make_node(AST_EmptyStatement, node);
+                return in_list ? List.skip : make_node(AST_EmptyStatement, node);
             } else if (node instanceof AST_Sequence) {
                 if (node.expressions.length == 1) return node.expressions[0];
             }
@@ -4610,7 +4610,7 @@ merge(Compressor.prototype, {
                     }));
                 });
                 defs_by_id[node.name.definition().id] = defs;
-                return MAP.splice(var_defs);
+                return List.splice(var_defs);
             }
 
             function make_sym(sym, key) {
index 5897aa7..e97cb74 100644 (file)
@@ -52,7 +52,7 @@ TreeTransformer.prototype = new TreeWalker;
 
 (function(DEF) {
     function do_list(list, tw) {
-        return MAP(list, function(node) {
+        return List(list, function(node) {
             return node.transform(tw, true);
         });
     }
index 6ac68bb..da82a14 100644 (file)
@@ -113,8 +113,8 @@ function return_true() { return true; }
 function return_this() { return this; }
 function return_null() { return null; }
 
-var MAP = (function() {
-    function MAP(a, f, backwards) {
+var List = (function() {
+    function List(a, f, backwards) {
         var ret = [], top = [], i;
         function doit() {
             var val = f(a[i], i);
@@ -149,14 +149,14 @@ var MAP = (function() {
         }
         return top.concat(ret);
     }
-    MAP.at_top = function(val) { return new AtTop(val) };
-    MAP.splice = function(val) { return new Splice(val) };
-    MAP.last = function(val) { return new Last(val) };
-    var skip = MAP.skip = {};
-    function AtTop(val) { this.v = val }
-    function Splice(val) { this.v = val }
-    function Last(val) { this.v = val }
-    return MAP;
+    List.at_top = function(val) { return new AtTop(val); };
+    List.splice = function(val) { return new Splice(val); };
+    List.last = function(val) { return new Last(val); };
+    var skip = List.skip = {};
+    function AtTop(val) { this.v = val; }
+    function Splice(val) { this.v = val; }
+    function Last(val) { this.v = val; }
+    return List;
 })();
 
 function push_uniq(array, el) {
index 793c9e3..1274ec6 100644 (file)
@@ -1,8 +1,8 @@
 exports["Compressor"] = Compressor;
 exports["defaults"] = defaults;
 exports["JS_Parse_Error"] = JS_Parse_Error;
+exports["List"] = List;
 exports["mangle_properties"] = mangle_properties;
-exports["MAP"] = MAP;
 exports["minify"] = minify;
 exports["OutputStream"] = OutputStream;
 exports["parse"] = parse;
index c4dc94b..6b5f911 100644 (file)
@@ -1,5 +1,5 @@
 var U = require("./node");
-var MAP = U.MAP;
+var List = U.List;
 var run_code = require("./sandbox").run_code;
 
 // Reduce a ufuzz-style `console.log` based test case by iteratively replacing
@@ -144,7 +144,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
                     if (node.body[0] instanceof U.AST_Break) {
                         if (node instanceof U.AST_Do) {
                             CHANGED = true;
-                            return MAP.skip;
+                            return List.skip;
                         }
                         expr = node.condition; // AST_While - fall through
                     }
@@ -269,7 +269,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
                     || parent instanceof U.AST_Switch && parent.expression != node) {
                     node.start._permute++;
                     CHANGED = true;
-                    return MAP.skip;
+                    return List.skip;
                 }
 
                 // replace or skip statement
@@ -290,7 +290,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
                     }
                     node.start._permute++;
                     CHANGED = true;
-                    return MAP.skip;
+                    return List.skip;
                 }
             }
 
@@ -300,7 +300,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
             });
             newNode.start._permute = ++node.start._permute;
             CHANGED = true;
-            return in_list ? MAP.skip : newNode;
+            return in_list ? List.skip : newNode;
         });
 
         for (var pass = 1; pass <= 3; ++pass) {
index c58f751..64a46e3 100644 (file)
@@ -1,4 +1,5 @@
 exports["Dictionary"] = Dictionary;
+exports["List"] = List;
 exports["minify"] = minify;
 exports["parse"] = parse;
 exports["push_uniq"] = push_uniq;