diff --git a/src/doc-builders.js b/src/doc-builders.js index 9402f9da0..6c1ec5555 100644 --- a/src/doc-builders.js +++ b/src/doc-builders.js @@ -67,6 +67,7 @@ function lineSuffix(contents) { return { type: "line-suffix", contents }; } +const lineSuffixBoundary = { type: "line-suffix-boundary" }; const breakParent = { type: "break-parent" }; const line = { type: "line" }; const softline = { type: "line", soft: true }; @@ -100,6 +101,7 @@ module.exports = { group, conditionalGroup, lineSuffix, + lineSuffixBoundary, breakParent, ifBreak, indent diff --git a/src/doc-printer.js b/src/doc-printer.js index e41ba0941..82b493026 100644 --- a/src/doc-printer.js +++ b/src/doc-printer.js @@ -192,6 +192,11 @@ function printDocToString(doc, width, newLine) { case "line-suffix": lineSuffix.push([ind, mode, doc.contents]); break; + case "line-suffix-boundary": + if (lineSuffix.length > 0) { + cmds.push([ind, mode, { type: "line", hard: true }]); + } + break; case "line": switch (mode) { // fallthrough diff --git a/src/printer.js b/src/printer.js index ee76ce91f..b86ce359c 100644 --- a/src/printer.js +++ b/src/printer.js @@ -18,6 +18,7 @@ var indent = docBuilders.indent; var conditionalGroup = docBuilders.conditionalGroup; var ifBreak = docBuilders.ifBreak; var breakParent = docBuilders.breakParent; +var lineSuffixBoundary = docBuilders.lineSuffixBoundary; var docUtils = require("./doc-utils"); var willBreak = docUtils.willBreak; @@ -1230,7 +1231,7 @@ function genericPrintNoParens(path, options, print) { n.expression.type === "LogicalExpression"); if (shouldInline) { - return group(concat(["{", path.call(print, "expression"), "}"])); + return group(concat(["{", path.call(print, "expression"), lineSuffixBoundary, "}"])); } return group( @@ -1241,6 +1242,7 @@ function genericPrintNoParens(path, options, print) { concat([softline, path.call(print, "expression")]) ), softline, + lineSuffixBoundary, "}" ]) ); @@ -1387,7 +1389,11 @@ function genericPrintNoParens(path, options, print) { parts.push(print(childPath)); if (i < expressions.length) { - parts.push("${", removeLines(expressions[i]), "}"); + parts.push( + "${", + removeLines(expressions[i]), lineSuffixBoundary, + "}" + ); } }, "quasis" diff --git a/tests/line_suffix_boundary/__snapshots__/jsfmt.spec.js.snap b/tests/line_suffix_boundary/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..e4fc91e72 --- /dev/null +++ b/tests/line_suffix_boundary/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,103 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`boundary.js 1`] = ` +"\`\${ +a + // a + a +} + +\${a // comment +} + +\${b /* comment */} + +\${/* comment */ c /* comment */} + +\${// comment +d //comment +} + +\${// $FlowFixMe found when converting React.createClass to ES6 +ExampleStory.getFragment('story')} +\`; + +
+{ExampleStory.getFragment('story') // $FlowFixMe found when converting React.createClass to ES6 +} +
; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\`\${a + a // a +} + +\${/* comment*/ +a} + +\${/* comment */ b} + +\${/* comment */ /* comment */ c} + +\${/* comment*/ +/*comment*/ +d} + +\${/* $FlowFixMe found when converting React.createClass to ES6*/ +ExampleStory.getFragment(\\"story\\")} +\`; + +
+ {ExampleStory.getFragment(\\"story\\") // $FlowFixMe found when converting React.createClass to ES6 + } +
; +" +`; + +exports[`boundary.js 2`] = ` +"\`\${ +a + // a + a +} + +\${a // comment +} + +\${b /* comment */} + +\${/* comment */ c /* comment */} + +\${// comment +d //comment +} + +\${// $FlowFixMe found when converting React.createClass to ES6 +ExampleStory.getFragment('story')} +\`; + +
+{ExampleStory.getFragment('story') // $FlowFixMe found when converting React.createClass to ES6 +} +
; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +\`\${a + a // a +} + +\${/* comment*/ +a} + +\${/* comment */ b} + +\${/* comment */ /* comment */ c} + +\${/* comment*/ +/*comment*/ +d} + +\${/* $FlowFixMe found when converting React.createClass to ES6*/ +ExampleStory.getFragment(\\"story\\")} +\`; + +
+ {ExampleStory.getFragment(\\"story\\") // $FlowFixMe found when converting React.createClass to ES6 + } +
; +" +`; diff --git a/tests/line_suffix_boundary/boundary.js b/tests/line_suffix_boundary/boundary.js new file mode 100644 index 000000000..e108933d9 --- /dev/null +++ b/tests/line_suffix_boundary/boundary.js @@ -0,0 +1,24 @@ +`${ +a + // a + a +} + +${a // comment +} + +${b /* comment */} + +${/* comment */ c /* comment */} + +${// comment +d //comment +} + +${// $FlowFixMe found when converting React.createClass to ES6 +ExampleStory.getFragment('story')} +`; + +
+{ExampleStory.getFragment('story') // $FlowFixMe found when converting React.createClass to ES6 +} +
; diff --git a/tests/line_suffix_boundary/jsfmt.spec.js b/tests/line_suffix_boundary/jsfmt.spec.js new file mode 100644 index 000000000..d5ec770a9 --- /dev/null +++ b/tests/line_suffix_boundary/jsfmt.spec.js @@ -0,0 +1,2 @@ +run_spec(__dirname); +run_spec(__dirname, {parser: 'babylon'});