From 1a60d7df9836df2ffc6a7a02ff4f90f3cccf599f Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Mon, 15 Nov 2021 15:38:49 -0800 Subject: [PATCH] [JS Printer] Fix bug with template literals --- integration/scripts/snippets.json | 3 +- integration/snippets/template-literal.js | 37 ++++++++++++++++++++++++ src/js_printer.zig | 12 ++------ 3 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 integration/snippets/template-literal.js diff --git a/integration/scripts/snippets.json b/integration/scripts/snippets.json index 233d508d85..a37394c1cc 100644 --- a/integration/scripts/snippets.json +++ b/integration/scripts/snippets.json @@ -22,5 +22,6 @@ "/latin1-chars-in-regexp.js", "/jsx-spacing.jsx", "/jsx-entities.jsx", - "/optional-chain-with-function.js" + "/optional-chain-with-function.js", + "/template-literal.js" ] diff --git a/integration/snippets/template-literal.js b/integration/snippets/template-literal.js new file mode 100644 index 0000000000..b65f1a6b1c --- /dev/null +++ b/integration/snippets/template-literal.js @@ -0,0 +1,37 @@ +const css = (templ) => templ.toString(); + +const fooNoBracesUTF8 = css` + before + /* */ + after +`; + +const fooNoBracesUT16 = css` + before + 🙃 + after +`; +const fooUTF8 = css` + before + ${true} + after + +`; + +const fooUTF16 = css` + before + 🙃 ${true} + after + +`; +export function test() { + for (let foo of [fooNoBracesUT16, fooNoBracesUTF8, fooUTF16, fooUTF8]) { + console.assert( + foo.includes("before"), + `Expected ${foo} to include "before"` + ); + console.assert(foo.includes("after"), `Expected ${foo} to include "after"`); + } + + return testDone(import.meta.url); +} diff --git a/src/js_printer.zig b/src/js_printer.zig index 06e309665e..2aa8007563 100644 --- a/src/js_printer.zig +++ b/src/js_printer.zig @@ -1473,11 +1473,7 @@ pub fn NewPrinter( p.print("`"); if (e.head.isPresent()) { - if (e.tag != null) { - p.print(e.head.utf8); - } else { - p.printStringContent(&e.head, '`'); - } + p.printStringContent(&e.head, '`'); } for (e.parts) |part| { @@ -1485,11 +1481,7 @@ pub fn NewPrinter( p.printExpr(part.value, .lowest, ExprFlag.None()); p.print("}"); if (part.tail.isPresent()) { - if (e.tag != null) { - p.print(part.tail.utf8); - } else { - p.printStringContent(&part.tail, '`'); - } + p.printStringContent(&part.tail, '`'); } } p.print("`");