diff --git a/README.md b/README.md index 2399e23f..11ef0a4a 100644 --- a/README.md +++ b/README.md @@ -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`. diff --git a/lib/compress.js b/lib/compress.js index ab7cca6f..df0cbd77 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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); } diff --git a/test/compress/drop-error.js b/test/compress/drop-error.js new file mode 100644 index 00000000..8bcc5f9f --- /dev/null +++ b/test/compress/drop-error.js @@ -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(); + } +}