From db73113599561eab969be7452bbec3d6056ffdd8 Mon Sep 17 00:00:00 2001 From: "a.gutnikov" Date: Wed, 9 Nov 2016 18:31:57 +0300 Subject: [PATCH 1/2] keep wrapping parens. for function extpressions --- bin/uglifyjs | 6 ++++++ lib/output.js | 9 +++++++++ 2 files changed, 15 insertions(+) diff --git a/bin/uglifyjs b/bin/uglifyjs index ce2e9411e00..c71c3149fe4 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -69,6 +69,7 @@ You need to pass an argument to this option to specify the name that your module .describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.") .describe("bare-returns", "Allow return outside of functions. Useful when minifying CommonJS modules.") .describe("keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.") + .describe("keep-fparens", "Do not drop parenthesises around function definitions. Useful for marking a function to be compiled eagerly by browser.") .describe("quotes", "Quote style (0 - auto, 1 - single, 2 - double, 3 - original)") .describe("reserved-file", "File containing reserved names") .describe("reserve-domprops", "Make (most?) DOM properties reserved for --mangle-props") @@ -132,6 +133,7 @@ You need to pass an argument to this option to specify the name that your module .boolean("noerr") .boolean("bare-returns") .boolean("keep-fnames") + .boolean("keep-fparens") .boolean("reserve-domprops") .boolean("wrap-iife") @@ -256,6 +258,10 @@ if (ARGS.wrap_iife) { OUTPUT_OPTIONS.wrap_iife = true; } +if (ARGS.keep_fparens) { + OUTPUT_OPTIONS.keep_fparens = true; +} + if (BEAUTIFY) UglifyJS.merge(OUTPUT_OPTIONS, BEAUTIFY); diff --git a/lib/output.js b/lib/output.js index 63a78c6075a..740ef843047 100644 --- a/lib/output.js +++ b/lib/output.js @@ -69,6 +69,7 @@ function OutputStream(options) { quote_style : 0, keep_quoted_props: false, wrap_iife : false, + keep_fparens : false }, true); // Convert comment option to RegExp if neccessary and set up comments filter @@ -558,6 +559,14 @@ function OutputStream(options) { return true; } + // If option is on then keep wrapping parenthesises + if (output.option('keep_fparens')) { + if (this.start && this.start instanceof AST_Token && this.start.value === '(' && + this.end && this.end instanceof AST_Token && this.end.value === ')') { + return true; + } + } + if (output.option('wrap_iife')) { var p = output.parent(); return p instanceof AST_Call && p.expression === this; From c0c42db0070a490d6d6184032e8bae048ae1c4c6 Mon Sep 17 00:00:00 2001 From: "a.gutnikov" Date: Wed, 9 Nov 2016 19:02:27 +0300 Subject: [PATCH 2/2] added tests --- bin/uglifyjs | 2 +- test/mocha/wrapping-parentheses.js | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 test/mocha/wrapping-parentheses.js diff --git a/bin/uglifyjs b/bin/uglifyjs index c71c3149fe4..0eeb0583946 100755 --- a/bin/uglifyjs +++ b/bin/uglifyjs @@ -69,7 +69,7 @@ You need to pass an argument to this option to specify the name that your module .describe("noerr", "Don't throw an error for unknown options in -c, -b or -m.") .describe("bare-returns", "Allow return outside of functions. Useful when minifying CommonJS modules.") .describe("keep-fnames", "Do not mangle/drop function names. Useful for code relying on Function.prototype.name.") - .describe("keep-fparens", "Do not drop parenthesises around function definitions. Useful for marking a function to be compiled eagerly by browser.") + .describe("keep-fparens", "Do not drop parentheses around function definitions. Useful for marking a function to be compiled eagerly by browser.") .describe("quotes", "Quote style (0 - auto, 1 - single, 2 - double, 3 - original)") .describe("reserved-file", "File containing reserved names") .describe("reserve-domprops", "Make (most?) DOM properties reserved for --mangle-props") diff --git a/test/mocha/wrapping-parentheses.js b/test/mocha/wrapping-parentheses.js new file mode 100644 index 00000000000..316d270496d --- /dev/null +++ b/test/mocha/wrapping-parentheses.js @@ -0,0 +1,25 @@ +var assert = require("assert"); +var uglify = require("../../"); + +describe("Keep wrapping parentheses", function() { + it("Should keep wrapping parentheses if keep-fparens option is turned on", function() { + var originalCode = "define(\"module\",(function() {module.exports = 42;}));"; + var expectedCode = "define(\"module\",(function(){module.exports=42}));"; + var result = uglify.minify(originalCode, { + output: { + keep_fparens: true + }, + fromString: true + }); + assert.strictEqual(result.code, expectedCode); + }); + + it("Should strip wrapping parentheses if keep-fparens option is turned off or not set", function() { + var originalCode = "define(\"module\",(function() {module.exports = 42;}));"; + var expectedCode = "define(\"module\",function(){module.exports=42});"; + var result = uglify.minify(originalCode, { + fromString: true + }); + assert.strictEqual(result.code, expectedCode); + }); +});