From 741abb73d28300f07d8d9ae8b4594ccb6ca37cf7 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Thu, 14 May 2015 23:45:45 -0700 Subject: [PATCH 1/2] Add failing test for printing with compact & retainLines --- .../return-with-retainlines-and-compact-option/actual.js | 6 ++++++ .../return-with-retainlines-and-compact-option/expected.js | 4 ++++ .../return-with-retainlines-and-compact-option/options.json | 4 ++++ 3 files changed, 14 insertions(+) create mode 100644 test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/actual.js create mode 100644 test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/expected.js create mode 100644 test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/options.json diff --git a/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/actual.js b/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/actual.js new file mode 100644 index 0000000000..2a0e91f010 --- /dev/null +++ b/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/actual.js @@ -0,0 +1,6 @@ +function foo(l) { + return ( + // hi + l + ); +} diff --git a/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/expected.js b/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/expected.js new file mode 100644 index 0000000000..042f71356d --- /dev/null +++ b/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/expected.js @@ -0,0 +1,4 @@ +function foo(l){ +return ( + +l);} diff --git a/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/options.json b/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/options.json new file mode 100644 index 0000000000..87138b0d7e --- /dev/null +++ b/test/core/fixtures/generation/edgecase/return-with-retainlines-and-compact-option/options.json @@ -0,0 +1,4 @@ +{ + "retainLines": true, + "compact": true +} From 2916d1262b0a76b0562518c232a16a576092e857 Mon Sep 17 00:00:00 2001 From: Amjad Masad Date: Thu, 14 May 2015 23:57:35 -0700 Subject: [PATCH 2/2] Don't print leftParen if already printed before the catchup --- src/babel/generation/index.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/babel/generation/index.js b/src/babel/generation/index.js index 11d8fdc458..49faa50453 100644 --- a/src/babel/generation/index.js +++ b/src/babel/generation/index.js @@ -149,11 +149,12 @@ class CodeGenerator { return print; } - catchUp(node, parent) { + catchUp(node, parent, leftParenPrinted) { // 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.isTerminatorless(parent)) { + if (!leftParenPrinted && parent && + this.position.line < node.loc.start.line && t.isTerminatorless(parent)) { needsParens = true; this._push("("); } @@ -216,7 +217,7 @@ class CodeGenerator { this.printLeadingComments(node, parent); - var needsParensFromCatchup = this.catchUp(node, parent); + var needsParensFromCatchup = this.catchUp(node, parent, needsParens); newline(true);