From 213ad1ed7acd325a84d01ab7813bf0b64b55e1f0 Mon Sep 17 00:00:00 2001 From: Sarup Banskota Date: Mon, 17 Jul 2017 23:30:28 +0700 Subject: [PATCH] [generator] remove parens from break & continue (#5950) * Remove parens around break and continue Fixes #5742 * Fix space in doc comment * Add some tests * Remove newlines within CommentBlock * Prevent newline before/after label * Remove reference to node * Check for label within startTerminatorless * Print block instead of single line comment * Clean up --- .../src/generators/statements.js | 4 +-- packages/babel-generator/src/printer.js | 33 +++++++++++++------ .../parentheses/break-statement/actual.js | 13 ++++++++ .../parentheses/break-statement/expected.js | 17 ++++++++++ 4 files changed, 55 insertions(+), 12 deletions(-) create mode 100644 packages/babel-generator/test/fixtures/parentheses/break-statement/actual.js create mode 100644 packages/babel-generator/test/fixtures/parentheses/break-statement/expected.js diff --git a/packages/babel-generator/src/generators/statements.js b/packages/babel-generator/src/generators/statements.js index 4929659874..c2d75a497b 100644 --- a/packages/babel-generator/src/generators/statements.js +++ b/packages/babel-generator/src/generators/statements.js @@ -123,8 +123,8 @@ function buildLabelStatement(prefix, key = "label") { const label = node[key]; if (label) { this.space(); - - const terminatorState = this.startTerminatorless(); + const isLabel = key == "label"; + const terminatorState = this.startTerminatorless(isLabel); this.print(label, node); this.endTerminatorless(terminatorState); } diff --git a/packages/babel-generator/src/printer.js b/packages/babel-generator/src/printer.js index 9065f24dad..27eadc19c6 100644 --- a/packages/babel-generator/src/printer.js +++ b/packages/babel-generator/src/printer.js @@ -43,6 +43,7 @@ export default class Printer { _insideAux: boolean = false; _printedCommentStarts: Object = {}; _parenPushNewlineState: ?Object = null; + _noLineTerminator: boolean = false; _printAuxAfterOnNextUserNode: boolean = false; _printedComments: WeakSet = new WeakSet(); _endsWithInteger = false; @@ -281,7 +282,7 @@ export default class Printer { * * This is to prevent breaking semantics for terminatorless separator nodes. eg: * - * return foo; + * return foo; * * returns `foo`. But if we do: * @@ -291,10 +292,15 @@ export default class Printer { * `undefined` will be returned and not `foo` due to the terminator. */ - startTerminatorless(): Object { - return (this._parenPushNewlineState = { - printed: false, - }); + startTerminatorless(isLabel: boolean = false): Object { + if (isLabel) { + this._noLineTerminator = true; + return null; + } else { + return (this._parenPushNewlineState = { + printed: false, + }); + } } /** @@ -302,7 +308,8 @@ export default class Printer { */ endTerminatorless(state: Object) { - if (state.printed) { + this._noLineTerminator = false; + if (state && state.printed) { this.dedent(); this.newline(); this.token(")"); @@ -530,13 +537,19 @@ export default class Printer { const isBlockComment = comment.type === "CommentBlock"; // Always add a newline before a block comment - this.newline(this._buf.hasContent() && isBlockComment ? 1 : 0); + this.newline( + this._buf.hasContent() && !this._noLineTerminator && isBlockComment + ? 1 + : 0, + ); if (!this.endsWith("[") && !this.endsWith("{")) this.space(); - let val = !isBlockComment ? `//${comment.value}\n` : `/*${comment.value}*/`; + let val = + !isBlockComment && !this._noLineTerminator + ? `//${comment.value}\n` + : `/*${comment.value}*/`; - // if (isBlockComment && this.format.indent.adjustMultilineComment) { const offset = comment.loc && comment.loc.start.column; if (offset) { @@ -559,7 +572,7 @@ export default class Printer { }); // Always add a newline after a block comment - this.newline(isBlockComment ? 1 : 0); + this.newline(isBlockComment && !this._noLineTerminator ? 1 : 0); } _printComments(comments?: Array) { diff --git a/packages/babel-generator/test/fixtures/parentheses/break-statement/actual.js b/packages/babel-generator/test/fixtures/parentheses/break-statement/actual.js new file mode 100644 index 0000000000..96a90f223d --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/break-statement/actual.js @@ -0,0 +1,13 @@ +label1: for (const a of [1, 2, 3]) { + break /*Block comment written in single line */ label1; +} + +label2: for (const a of [1, 2, 3]) { + break /*Block comment written + in multiple lines */ label2; +} + +label2: for (const a of [1, 2, 3]) { + break //foo + label2; +} diff --git a/packages/babel-generator/test/fixtures/parentheses/break-statement/expected.js b/packages/babel-generator/test/fixtures/parentheses/break-statement/expected.js new file mode 100644 index 0000000000..59c5be8053 --- /dev/null +++ b/packages/babel-generator/test/fixtures/parentheses/break-statement/expected.js @@ -0,0 +1,17 @@ +label1: for (const a of [1, 2, 3]) { + break /*Block comment written in single line */label1; +} + +label2: for (const a of [1, 2, 3]) { + break; + /*Block comment written + in multiple lines */ + + label2; +} + +label2: for (const a of [1, 2, 3]) { + break; //foo + + label2; +}