Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep fparens #1371

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bin/uglifyjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 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")
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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);

Expand Down
9 changes: 9 additions & 0 deletions lib/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function OutputStream(options) {
quote_style : 0,
keep_quoted_props: false,
wrap_iife : false,
keep_fparens : false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add trailing comma

}, true);

// Convert comment option to RegExp if neccessary and set up comments filter
Expand Down Expand Up @@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indent by 4

}
}

if (output.option('wrap_iife')) {
var p = output.parent();
return p instanceof AST_Call && p.expression === this;
Expand Down
25 changes: 25 additions & 0 deletions test/mocha/wrapping-parentheses.js
Original file line number Diff line number Diff line change
@@ -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}));";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use outer single quoted string to avoid need for escaping

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});";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use outer single quoted string to avoid need for escaping

var result = uglify.minify(originalCode, {
fromString: true
});
assert.strictEqual(result.code, expectedCode);
});
});