diff --git a/src/babel/generation/index.js b/src/babel/generation/index.js index ed784c4019..09d74c4fe0 100644 --- a/src/babel/generation/index.js +++ b/src/babel/generation/index.js @@ -147,13 +147,22 @@ class CodeGenerator { return print; } - catchUp(node) { + catchUp(node, parent) { // catch up to this nodes newline if we're behind if (node.loc && this.format.retainLines && this.buffer.buf) { + var needsParens = false; + if (parent && (this.position.line < node.loc.start.line) && + (t.isContinueStatement(parent) || t.isBreakStatement(parent) || + t.isReturnStatement(parent) || t.isThrowStatement(parent))) { + needsParens = true; + this._push("("); + } while (this.position.line < node.loc.start.line) { this._push("\n"); } + return needsParens; } + return false; } print(node, parent, opts = {}) { @@ -207,7 +216,7 @@ class CodeGenerator { this.printLeadingComments(node, parent); - this.catchUp(node); + var needsParensFromCatchup = this.catchUp(node, parent); newline(true); @@ -220,7 +229,7 @@ class CodeGenerator { this.newline(); this.dedent(); } - if (needsParens) this.push(")"); + if (needsParens || needsParensFromCatchup) this.push(")"); this.map.mark(node, "end"); if (opts.after) opts.after(); diff --git a/test/core/fixtures/generation/edgecase/return-with-retainlines-option/actual.js b/test/core/fixtures/generation/edgecase/return-with-retainlines-option/actual.js new file mode 100644 index 0000000000..66e5d50920 --- /dev/null +++ b/test/core/fixtures/generation/edgecase/return-with-retainlines-option/actual.js @@ -0,0 +1,5 @@ +function foo(l) { + return ( + l + ); +} diff --git a/test/core/fixtures/generation/edgecase/return-with-retainlines-option/expected.js b/test/core/fixtures/generation/edgecase/return-with-retainlines-option/expected.js new file mode 100644 index 0000000000..7b9861c5aa --- /dev/null +++ b/test/core/fixtures/generation/edgecase/return-with-retainlines-option/expected.js @@ -0,0 +1,3 @@ +function foo(l) { + return ( + l);} diff --git a/test/core/fixtures/generation/edgecase/return-with-retainlines-option/options.json b/test/core/fixtures/generation/edgecase/return-with-retainlines-option/options.json new file mode 100644 index 0000000000..97925bbcb6 --- /dev/null +++ b/test/core/fixtures/generation/edgecase/return-with-retainlines-option/options.json @@ -0,0 +1,3 @@ +{ + "retainLines": true +}