aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-02-06 18:46:25 +0000
committerGitHub <noreply@github.com>2020-02-06 18:46:25 +0000
commit551420132c357467a747cae12be39c2662ce56f1 (patch)
treec3eac8ac64078004a2d2cbfba29fc33c648339ba
parentb0040ba6540232687204d125eff021adf11c4efa (diff)
downloadtracifyjs-551420132c357467a747cae12be39c2662ce56f1.tar.gz
tracifyjs-551420132c357467a747cae12be39c2662ce56f1.zip
export missing API for AST manipulation (#3707)
-rw-r--r--lib/ast.js4
-rw-r--r--lib/compress.js18
-rw-r--r--lib/transform.js2
-rw-r--r--lib/utils.js20
-rw-r--r--test/exports.js2
-rw-r--r--test/reduce.js10
-rw-r--r--tools/exports.js1
7 files changed, 29 insertions, 28 deletions
diff --git a/lib/ast.js b/lib/ast.js
index f2052350..99e2353e 100644
--- a/lib/ast.js
+++ b/lib/ast.js
@@ -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);
}
}));
}
diff --git a/lib/compress.js b/lib/compress.js
index b190ef9c..0fcd199e 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -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) {
diff --git a/lib/transform.js b/lib/transform.js
index 5897aa77..e97cb741 100644
--- a/lib/transform.js
+++ b/lib/transform.js
@@ -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);
});
}
diff --git a/lib/utils.js b/lib/utils.js
index 6ac68bbd..da82a140 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -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) {
diff --git a/test/exports.js b/test/exports.js
index 793c9e33..1274ec6e 100644
--- a/test/exports.js
+++ b/test/exports.js
@@ -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;
diff --git a/test/reduce.js b/test/reduce.js
index c4dc94bf..6b5f9114 100644
--- a/test/reduce.js
+++ b/test/reduce.js
@@ -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) {
diff --git a/tools/exports.js b/tools/exports.js
index c58f7514..64a46e30 100644
--- a/tools/exports.js
+++ b/tools/exports.js
@@ -1,4 +1,5 @@
exports["Dictionary"] = Dictionary;
+exports["List"] = List;
exports["minify"] = minify;
exports["parse"] = parse;
exports["push_uniq"] = push_uniq;