From 43ff5ed37fd95c2f5f3c06634abd8afda7902ab0 Mon Sep 17 00:00:00 2001 From: pborunda Date: Fri, 24 Feb 2017 16:01:59 -0700 Subject: [PATCH] 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 --- README.md | 3 ++ lib/compress.js | 23 +++++++++++ test/compress/drop-error.js | 80 +++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+) create mode 100644 test/compress/drop-error.js diff --git a/README.md b/README.md index 490f178c..d1e9ca09 100644 --- a/README.md +++ b/README.md @@ -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 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 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 2bc1c5a5..9e07e724 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -78,6 +78,7 @@ function Compressor(options, false_by_default) { negate_iife : !false_by_default, screw_ie8 : true, drop_console : false, + drop_error : false, angular : false, warnings : true, global_defs : {}, @@ -2581,6 +2582,22 @@ merge(Compressor.prototype, { 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 (self.expression instanceof AST_Function && self.args.length == 0 @@ -2618,6 +2635,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(); + } +}