export missing API for AST manipulation (#3707)

This commit is contained in:
Alex Lam S.L
2020-02-06 18:46:25 +00:00
committed by GitHub
parent b0040ba654
commit 551420132c
7 changed files with 29 additions and 28 deletions

View File

@@ -351,7 +351,7 @@ var AST_Toplevel = DEFNODE("Toplevel", "globals", {
filename: "wrap=" + JSON.stringify(name) filename: "wrap=" + JSON.stringify(name)
}).transform(new TreeTransformer(function(node) { }).transform(new TreeTransformer(function(node) {
if (node instanceof AST_Directive && node.value == "$ORIG") { 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) filename: "enclose=" + JSON.stringify(args_values)
}).transform(new TreeTransformer(function(node) { }).transform(new TreeTransformer(function(node) {
if (node instanceof AST_Directive && node.value == "$ORIG") { if (node instanceof AST_Directive && node.value == "$ORIG") {
return MAP.splice(body); return List.splice(body);
} }
})); }));
} }

View File

@@ -1230,7 +1230,7 @@ merge(Compressor.prototype, {
var parent = multi_replacer.parent(); var parent = multi_replacer.parent();
if (parent instanceof AST_Sequence && parent.tail_node() !== node) { if (parent instanceof AST_Sequence && parent.tail_node() !== node) {
value_def.replaced++; value_def.replaced++;
return MAP.skip; return List.skip;
} }
return get_rvalue(candidate); return get_rvalue(candidate);
case 1: case 1:
@@ -1863,7 +1863,7 @@ merge(Compressor.prototype, {
node.value = null; node.value = null;
return node; return node;
} }
return in_list ? MAP.skip : null; return in_list ? List.skip : null;
}, patch_sequence)); }, patch_sequence));
} }
@@ -4017,7 +4017,7 @@ merge(Compressor.prototype, {
if (value) props.push(value); if (value) props.push(value);
switch (props.length) { switch (props.length) {
case 0: case 0:
return MAP.skip; return List.skip;
case 1: case 1:
return maintain_this_binding(compressor, parent, node, props[0].transform(tt)); return maintain_this_binding(compressor, parent, node, props[0].transform(tt));
default: default:
@@ -4159,11 +4159,11 @@ merge(Compressor.prototype, {
} }
switch (body.length) { switch (body.length) {
case 0: case 0:
return in_list ? MAP.skip : make_node(AST_EmptyStatement, node); return in_list ? List.skip : make_node(AST_EmptyStatement, node);
case 1: case 1:
return body[0]; return body[0];
default: 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 body: body
}); });
} }
@@ -4177,7 +4177,7 @@ merge(Compressor.prototype, {
var block = node.body; var block = node.body;
node.body = block.body.pop(); node.body = block.body.pop();
block.body.push(node); block.body.push(node);
return in_list ? MAP.splice(block.body) : block; return in_list ? List.splice(block.body) : block;
} }
return node; return node;
} }
@@ -4214,7 +4214,7 @@ merge(Compressor.prototype, {
} else if (is_empty(node.init)) { } else if (is_empty(node.init)) {
node.init = null; 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) { } else if (node instanceof AST_ForIn) {
if (!drop_vars || !compressor.option("loops")) return; if (!drop_vars || !compressor.option("loops")) return;
if (!(node.init instanceof AST_Definitions)) return; if (!(node.init instanceof AST_Definitions)) return;
@@ -4229,7 +4229,7 @@ merge(Compressor.prototype, {
body: value 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) { } else if (node instanceof AST_Sequence) {
if (node.expressions.length == 1) return node.expressions[0]; if (node.expressions.length == 1) return node.expressions[0];
} }
@@ -4610,7 +4610,7 @@ merge(Compressor.prototype, {
})); }));
}); });
defs_by_id[node.name.definition().id] = defs; defs_by_id[node.name.definition().id] = defs;
return MAP.splice(var_defs); return List.splice(var_defs);
} }
function make_sym(sym, key) { function make_sym(sym, key) {

View File

@@ -52,7 +52,7 @@ TreeTransformer.prototype = new TreeWalker;
(function(DEF) { (function(DEF) {
function do_list(list, tw) { function do_list(list, tw) {
return MAP(list, function(node) { return List(list, function(node) {
return node.transform(tw, true); return node.transform(tw, true);
}); });
} }

View File

@@ -113,8 +113,8 @@ function return_true() { return true; }
function return_this() { return this; } function return_this() { return this; }
function return_null() { return null; } function return_null() { return null; }
var MAP = (function() { var List = (function() {
function MAP(a, f, backwards) { function List(a, f, backwards) {
var ret = [], top = [], i; var ret = [], top = [], i;
function doit() { function doit() {
var val = f(a[i], i); var val = f(a[i], i);
@@ -149,14 +149,14 @@ var MAP = (function() {
} }
return top.concat(ret); return top.concat(ret);
} }
MAP.at_top = function(val) { return new AtTop(val) }; List.at_top = function(val) { return new AtTop(val); };
MAP.splice = function(val) { return new Splice(val) }; List.splice = function(val) { return new Splice(val); };
MAP.last = function(val) { return new Last(val) }; List.last = function(val) { return new Last(val); };
var skip = MAP.skip = {}; var skip = List.skip = {};
function AtTop(val) { this.v = val } function AtTop(val) { this.v = val; }
function Splice(val) { this.v = val } function Splice(val) { this.v = val; }
function Last(val) { this.v = val } function Last(val) { this.v = val; }
return MAP; return List;
})(); })();
function push_uniq(array, el) { function push_uniq(array, el) {

View File

@@ -1,8 +1,8 @@
exports["Compressor"] = Compressor; exports["Compressor"] = Compressor;
exports["defaults"] = defaults; exports["defaults"] = defaults;
exports["JS_Parse_Error"] = JS_Parse_Error; exports["JS_Parse_Error"] = JS_Parse_Error;
exports["List"] = List;
exports["mangle_properties"] = mangle_properties; exports["mangle_properties"] = mangle_properties;
exports["MAP"] = MAP;
exports["minify"] = minify; exports["minify"] = minify;
exports["OutputStream"] = OutputStream; exports["OutputStream"] = OutputStream;
exports["parse"] = parse; exports["parse"] = parse;

View File

@@ -1,5 +1,5 @@
var U = require("./node"); var U = require("./node");
var MAP = U.MAP; var List = U.List;
var run_code = require("./sandbox").run_code; var run_code = require("./sandbox").run_code;
// Reduce a ufuzz-style `console.log` based test case by iteratively replacing // 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.body[0] instanceof U.AST_Break) {
if (node instanceof U.AST_Do) { if (node instanceof U.AST_Do) {
CHANGED = true; CHANGED = true;
return MAP.skip; return List.skip;
} }
expr = node.condition; // AST_While - fall through 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) { || parent instanceof U.AST_Switch && parent.expression != node) {
node.start._permute++; node.start._permute++;
CHANGED = true; CHANGED = true;
return MAP.skip; return List.skip;
} }
// replace or skip statement // replace or skip statement
@@ -290,7 +290,7 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
} }
node.start._permute++; node.start._permute++;
CHANGED = true; 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; newNode.start._permute = ++node.start._permute;
CHANGED = true; CHANGED = true;
return in_list ? MAP.skip : newNode; return in_list ? List.skip : newNode;
}); });
for (var pass = 1; pass <= 3; ++pass) { for (var pass = 1; pass <= 3; ++pass) {

View File

@@ -1,4 +1,5 @@
exports["Dictionary"] = Dictionary; exports["Dictionary"] = Dictionary;
exports["List"] = List;
exports["minify"] = minify; exports["minify"] = minify;
exports["parse"] = parse; exports["parse"] = parse;
exports["push_uniq"] = push_uniq; exports["push_uniq"] = push_uniq;