diff --git a/lib/compress.js b/lib/compress.js index 73ab21f4..e2f2d6b4 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -87,6 +87,17 @@ function Compressor(options, false_by_default) { unused : !false_by_default, warnings : false, }, true); + var global_defs = this.options["global_defs"]; + if (typeof global_defs == "object") for (var key in global_defs) { + if (/^@/.test(key)) { + var ast = parse(global_defs[key]); + if (ast.body.length == 1 && ast.body[0] instanceof AST_SimpleStatement) { + global_defs[key.slice(1)] = ast.body[0].body; + } else throw new Error(string_template("Can't handle expression: {value}", { + value: global_defs[key] + })); + } + } var pure_funcs = this.options["pure_funcs"]; if (typeof pure_funcs == "function") { this.pure_funcs = pure_funcs; diff --git a/test/compress/global_defs.js b/test/compress/global_defs.js index bfd1d5f6..d784d335 100644 --- a/test/compress/global_defs.js +++ b/test/compress/global_defs.js @@ -160,3 +160,17 @@ issue_1801: { console.log(!0); } } + +issue_1986: { + options = { + global_defs: { + "@alert": "console.log", + }, + } + input: { + alert(42); + } + expect: { + console.log(42); + } +} diff --git a/test/mocha/minify.js b/test/mocha/minify.js index 7812fe6b..99771755 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -181,4 +181,19 @@ describe("minify", function() { assert.strictEqual(err.col, 12); }); }); + + describe("global_defs", function() { + it("should throw for non-trivial expressions", function() { + var result = Uglify.minify("alert(42);", { + compress: { + global_defs: { + "@alert": "debugger" + } + } + }); + var err = result.error; + assert.ok(err instanceof Error); + assert.strictEqual(err.stack.split(/\n/)[0], "Error: Can't handle expression: debugger"); + }); + }); });