This commit is contained in:
pborunda 2017-03-24 21:05:00 +00:00 committed by GitHub
commit b7eae12569
3 changed files with 124 additions and 0 deletions

View File

@ -428,9 +428,13 @@ to set `true`; it's effectively a shortcut for `foo=true`).
such as `console.info` and/or retain side effects from function arguments
after dropping the function call then use `pure_funcs` instead.
- `drop_error` -- default `false`. Pass `true` to discard description
of Error() calls.
- `expression` -- default `false`. Pass `true` to preserve completion values
from terminal statements without `return`, e.g. in bookmarklets.
- `keep_fargs` -- default `true`. Prevents the
compressor from discarding unused function arguments. You need this
for code which relies on `Function.length`.

View File

@ -79,6 +79,7 @@ function Compressor(options, false_by_default) {
negate_iife : !false_by_default,
screw_ie8 : true,
drop_console : false,
drop_error : false,
angular : false,
expression : false,
warnings : true,
@ -2854,6 +2855,22 @@ merge(Compressor.prototype, {
}
}
}
if (compressor.option("drop_error")) {
var exp = self.expression;
if (exp instanceof AST_SymbolRef
&& (exp.name == "Error" ||
exp.name == "EvalError" ||
exp.name == "InternalError" ||
exp.name == "RangeError" ||
exp.name == "ReferenceError" ||
exp.name == "SyntaxError" ||
exp.name == "TypeError" ||
exp.name == "URIError")
&& exp.undeclared()) {
// drop error description(s)
self.args = [];
}
}
if (exp instanceof AST_Function) {
if (exp.body[0] instanceof AST_Return) {
var value = exp.body[0].value;
@ -2891,6 +2908,23 @@ merge(Compressor.prototype, {
});
OPT(AST_New, function(self, compressor){
if (compressor.option("drop_error")) {
var exp = self.expression;
if (exp instanceof AST_SymbolRef
&& (exp.name == "Error" ||
exp.name == "EvalError" ||
exp.name == "InternalError" ||
exp.name == "RangeError" ||
exp.name == "ReferenceError" ||
exp.name == "SyntaxError" ||
exp.name == "TypeError" ||
exp.name == "URIError")
&& exp.undeclared()) {
// drop error description(s)
self.args = [];
}
}
if (compressor.option("unsafe")) {
var exp = self.expression;
if (exp instanceof AST_SymbolRef && exp.undeclared()) {
@ -2899,6 +2933,12 @@ merge(Compressor.prototype, {
case "RegExp":
case "Function":
case "Error":
case "EvalError":
case "InternalError":
case "RangeError":
case "ReferenceError":
case "SyntaxError":
case "URIError":
case "Array":
return make_node(AST_Call, self, self).transform(compressor);
}

View File

@ -0,0 +1,80 @@
drop_error_1: {
options = {};
input: {
Error('foo');
Error('foo', arguments);
EvalError('foo');
EvalError('foo', arguments);
InternalError('foo');
InternalError('foo', arguments);
RangeError('foo');
RangeError('foo', arguments);
ReferenceError('foo');
ReferenceError('foo', arguments);
SyntaxError('foo');
SyntaxError('foo', arguments);
TypeError('foo');
TypeError('foo', arguments);
URIError('foo');
URIError('foo', arguments);
}
expect: {
Error('foo');
Error('foo', arguments);
EvalError('foo');
EvalError('foo', arguments);
InternalError('foo');
InternalError('foo', arguments);
RangeError('foo');
RangeError('foo', arguments);
ReferenceError('foo');
ReferenceError('foo', arguments);
SyntaxError('foo');
SyntaxError('foo', arguments);
TypeError('foo');
TypeError('foo', arguments);
URIError('foo');
URIError('foo', arguments);
}
}
drop_error_2: {
options = { drop_error: true };
input: {
Error('foo');
Error('foo', arguments);
EvalError('foo');
EvalError('foo', arguments);
InternalError('foo');
InternalError('foo', arguments);
RangeError('foo');
RangeError('foo', arguments);
ReferenceError('foo');
ReferenceError('foo', arguments);
SyntaxError('foo');
SyntaxError('foo', arguments);
TypeError('foo');
TypeError('foo', arguments);
URIError('foo');
URIError('foo', arguments);
}
expect: {
Error();
Error();
EvalError();
EvalError();
InternalError();
InternalError();
RangeError();
RangeError();
ReferenceError();
ReferenceError();
SyntaxError();
SyntaxError();
TypeError();
TypeError();
URIError();
URIError();
}
}