implement annotations (#4763)

This commit is contained in:
Alex Lam S.L
2021-03-12 19:40:49 +00:00
committed by GitHub
parent c36c3cb470
commit 3b5d5014e0
15 changed files with 690 additions and 552 deletions

View File

@@ -48,6 +48,7 @@ function Compressor(options, false_by_default) {
return new Compressor(options, false_by_default);
TreeTransformer.call(this, this.before, this.after);
this.options = defaults(options, {
annotations : !false_by_default,
arguments : !false_by_default,
arrows : !false_by_default,
assignments : !false_by_default,
@@ -4725,7 +4726,7 @@ merge(Compressor.prototype, {
|| expr.expression.name == "Math" && expr.property == "random");
}
}
return this.pure || !compressor.pure_funcs(this);
return compressor.option("annotations") && this.pure || !compressor.pure_funcs(this);
});
AST_Node.DEFMETHOD("is_call_pure", return_false);
AST_Call.DEFMETHOD("is_call_pure", function(compressor) {

View File

@@ -47,14 +47,12 @@ function parse_source_map(content) {
}
function set_shorthand(name, options, keys) {
if (options[name]) {
keys.forEach(function(key) {
if (options[key]) {
if (typeof options[key] != "object") options[key] = {};
if (!(name in options[key])) options[key][name] = options[name];
}
});
}
keys.forEach(function(key) {
if (options[key]) {
if (typeof options[key] != "object") options[key] = {};
if (!(name in options[key])) options[key][name] = options[name];
}
});
}
function init_cache(cache) {
@@ -75,6 +73,7 @@ function to_json(cache) {
function minify(files, options) {
try {
options = defaults(options, {
annotations: undefined,
compress: {},
enclose: false,
ie8: false,
@@ -94,17 +93,14 @@ function minify(files, options) {
wrap: false,
}, true);
if (options.validate) AST_Node.enable_validation();
var timings = options.timings && {
start: Date.now()
};
if (options.rename === undefined) {
options.rename = options.compress && options.mangle;
}
set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
set_shorthand("toplevel", options, [ "compress", "mangle" ]);
set_shorthand("v8", options, [ "mangle", "output" ]);
set_shorthand("webkit", options, [ "mangle", "output" ]);
var timings = options.timings && { start: Date.now() };
if (options.rename === undefined) options.rename = options.compress && options.mangle;
if (options.annotations !== undefined) set_shorthand("annotations", options, [ "compress", "output" ]);
if (options.ie8) set_shorthand("ie8", options, [ "compress", "mangle", "output" ]);
if (options.keep_fnames) set_shorthand("keep_fnames", options, [ "compress", "mangle" ]);
if (options.toplevel) set_shorthand("toplevel", options, [ "compress", "mangle" ]);
if (options.v8) set_shorthand("v8", options, [ "mangle", "output" ]);
if (options.webkit) set_shorthand("webkit", options, [ "mangle", "output" ]);
var quoted_props;
if (options.mangle) {
options.mangle = defaults(options.mangle, {

View File

@@ -52,6 +52,7 @@ function OutputStream(options) {
var readonly = !options;
options = defaults(options, {
annotations : false,
ascii_only : false,
beautify : false,
braces : false,
@@ -500,12 +501,14 @@ function OutputStream(options) {
space();
}
}
var value = c.value.replace(/[@#]__PURE__/g, " ");
if (/^\s*$/.test(value)) return;
if (/comment[134]/.test(c.type)) {
print("//" + c.value.replace(/[@#]__PURE__/g, " ") + "\n");
print("//" + value + "\n");
indent();
last_nlb = true;
} else if (c.type == "comment2") {
print("/*" + c.value.replace(/[@#]__PURE__/g, " ") + "*/");
print("/*" + value + "*/");
last_nlb = false;
}
});
@@ -558,11 +561,13 @@ function OutputStream(options) {
} else if (i > 0 || !tail) {
space();
}
var value = c.value.replace(/[@#]__PURE__/g, " ");
if (/^\s*$/.test(value)) return;
if (/comment[134]/.test(c.type)) {
print("//" + c.value.replace(/[@#]__PURE__/g, " "));
print("//" + value);
need_newline_indented = true;
} else if (c.type == "comment2") {
print("/*" + c.value.replace(/[@#]__PURE__/g, " ") + "*/");
print("/*" + value + "*/");
need_space = true;
}
});
@@ -1436,6 +1441,17 @@ function OutputStream(options) {
});
/* -----[ other expressions ]----- */
function print_annotation(self, output) {
if (!output.option("annotations")) return;
if (!self.pure) return;
var level = 0, parent = self, node;
do {
node = parent;
parent = output.parent(level++);
if (parent instanceof AST_Call && parent.expression === node) return;
} while (parent instanceof AST_PropAccess && parent.expression === node);
output.print("/*" + self.pure + "*/");
}
function print_call_args(self, output) {
if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
output.add_mapping(self.start);
@@ -1448,11 +1464,14 @@ function OutputStream(options) {
});
}
DEFPRINT(AST_Call, function(output) {
this.expression.print(output);
print_call_args(this, output);
var self = this;
print_annotation(self, output);
self.expression.print(output);
print_call_args(self, output);
});
DEFPRINT(AST_New, function(output) {
var self = this;
print_annotation(self, output);
output.print("new");
output.space();
self.expression.print(output);

View File

@@ -1758,7 +1758,6 @@ function parse($TEXT, options) {
args : args,
end : prev()
});
mark_pure(call);
return subscripts(call, allow_calls);
};
@@ -1840,7 +1839,6 @@ function parse($TEXT, options) {
end.comments_after.length = 0;
end.comments_after = ex.end.comments_after;
ex.end = end;
if (ex instanceof AST_Call) mark_pure(ex);
if (is("punc", "=>")) return arrow(ex instanceof AST_Sequence ? ex.expressions : [ ex ], start);
return subscripts(ex, allow_calls);
case "[":
@@ -2217,19 +2215,6 @@ function parse($TEXT, options) {
});
}
function mark_pure(call) {
var start = call.start;
var comments = start.comments_before;
var i = HOP(start, "comments_before_length") ? start.comments_before_length : comments.length;
while (--i >= 0) {
var comment = comments[i];
if (/[@#]__PURE__/.test(comment.value)) {
call.pure = comment;
break;
}
}
}
function template(tag) {
var read = S.input.context().read_template;
var strings = [];
@@ -2277,7 +2262,6 @@ function parse($TEXT, options) {
args : expr_list(")", !options.strict),
end : prev()
});
mark_pure(call);
return subscripts(call, true);
}
if (is("punc", "`")) {
@@ -2286,6 +2270,18 @@ function parse($TEXT, options) {
tmpl.end = prev();
return subscripts(tmpl, allow_calls);
}
if (expr instanceof AST_Call && !expr.pure) {
var start = expr.start;
var comments = start.comments_before;
var i = HOP(start, "comments_before_length") ? start.comments_before_length : comments.length;
while (--i >= 0) {
var match = /[@#]__PURE__/.exec(comments[i].value);
if (match) {
expr.pure = match[0];
break;
}
}
}
return expr;
};