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:
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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);
|
||||
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/argument/actual.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/argument/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
__d('x', (function () {}));
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/argument/expected.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/argument/expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
__d('x', (function () {}));
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/assignment/actual.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/assignment/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var x = (function () {});
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/assignment/expected.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/assignment/expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var x = (function () {});
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/negation/actual.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/negation/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!function (){}()
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/negation/expected.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/negation/expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
!function () {}();
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/nested/actual.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/nested/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var x = (((function () { })));
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/nested/expected.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/nested/expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
var x = (function () {});
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/actual.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/actual.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
callback(function () {});
|
||||
1
packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/expected.js
vendored
Normal file
1
packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/expected.js
vendored
Normal file
@@ -0,0 +1 @@
|
||||
callback(function () {});
|
||||
3
packages/babel-generator/test/fixtures/retainFunctionParens/options.json
vendored
Normal file
3
packages/babel-generator/test/fixtures/retainFunctionParens/options.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"retainFunctionParens": true
|
||||
}
|
||||
Reference in New Issue
Block a user