From d97ff917bd7d8b5b2fb37193dd44dcfa5fd72843 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Wed, 7 Aug 2024 11:38:37 +0300 Subject: [PATCH] support `ascii_only` in template strings closes #5910 --- lib/output.js | 4 +-- test/compress/templates.js | 64 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/lib/output.js b/lib/output.js index 859ec28c2e..2982cc3f60 100644 --- a/lib/output.js +++ b/lib/output.js @@ -1788,12 +1788,12 @@ function OutputStream(options) { if (self.tag) self.tag.print(output); output.print("`"); for (var i = 0; i < self.expressions.length; i++) { - output.print(self.strings[i]); + output.print(output.to_utf8(self.strings[i])); output.print("${"); self.expressions[i].print(output); output.print("}"); } - output.print(self.strings[i]); + output.print(output.to_utf8(self.strings[i])); output.print("`"); }); DEFPRINT(AST_BigInt, function(output) { diff --git a/test/compress/templates.js b/test/compress/templates.js index 4658f0d882..604f9dedf5 100644 --- a/test/compress/templates.js +++ b/test/compress/templates.js @@ -429,6 +429,70 @@ pure_funcs: { node_version: ">=4" } +ascii_only: { + beautify = { + ascii_only: true, + } + options = { + templates: false, + } + input: { + console.log(`\ud801\udc37\ud801𐐷${42}\u{10437}`); + } + expect_exact: "console.log(`\\ud801\\udc37\\ud801\\ud801\\udc37${42}\\u{10437}`);" + expect_stdout: "𐐷\ud801𐐷42𐐷" + // non-BMP support is platform-dependent on Node.js v4 + node_version: ">=6" +} + +ascii_only_templates: { + beautify = { + ascii_only: true, + } + options = { + templates: true, + } + input: { + console.log(`\ud801\udc37\ud801𐐷${42}\u{10437}`); + } + expect_exact: "console.log(`\\ud801\\udc37\\ud801\\ud801\\udc37${42}\\ud801\\udc37`);" + expect_stdout: "𐐷\ud801𐐷42𐐷" + // non-BMP support is platform-dependent on Node.js v4 + node_version: ">=6" +} + +unicode: { + beautify = { + ascii_only: false, + } + options = { + templates: false, + } + input: { + console.log(`\ud801\udc37\ud801𐐷${42}\u{10437}`); + } + expect_exact: "console.log(`\\ud801\\udc37\\ud801𐐷${42}\\u{10437}`);" + expect_stdout: "𐐷\ud801𐐷42𐐷" + // non-BMP support is platform-dependent on Node.js v4 + node_version: ">=6" +} + +unicode_templates: { + beautify = { + ascii_only: false, + } + options = { + templates: true, + } + input: { + console.log(`\ud801\udc37\ud801𐐷${42}\u{10437}`); + } + expect_exact: "console.log(`𐐷\\ud801𐐷${42}𐐷`);" + expect_stdout: "𐐷\ud801𐐷42𐐷" + // non-BMP support is platform-dependent on Node.js v4 + node_version: ">=6" +} + issue_4604: { options = { collapse_vars: true,