Skip to content

Commit cbaf97c

Browse files
committed
Preserve the delimiter of strings in concatenation.
Fixes rescript-lang#5638
1 parent 16dc2e2 commit cbaf97c

8 files changed

+96
-33
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#### :bug: Bug Fix
3030

31+
- Fix issue where characters such as newlines would be escaped in a template string expression https://github.com/rescript-lang/rescript-compiler/issues/5638
3132
- Fix issue where pipe `->` processing eats up attributes https://github.com/rescript-lang/rescript-compiler/pull/5581
3233
- Fix issue where cancelling `rescript build` would leave the `.bsb.lock` file behind and block future builds
3334

jscomp/core/js_exp_make.ml

+18-8
Original file line numberDiff line numberDiff line change
@@ -511,15 +511,25 @@ let function_length ?comment (e : t) : t =
511511
*)
512512

513513
let rec string_append ?comment (e : t) (el : t) : t =
514+
let concat a b ~delim =
515+
{ e with expression_desc = Str { txt = a ^ b; delim } }
516+
in
514517
match (e.expression_desc, el.expression_desc) with
515-
| Str { txt = a }, String_append ({ expression_desc = Str { txt = b } }, c) ->
516-
string_append ?comment (str (a ^ b)) c
517-
| String_append (c, { expression_desc = Str { txt = b } }), Str { txt = a } ->
518-
string_append ?comment c (str (b ^ a))
519-
| ( String_append (a, { expression_desc = Str { txt = b } }),
520-
String_append ({ expression_desc = Str { txt = c } }, d) ) ->
521-
string_append ?comment (string_append a (str (b ^ c))) d
522-
| Str { txt = a }, Str { txt = b } -> str ?comment (a ^ b)
518+
| ( Str { txt = a; delim },
519+
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
520+
when delim = delim_ ->
521+
string_append ?comment (concat a b ~delim) c
522+
| ( String_append (c, { expression_desc = Str { txt = b; delim } }),
523+
Str { txt = a; delim = delim_ } )
524+
when delim = delim_ ->
525+
string_append ?comment c (concat b a ~delim)
526+
| ( String_append (a, { expression_desc = Str { txt = b; delim } }),
527+
String_append ({ expression_desc = Str { txt = c; delim = delim_ } }, d) )
528+
when delim = delim_ ->
529+
string_append ?comment (string_append a (concat b c ~delim)) d
530+
| Str { txt = a; delim }, Str { txt = b; delim = delim_ } when delim = delim_
531+
->
532+
{ (concat a b ~delim) with comment }
523533
| _, _ -> { comment; expression_desc = String_append (e, el) }
524534

525535
let obj ?comment properties : t =

jscomp/test/build.ninja

+2-1
Large diffs are not rendered by default.

jscomp/test/template.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
4+
var bla2 = "";
5+
6+
function concat(param) {
7+
return "\n display:\r flex;\n " + bla2 + "";
8+
}
9+
10+
exports.bla2 = bla2;
11+
exports.concat = concat;
12+
/* No side effect */

jscomp/test/template.res

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
let bla2 = ``
2+
3+
let concat = () => {
4+
let bla = `
5+
display:\r flex;
6+
`
7+
8+
`${bla}${bla2}`
9+
}

lib/4.06.1/unstable/js_compiler.ml

+18-8
Original file line numberDiff line numberDiff line change
@@ -77523,15 +77523,25 @@ let function_length ?comment (e : t) : t =
7752377523
*)
7752477524

7752577525
let rec string_append ?comment (e : t) (el : t) : t =
77526+
let concat a b ~delim =
77527+
{ e with expression_desc = Str { txt = a ^ b; delim } }
77528+
in
7752677529
match (e.expression_desc, el.expression_desc) with
77527-
| Str { txt = a }, String_append ({ expression_desc = Str { txt = b } }, c) ->
77528-
string_append ?comment (str (a ^ b)) c
77529-
| String_append (c, { expression_desc = Str { txt = b } }), Str { txt = a } ->
77530-
string_append ?comment c (str (b ^ a))
77531-
| ( String_append (a, { expression_desc = Str { txt = b } }),
77532-
String_append ({ expression_desc = Str { txt = c } }, d) ) ->
77533-
string_append ?comment (string_append a (str (b ^ c))) d
77534-
| Str { txt = a }, Str { txt = b } -> str ?comment (a ^ b)
77530+
| ( Str { txt = a; delim },
77531+
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
77532+
when delim = delim_ ->
77533+
string_append ?comment (concat a b ~delim) c
77534+
| ( String_append (c, { expression_desc = Str { txt = b; delim } }),
77535+
Str { txt = a; delim = delim_ } )
77536+
when delim = delim_ ->
77537+
string_append ?comment c (concat b a ~delim)
77538+
| ( String_append (a, { expression_desc = Str { txt = b; delim } }),
77539+
String_append ({ expression_desc = Str { txt = c; delim = delim_ } }, d) )
77540+
when delim = delim_ ->
77541+
string_append ?comment (string_append a (concat b c ~delim)) d
77542+
| Str { txt = a; delim }, Str { txt = b; delim = delim_ } when delim = delim_
77543+
->
77544+
{ (concat a b ~delim) with comment }
7753577545
| _, _ -> { comment; expression_desc = String_append (e, el) }
7753677546

7753777547
let obj ?comment properties : t =

lib/4.06.1/unstable/js_playground_compiler.ml

+18-8
Original file line numberDiff line numberDiff line change
@@ -77523,15 +77523,25 @@ let function_length ?comment (e : t) : t =
7752377523
*)
7752477524

