improve AST tests & tools (#4873)

This commit is contained in:
Alex Lam S.L
2021-04-27 01:53:45 +01:00
committed by GitHub
parent acf951a5bc
commit 97bd56b7e8
7 changed files with 136 additions and 74 deletions

View File

@@ -204,27 +204,29 @@ function minify(files, options) {
if (options.mangle && options.mangle.properties) mangle_properties(toplevel, options.mangle.properties);
if (timings) timings.output = Date.now();
var result = {};
if (options.output.ast) {
result.ast = toplevel;
}
if (!HOP(options.output, "code") || options.output.code) {
var output = defaults(options.output, {
ast: false,
code: true,
});
if (output.ast) result.ast = toplevel;
if (output.code) {
if (options.sourceMap) {
options.output.source_map = SourceMap(options.sourceMap);
output.source_map = SourceMap(options.sourceMap);
if (options.sourceMap.includeSources) {
if (files instanceof AST_Toplevel) {
throw new Error("original source content unavailable");
} else for (var name in files) if (HOP(files, name)) {
options.output.source_map.setSourceContent(name, files[name]);
output.source_map.setSourceContent(name, files[name]);
}
}
}
delete options.output.ast;
delete options.output.code;
var stream = OutputStream(options.output);
delete output.ast;
delete output.code;
var stream = OutputStream(output);
toplevel.print(stream);
result.code = stream.get();
if (options.sourceMap) {
result.map = options.output.source_map.toString();
result.map = output.source_map.toString();
var url = options.sourceMap.url;
if (url) {
result.code = result.code.replace(/\n\/\/# sourceMappingURL=\S+\s*$/, "");

View File

@@ -220,7 +220,7 @@
start: my_start_token(M),
end: my_end_token(M),
key: key,
value: from_moz(M[M.shorthand ? "key" : "value"]),
value: from_moz(M.value),
};
if (M.kind == "init") return new (M.method ? AST_ObjectMethod : AST_ObjectKeyVal)(args);
args.value = new AST_Accessor(args.value);
@@ -395,6 +395,20 @@
path: M.source.value,
});
},
ImportExpression: function(M) {
var start = my_start_token(M);
var arg = from_moz(M.source);
return new AST_Call({
start: start,
end: my_end_token(M),
expression: new AST_SymbolRef({
start: start,
end: arg.start,
name: "import",
}),
args: [ arg ],
});
},
VariableDeclaration: function(M) {
return new ({
const: AST_Const,
@@ -462,7 +476,7 @@
} while (p.type == "ArrayPattern"
|| p.type == "AssignmentPattern" && p.left === FROM_MOZ_STACK[level + 1]
|| p.type == "ObjectPattern"
|| p.type == "Property" && p[p.shorthand ? "key" : "value"] === FROM_MOZ_STACK[level + 1]
|| p.type == "Property" && p.value === FROM_MOZ_STACK[level + 1]
|| p.type == "VariableDeclarator" && p.id === FROM_MOZ_STACK[level + 1]);
var ctor = AST_SymbolRef;
switch (p.type) {
@@ -731,18 +745,9 @@
});
def_to_moz(AST_ExportDefault, function To_Moz_ExportDefaultDeclaration(M) {
var decl = to_moz(M.body);
switch (decl.type) {
case "ClassExpression":
decl.type = "ClassDeclaration";
break;
case "FunctionExpression":
decl.type = "FunctionDeclaration";
break;
}
return {
type: "ExportDefaultDeclaration",
declaration: decl,
declaration: to_moz(M.body),
};
});
@@ -1158,11 +1163,15 @@
var FROM_MOZ_STACK = null;
function from_moz(node) {
FROM_MOZ_STACK.push(node);
var ret = node != null ? MOZ_TO_ME[node.type](node) : null;
function from_moz(moz) {
FROM_MOZ_STACK.push(moz);
var node = null;
if (moz) {
if (!HOP(MOZ_TO_ME, moz.type)) throw new Error("Unsupported type: " + moz.type);
node = MOZ_TO_ME[moz.type](moz);
}
FROM_MOZ_STACK.pop();
return ret;
return node;
}
AST_Node.from_mozilla_ast = function(node) {