Add option discard_error: Error('foo') -> Error()

Add option discard_error:
Error('foo') -> Error()

Applies to built in error types Error, EvalError, InternalError,
RangeError, ReferenceError, SyntaxError, TypeError, URIError
This commit is contained in:
pborunda 2017-02-24 16:01:59 -07:00
parent 108b6cbf3a
commit 43ff5ed37f
3 changed files with 106 additions and 0 deletions

View File

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

View File

@ -78,6 +78,7 @@ function Compressor(options, false_by_default) {
negate_iife : !false_by_default, negate_iife : !false_by_default,
screw_ie8 : true, screw_ie8 : true,
drop_console : false, drop_console : false,
drop_error : false,
angular : false, angular : false,
warnings : true, warnings : true,
global_defs : {}, global_defs : {},
@ -2581,6 +2582,22 @@ merge(Compressor.prototype, {
return best_of(self, node); return best_of(self, node);
} }
} }
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("side_effects")) { if (compressor.option("side_effects")) {
if (self.expression instanceof AST_Function if (self.expression instanceof AST_Function
&& self.args.length == 0 && self.args.length == 0
@ -2618,6 +2635,12 @@ merge(Compressor.prototype, {
case "RegExp": case "RegExp":
case "Function": case "Function":
case "Error": case "Error":
case "EvalError":
case "InternalError":
case "RangeError":
case "ReferenceError":
case "SyntaxError":
case "URIError":
case "Array": case "Array":
return make_node(AST_Call, self, self).transform(compressor); 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();
}
}