From 650e63c8aa65aa25b7b00dcd3fe49ff9d9e3c2fa Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 2 Dec 2022 04:14:07 +0200 Subject: [PATCH] improve usability (#5753) --- bin/uglifyjs | 1 + lib/parse.js | 6 +++++- lib/utils.js | 14 +++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/bin/uglifyjs b/bin/uglifyjs index a2a0257520f..345765a41ee 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -272,6 +272,7 @@ if (typeof options.sourceMap == "object" && "base" in options.sourceMap) { if (specified["self"]) { if (paths.length) UglifyJS.AST_Node.warn("Ignoring input files since --self was passed"); if (!options.wrap) options.wrap = "UglifyJS"; + if (!("toplevel" in options)) options.toplevel = false; paths = UglifyJS.FILES; } else if (paths.length) { paths = simple_glob(paths); diff --git a/lib/parse.js b/lib/parse.js index ffd6cd5b69b..258e86e651d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -207,7 +207,11 @@ function JS_Parse_Error(message, filename, line, col, pos) { this.line = line; this.col = col; this.pos = pos; - configure_error_stack(this, new SyntaxError(message, filename, line, col)); + try { + throw new SyntaxError(message, filename, line, col); + } catch (cause) { + configure_error_stack(this, cause); + } } JS_Parse_Error.prototype = Object.create(SyntaxError.prototype); JS_Parse_Error.prototype.constructor = JS_Parse_Error; diff --git a/lib/utils.js b/lib/utils.js index 22b069e1222..779be75ee12 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -65,8 +65,12 @@ function configure_error_stack(ex, cause) { var msg = "" + cause.message; cause = null; var index = stack.indexOf(msg); - if (index >= 0) index += msg.length; - index = stack.indexOf("\n", index) + 1; + if (index < 0) { + index = 0; + } else { + index += msg.length; + index = stack.indexOf("\n", index) + 1; + } stack = stack.slice(0, index) + stack.slice(stack.indexOf("\n", index) + 1); } return stack; @@ -77,7 +81,11 @@ function configure_error_stack(ex, cause) { function DefaultsError(msg, defs) { this.message = msg; this.defs = defs; - configure_error_stack(this, new Error(msg)); + try { + throw new Error(msg); + } catch (cause) { + configure_error_stack(this, cause); + } } DefaultsError.prototype = Object.create(Error.prototype); DefaultsError.prototype.constructor = DefaultsError;