diff --git a/src/printer.js b/src/printer.js index ebc89aaf0..233a92921 100644 --- a/src/printer.js +++ b/src/printer.js @@ -1269,6 +1269,8 @@ function genericPrintNoParens(path, options, print, args) { n.expression.type === "CallExpression" || n.expression.type === "FunctionExpression" || n.expression.type === "JSXEmptyExpression" || + n.expression.type === "TemplateLiteral" || + n.expression.type === "TaggedTemplateExpression" || (parent.type === "JSXElement" && (n.expression.type === "ConditionalExpression" || isBinaryish(n.expression))); @@ -2912,8 +2914,23 @@ function printJSXElement(path, options, print) { delete n.closingElement; } - // If no children, just print the opening element const openingLines = path.call(print, "openingElement"); + const closingLines = path.call(print, "closingElement"); + + if ( + n.children.length === 1 && + n.children[0].type === "JSXExpressionContainer" && + (n.children[0].expression.type === "TemplateLiteral" || + n.children[0].expression.type === "TaggedTemplateExpression") + ) { + return concat([ + openingLines, + concat(path.map(print, "children")), + closingLines + ]); + } + + // If no children, just print the opening element if (n.openingElement.selfClosing) { assert.ok(!n.closingElement); return openingLines; @@ -3000,8 +3017,6 @@ function printJSXElement(path, options, print) { ) ]; - const closingLines = path.call(print, "closingElement"); - const multiLineElem = group( concat([ openingLines, diff --git a/tests/jsx_template/__snapshots__/jsfmt.spec.js.snap b/tests/jsx_template/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..f25403605 --- /dev/null +++ b/tests/jsx_template/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,42 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`styled-components.js 1`] = ` +"; + +; + +; +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +; + +; + +; +" +`; diff --git a/tests/jsx_template/jsfmt.spec.js b/tests/jsx_template/jsfmt.spec.js new file mode 100644 index 000000000..989047bcc --- /dev/null +++ b/tests/jsx_template/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(__dirname); diff --git a/tests/jsx_template/styled-components.js b/tests/jsx_template/styled-components.js new file mode 100644 index 000000000..3377c4078 --- /dev/null +++ b/tests/jsx_template/styled-components.js @@ -0,0 +1,18 @@ +; + +; + +;