Add retainFunctionParens option (#4621)

* Add retainParens option

* Add doc, remove option

* Add negation test

* Make sure parens are not added

* Change name

* Change dir name
This commit is contained in:
Juriy Zaytsev
2016-09-30 18:05:33 -04:00
committed by Henry Zhu
parent 6f363b60bb
commit 1d728750c9
14 changed files with 21 additions and 0 deletions

View File

@@ -30,6 +30,7 @@ auxiliaryCommentBefore | string | | Optional string to add as
auxiliaryCommentAfter | string | | Optional string to add as a block comment at the end of the output file
shouldPrintComment | function | `opts.comments` | Function that takes a comment (as a string) and returns `true` if the comment should be included in the output. By default, comments are included if `opts.comments` is `true` or if `opts.minifed` is `false` and the comment contains `@preserve` or `@license`
retainLines | boolean | `false` | Attempt to use the same line numbers in the output code as in the source code (helps preserve stack traces)
retainFunctionParens | boolean | `false` | Retain parens around function expressions (could be used to change engine parsing behavior)
comments | boolean | `true` | Should comments be included in output
compact | boolean or `'auto'` | `opts.minified` | Set to `true` to avoid adding whitespace for formatting
minified | boolean | `false` | Should the output be minified

View File

@@ -53,6 +53,7 @@ function normalizeOptions(code, opts, tokens): Format {
auxiliaryCommentAfter: opts.auxiliaryCommentAfter,
shouldPrintComment: opts.shouldPrintComment,
retainLines: opts.retainLines,
retainFunctionParens: opts.retainFunctionParens,
comments: opts.comments == null || opts.comments,
compact: opts.compact,
minified: opts.minified,

View File

@@ -16,6 +16,7 @@ const NON_DECIMAL_LITERAL = /^0[box]/;
export type Format = {
shouldPrintComment: (comment: string) => boolean;
retainLines: boolean;
retainFunctionParens: boolean;
comments: boolean;
auxiliaryCommentBefore: string;
auxiliaryCommentAfter: string;
@@ -330,6 +331,11 @@ export default class Printer {
this._maybeAddAuxComment(this._insideAux && !oldInAux);
let needsParens = n.needsParens(node, parent, this._printStack);
if (this.format.retainFunctionParens &&
node.type === "FunctionExpression" &&
node.extra && node.extra.parenthesized) {
needsParens = true;
}
if (needsParens) this.token("(");
this._printLeadingComments(node, parent);

View File

@@ -0,0 +1 @@
__d('x', (function () {}));

View File

@@ -0,0 +1 @@
__d('x', (function () {}));

View File

@@ -0,0 +1 @@
var x = (function () {});

View File

@@ -0,0 +1 @@
var x = (function () {});

View File

@@ -0,0 +1 @@
!function (){}()

View File

@@ -0,0 +1 @@
!function () {}();

View File

@@ -0,0 +1 @@
var x = (((function () { })));

View File

@@ -0,0 +1 @@
var x = (function () {});

View File

@@ -0,0 +1 @@
callback(function () {});

View File

@@ -0,0 +1 @@
callback(function () {});

View File

@@ -0,0 +1,3 @@
{
"retainFunctionParens": true
}