Skip to content

Preserve the delimiter of strings in concatenation. #5639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#### :bug: Bug Fix

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

Expand Down
26 changes: 18 additions & 8 deletions jscomp/core/js_exp_make.ml
Original file line number Diff line number Diff line change
Expand Up @@ -511,15 +511,25 @@ let function_length ?comment (e : t) : t =
*)

let rec string_append ?comment (e : t) (el : t) : t =
let concat a b ~delim =
{ e with expression_desc = Str { txt = a ^ b; delim } }
in
match (e.expression_desc, el.expression_desc) with
| Str { txt = a }, String_append ({ expression_desc = Str { txt = b } }, c) ->
string_append ?comment (str (a ^ b)) c
| String_append (c, { expression_desc = Str { txt = b } }), Str { txt = a } ->
string_append ?comment c (str (b ^ a))
| ( String_append (a, { expression_desc = Str { txt = b } }),
String_append ({ expression_desc = Str { txt = c } }, d) ) ->
string_append ?comment (string_append a (str (b ^ c))) d
| Str { txt = a }, Str { txt = b } -> str ?comment (a ^ b)
| ( Str { txt = a; delim },
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
when delim = delim_ ->
string_append ?comment (concat a b ~delim) c
| ( String_append (c, { expression_desc = Str { txt = b; delim } }),
Str { txt = a; delim = delim_ } )
when delim = delim_ ->
string_append ?comment c (concat b a ~delim)
| ( String_append (a, { expression_desc = Str { txt = b; delim } }),
String_append ({ expression_desc = Str { txt = c; delim = delim_ } }, d) )
when delim = delim_ ->
string_append ?comment (string_append a (concat b c ~delim)) d
| Str { txt = a; delim }, Str { txt = b; delim = delim_ } when delim = delim_
->
{ (concat a b ~delim) with comment }
| _, _ -> { comment; expression_desc = String_append (e, el) }

let obj ?comment properties : t =
Expand Down
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions jscomp/test/template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';


var bla2 = "";

function concat(param) {
return "\n display:\r flex;\n " + bla2 + "";
}

exports.bla2 = bla2;
exports.concat = concat;
/* No side effect */
9 changes: 9 additions & 0 deletions jscomp/test/template.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let bla2 = ``

let concat = () => {
let bla = `
display:\r flex;
`

`${bla}${bla2}`
}
26 changes: 18 additions & 8 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77523,15 +77523,25 @@ let function_length ?comment (e : t) : t =
*)

let rec string_append ?comment (e : t) (el : t) : t =
let concat a b ~delim =
{ e with expression_desc = Str { txt = a ^ b; delim } }
in
match (e.expression_desc, el.expression_desc) with
| Str { txt = a }, String_append ({ expression_desc = Str { txt = b } }, c) ->
string_append ?comment (str (a ^ b)) c
| String_append (c, { expression_desc = Str { txt = b } }), Str { txt = a } ->
string_append ?comment c (str (b ^ a))
| ( String_append (a, { expression_desc = Str { txt = b } }),
String_append ({ expression_desc = Str { txt = c } }, d) ) ->
string_append ?comment (string_append a (str (b ^ c))) d
| Str { txt = a }, Str { txt = b } -> str ?comment (a ^ b)
| ( Str { txt = a; delim },
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
when delim = delim_ ->
string_append ?comment (concat a b ~delim) c
| ( String_append (c, { expression_desc = Str { txt = b; delim } }),
Str { txt = a; delim = delim_ } )
when delim = delim_ ->
string_append ?comment c (concat b a ~delim)
| ( String_append (a, { expression_desc = Str { txt = b; delim } }),
String_append ({ expression_desc = Str { txt = c; delim = delim_ } }, d) )
when delim = delim_ ->
string_append ?comment (string_append a (concat b c ~delim)) d
| Str { txt = a; delim }, Str { txt = b; delim = delim_ } when delim = delim_
->
{ (concat a b ~delim) with comment }
| _, _ -> { comment; expression_desc = String_append (e, el) }

let obj ?comment properties : t =
Expand Down
26 changes: 18 additions & 8 deletions lib/4.06.1/unstable/js_playground_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -77523,15 +77523,25 @@ let function_length ?comment (e : t) : t =
*)

let rec string_append ?comment (e : t) (el : t) : t =
let concat a b ~delim =
{ e with expression_desc = Str { txt = a ^ b; delim } }
in
match (e.expression_desc, el.expression_desc) with
| Str { txt = a }, String_append ({ expression_desc = Str { txt = b } }, c) ->
string_append ?comment (str (a ^ b)) c
| String_append (c, { expression_desc = Str { txt = b } }), Str { txt = a } ->
string_append ?comment c (str (b ^ a))
| ( String_append (a, { expression_desc = Str { txt = b } }),
String_append ({ expression_desc = Str { txt = c } }, d) ) ->
string_append ?comment (string_append a (str (b ^ c))) d
| Str { txt = a }, Str { txt = b } -> str ?comment (a ^ b)
| ( Str { txt = a; delim },
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
when delim = delim_ ->
string_append ?comment (concat a b ~delim) c
| ( String_append (c, { expression_desc = Str { txt = b; delim } }),
Str { txt = a; delim = delim_ } )
when delim = delim_ ->
string_append ?comment c (concat b a ~delim)
| ( String_append (a, { expression_desc = Str { txt = b; delim } }),
String_append ({ expression_desc = Str { txt = c; delim = delim_ } }, d) )
when delim = delim_ ->
string_append ?comment (string_append a (concat b c ~delim)) d
| Str { txt = a; delim }, Str { txt = b; delim = delim_ } when delim = delim_
->
{ (concat a b ~delim) with comment }
| _, _ -> { comment; expression_desc = String_append (e, el) }

let obj ?comment properties : t =
Expand Down
26 changes: 18 additions & 8 deletions lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -253491,15 +253491,25 @@ let function_length ?comment (e : t) : t =
*)

let rec string_append ?comment (e : t) (el : t) : t =
let concat a b ~delim =
{ e with expression_desc = Str { txt = a ^ b; delim } }
in
match (e.expression_desc, el.expression_desc) with
| Str { txt = a }, String_append ({ expression_desc = Str { txt = b } }, c) ->
string_append ?comment (str (a ^ b)) c
| String_append (c, { expression_desc = Str { txt = b } }), Str { txt = a } ->
string_append ?comment c (str (b ^ a))
| ( String_append (a, { expression_desc = Str { txt = b } }),
String_append ({ expression_desc = Str { txt = c } }, d) ) ->
string_append ?comment (string_append a (str (b ^ c))) d
| Str { txt = a }, Str { txt = b } -> str ?comment (a ^ b)
| ( Str { txt = a; delim },
String_append ({ expression_desc = Str { txt = b; delim = delim_ } }, c) )
when delim = delim_ ->
string_append ?comment (concat a b ~delim) c
| ( String_append (c, { expression_desc = Str { txt = b; delim } }),
Str { txt = a; delim = delim_ } )
when delim = delim_ ->
string_append ?comment c (concat b a ~delim)
| ( String_append (a, { expression_desc = Str { txt = b; delim } }),
String_append ({ expression_desc = Str { txt = c; delim = delim_ } }, d) )
when delim = delim_ ->
string_append ?comment (string_append a (concat b c ~delim)) d
| Str { txt = a; delim }, Str { txt = b; delim = delim_ } when delim = delim_
->
{ (concat a b ~delim) with comment }
| _, _ -> { comment; expression_desc = String_append (e, el) }

let obj ?comment properties : t =
Expand Down