diff --git a/CHANGELOG.md b/CHANGELOG.md index 67de4a6fb5..4dc5e8f5c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ #### :bug: Bug Fix +- Fix encoding inside tagged template literals. https://github.com/rescript-lang/rescript-compiler/pull/6810 - Fix unhandled cases for exotic idents (allow to use exotic PascalCased identifiers for types). https://github.com/rescript-lang/rescript-compiler/pull/6777 - PPX v4: mark props type in externals as `@live` to avoid dead code warnings for prop fields in the editor tooling. https://github.com/rescript-lang/rescript-compiler/pull/6796 - Fix unused attribute check for `@as`. https://github.com/rescript-lang/rescript-compiler/pull/6795 diff --git a/jscomp/syntax/src/res_core.ml b/jscomp/syntax/src/res_core.ml index 48023f3782..9d712b28cd 100644 --- a/jscomp/syntax/src/res_core.ml +++ b/jscomp/syntax/src/res_core.ml @@ -2284,8 +2284,7 @@ and parse_template_expr ?prefix p = match prefix with | Some {txt = Longident.Lident (("js" | "j" | "json") as prefix); _} -> Some prefix - | Some _ -> None - | None -> Some "js" + | _ -> Some "js" in let start_pos = p.Parser.start_pos in diff --git a/jscomp/syntax/tests/parsing/other/expected/stringLiterals.res.txt b/jscomp/syntax/tests/parsing/other/expected/stringLiterals.res.txt new file mode 100644 index 0000000000..19fcd7de50 --- /dev/null +++ b/jscomp/syntax/tests/parsing/other/expected/stringLiterals.res.txt @@ -0,0 +1,20 @@ +let s = {js|some unicode é £ |js} +let s = (({js|foo|js})[@res.template ]) +let s = + (((((({js|foo |js})[@res.template ]) ^ bar)[@res.template ]) ^ + (({js| baz|js})[@res.template ])) + [@res.template ]) +let s = + (((((({js|some unicode é |js})[@res.template ]) ^ bar)[@res.template ]) ^ + (({js| £ |js})[@res.template ])) + [@res.template ]) +let s = ((x [|(({js|foo|js})[@res.template ])|] [||])[@res.taggedTemplate ]) +let s = + ((x [|(({js|foo |js})[@res.template ]);(({js| baz|js})[@res.template ])|] + [|bar|]) + [@res.taggedTemplate ]) +let s = + ((x + [|(({js|some unicode é |js})[@res.template ]);(({js| £ |js}) + [@res.template ])|] [|bar|]) + [@res.taggedTemplate ]) \ No newline at end of file diff --git a/jscomp/syntax/tests/parsing/other/stringLiterals.res b/jscomp/syntax/tests/parsing/other/stringLiterals.res new file mode 100644 index 0000000000..b54010b378 --- /dev/null +++ b/jscomp/syntax/tests/parsing/other/stringLiterals.res @@ -0,0 +1,7 @@ +let s = "some unicode é £ " +let s = `foo` +let s = `foo ${bar} baz` +let s = `some unicode é ${bar} £ ` +let s = x`foo` +let s = x`foo ${bar} baz` +let s = x`some unicode é ${bar} £ ` \ No newline at end of file diff --git a/jscomp/test/tagged_template_test.js b/jscomp/test/tagged_template_test.js index dd27498e7a..afd462a124 100644 --- a/jscomp/test/tagged_template_test.js +++ b/jscomp/test/tagged_template_test.js @@ -37,7 +37,7 @@ function foo(strings, values) { } let res = foo([ - "| 5 * 10 = ", + "| 5 × 10 = ", " |" ], [5]); @@ -76,12 +76,12 @@ Mt.from_pair_suites("tagged templates", { ], tl: { hd: [ - "with rescript function, it should return a string with the correct interpolations", + "with rescript function, it should return a string with the correct encoding and interpolations", (function (param) { return { TAG: "Eq", _0: res, - _1: "| 5 * 10 = 50 |" + _1: "| 5 × 10 = 50 |" }; }) ], diff --git a/jscomp/test/tagged_template_test.res b/jscomp/test/tagged_template_test.res index a1e169a582..bb302d73bc 100644 --- a/jscomp/test/tagged_template_test.res +++ b/jscomp/test/tagged_template_test.res @@ -26,7 +26,7 @@ let foo = (strings, values) => { res.contents ++ strings[valueCount] } -let res = foo`| 5 * 10 = ${5} |` +let res = foo`| 5 × 10 = ${5} |` Mt.from_pair_suites( "tagged templates", @@ -44,8 +44,8 @@ Mt.from_pair_suites( () => Eq(length, 52), ), ( - "with rescript function, it should return a string with the correct interpolations", - () => Eq(res, "| 5 * 10 = 50 |"), + "with rescript function, it should return a string with the correct encoding and interpolations", + () => Eq(res, "| 5 × 10 = 50 |"), ), ( "a template literal tagged with json should generate a regular string interpolation for now",