7752577525
let rec string_append ?comment (e : t) (el : t) : t =
77526+
let concat a b ~delim =
77527+
{ e with expression_desc = Str { txt = a ^ b; delim } }
77528+
in
7752677529
match (e.expression_desc, el.expression_desc) with
77527-
| Str { txt = a }, String_append ({ expression_desc = Str { txt = b } }, c) ->
77528-
string_append ?comment (str (a ^ b)) c
77529-
| String_append (c, { expression_desc = Str { txt = b } }), Str { txt = a } ->
77530-
string_append ?comment c (str (b ^ a))
77531-
| ( String_append (a, { expression_desc = Str { txt = b } }),
77532-
String_append ({ expression_desc = Str { txt = c } }, d) ) ->
77533-
string_append ?comment (string_append a (str (b ^ c))) d
77534-
| Str { txt = a }, Str { txt = b } -> str ?comment (a ^ b)
77530+
| ( Str { txt = a; delim },
77531+
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
77532+
when delim = delim_ ->
77533+
string_append ?comment (concat a b ~delim) c
77534+
| ( String_append (c, { expression_desc = Str { txt = b; delim } }),
77535+
Str { txt = a; delim = delim_ } )
77536+
when delim = delim_ ->
77537+
string_append ?comment c (concat b a ~delim)
77538+
| ( String_append (a, { expression_desc = Str { txt = b; delim } }),
77539+
String_append ({ expression_desc = Str { txt = c; delim = delim_ } }, d) )
77540+
when delim = delim_ ->
77541+
string_append ?comment (string_append a (concat b c ~delim)) d
77542+
| Str { txt = a; delim }, Str { txt = b; delim = delim_ } when delim = delim_
77543+
->
77544+
{ (concat a b ~delim) with comment }
7753577545
| _, _ -> { comment; expression_desc = String_append (e, el) }
7753677546

7753777547
let obj ?comment properties : t =

lib/4.06.1/whole_compiler.ml

+18-8
Original file line numberDiff line numberDiff line change
@@ -253491,15 +253491,25 @@ let function_length ?comment (e : t) : t =
253491253491
*)
253492253492

253493253493
let rec string_append ?comment (e : t) (el : t) : t =
253494+
let concat a b ~delim =
253495+
{ e with expression_desc = Str { txt = a ^ b; delim } }
253496+
in
253494253497
match (e.expression_desc, el.expression_desc) with
253495-
| Str { txt = a }, String_append ({ expression_desc = Str { txt = b } }, c) ->
253496-
string_append ?comment (str (a ^ b)) c
253497-
| String_append (c, { expression_desc = Str { txt = b } }), Str { txt = a } ->
253498-
string_append ?comment c (str (b ^ a))
253499-
| ( String_append (a, { expression_desc = Str { txt = b } }),
253500-
String_append ({ expression_desc = Str { txt = c } }, d) ) ->
253501-
string_append ?comment (string_append a (str (b ^ c))) d
253502-
| Str { txt = a }, Str { txt = b } -> str ?comment (a ^ b)
253498+
| ( Str { txt = a; delim },
253499+
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
253500+
when delim = delim_ ->
253501+
string_append ?comment (concat a b ~delim) c
253502+
| ( String_append (c, { expression_desc = Str { txt = b; delim } }),
253503+
Str { txt = a; delim = delim_ } )
253504+
when delim = delim_ ->
253505+
string_append ?comment c (concat b a ~delim)
253506+
| ( String_append (a, { expression_desc = Str { txt = b; delim } }),
253507+
String_append ({ expression_desc = Str { txt = c; delim = delim_ } }, d) )
253508+
when delim = delim_ ->
253509+
string_append ?comment (string_append a (concat b c ~delim)) d
253510+
| Str { txt = a; delim }, Str { txt = b; delim = delim_ } when delim = delim_
253511+
->
253512+
{ (concat a b ~delim) with comment }
253503253513
| _, _ -> { comment; expression_desc = String_append (e, el) }
253504253514

253505253515
let obj ?comment properties : t =

0 commit comments

Comments
 (0)