diff --git a/packages/babel-generator/README.md b/packages/babel-generator/README.md index 409dd0606f..5de763278c 100644 --- a/packages/babel-generator/README.md +++ b/packages/babel-generator/README.md @@ -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 diff --git a/packages/babel-generator/src/index.js b/packages/babel-generator/src/index.js index ad6fee5724..714f21d748 100644 --- a/packages/babel-generator/src/index.js +++ b/packages/babel-generator/src/index.js @@ -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, diff --git a/packages/babel-generator/src/printer.js b/packages/babel-generator/src/printer.js index fea799595f..9ce317e52b 100644 --- a/packages/babel-generator/src/printer.js +++ b/packages/babel-generator/src/printer.js @@ -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); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/argument/actual.js b/packages/babel-generator/test/fixtures/retainFunctionParens/argument/actual.js new file mode 100644 index 0000000000..0a7a235221 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/argument/actual.js @@ -0,0 +1 @@ +__d('x', (function () {})); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/argument/expected.js b/packages/babel-generator/test/fixtures/retainFunctionParens/argument/expected.js new file mode 100644 index 0000000000..0a7a235221 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/argument/expected.js @@ -0,0 +1 @@ +__d('x', (function () {})); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/assignment/actual.js b/packages/babel-generator/test/fixtures/retainFunctionParens/assignment/actual.js new file mode 100644 index 0000000000..28a92c35e0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/assignment/actual.js @@ -0,0 +1 @@ +var x = (function () {}); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/assignment/expected.js b/packages/babel-generator/test/fixtures/retainFunctionParens/assignment/expected.js new file mode 100644 index 0000000000..28a92c35e0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/assignment/expected.js @@ -0,0 +1 @@ +var x = (function () {}); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/negation/actual.js b/packages/babel-generator/test/fixtures/retainFunctionParens/negation/actual.js new file mode 100644 index 0000000000..a4314de87d --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/negation/actual.js @@ -0,0 +1 @@ +!function (){}() diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/negation/expected.js b/packages/babel-generator/test/fixtures/retainFunctionParens/negation/expected.js new file mode 100644 index 0000000000..5c4f16f7a2 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/negation/expected.js @@ -0,0 +1 @@ +!function () {}(); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/nested/actual.js b/packages/babel-generator/test/fixtures/retainFunctionParens/nested/actual.js new file mode 100644 index 0000000000..369e5e207e --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/nested/actual.js @@ -0,0 +1 @@ +var x = (((function () { }))); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/nested/expected.js b/packages/babel-generator/test/fixtures/retainFunctionParens/nested/expected.js new file mode 100644 index 0000000000..28a92c35e0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/nested/expected.js @@ -0,0 +1 @@ +var x = (function () {}); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/actual.js b/packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/actual.js new file mode 100644 index 0000000000..55f270aac0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/actual.js @@ -0,0 +1 @@ +callback(function () {}); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/expected.js b/packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/expected.js new file mode 100644 index 0000000000..55f270aac0 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/no-parens/expected.js @@ -0,0 +1 @@ +callback(function () {}); diff --git a/packages/babel-generator/test/fixtures/retainFunctionParens/options.json b/packages/babel-generator/test/fixtures/retainFunctionParens/options.json new file mode 100644 index 0000000000..29c6110939 --- /dev/null +++ b/packages/babel-generator/test/fixtures/retainFunctionParens/options.json @@ -0,0 +1,3 @@ +{ + "retainFunctionParens": true +}