Skip to content

Commit 9ba7146

Browse files
committed
Do not escape strings in @bs.as on externals.
Fixes #5402
1 parent f8c33db commit 9ba7146

File tree

11 files changed

+50
-65
lines changed

11 files changed

+50
-65
lines changed

jscomp/core/js_dump.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
591591
P.string f s;
592592
P.string f "\"";
593593
cxt
594-
| Str (_, s, _) ->
594+
| Str (_, s, escape) ->
595595
(*TODO --
596596
when utf8-> it will not escape '\\' which is definitely not we want
597597
*)
598-
Js_dump_string.pp_string f s;
598+
Js_dump_string.pp_string ~escape f s;
599599
cxt
600600
| Raw_js_code { code = s; code_info = info } -> (
601601
match info with

jscomp/core/js_dump_string.ml

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,9 @@ let escape_to_string s =
101101
escape_to_buffer buf s;
102102
Ext_buffer.contents buf
103103

104-
(*
105-
let pp_string ?(escape=true) f s =
106-
if escape then
107-
P.string f (escape_to_string s)
108-
else
109-
P.string f ("\"" ^ s ^ "\"")
110-
111-
*)
112-
let pp_string f s = P.string f (escape_to_string s)
104+
let pp_string ?(escape = true) f s =
105+
if escape then P.string f (escape_to_string s)
106+
else P.string f ("\"" ^ s ^ "\"")
113107
(* let _best_string_quote s =
114108
let simple = ref 0 in
115109
let double = ref 0 in

jscomp/core/js_dump_string.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,4 @@
2626
JS lexing convention
2727
*)
2828
val escape_to_string : string -> string
29-
30-
(* val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit *)
31-
val pp_string : Ext_pp.t -> string -> unit
29+
val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit

jscomp/core/js_of_lam_variant.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
3434
match arg.expression_desc with
3535
| Str (_, s, _) ->
3636
let s = Ext_list.assoc_by_string dispatches s None in
37-
E.str (* ~escape:false *) s
37+
E.str s
3838
| _ ->
3939
E.of_block
4040
[

jscomp/core/lam_compile_const.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ and translate (x : Lam_constant.t) : J.expression =
101101
let translate_arg_cst (cst : External_arg_spec.cst) =
102102
match cst with
103103
| Arg_int_lit i -> E.int (Int32.of_int i)
104-
| Arg_string_lit i -> E.str i
104+
| Arg_string_lit i -> E.str ~escape:false i
105105
| Arg_js_literal s -> E.raw_js_code (Exp (Js_literal { comment = None })) s

jscomp/test/build.ninja

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

jscomp/test/external_ppx2.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
4+
f("\h\e\l\lo", 42);
5+
6+
var x = "\h\e\l\lo";
7+
8+
var y;
9+
10+
exports.x = x;
11+
exports.y = y;
12+
/* Not a pure module */

jscomp/test/external_ppx2.res

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
external f: (@as("\h\e\l\lo") _, int) => unit = "f"
2+
3+
let x = "\h\e\l\lo"
4+
let y = f(42)

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65732,9 +65732,7 @@ module Js_dump_string : sig
6573265732
JS lexing convention
6573365733
*)
6573465734
val escape_to_string : string -> string
65735-
65736-
(* val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit *)
65737-
val pp_string : Ext_pp.t -> string -> unit
65735+
val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit
6573865736

6573965737
end = struct
6574065738
#1 "js_dump_string.ml"
@@ -65841,15 +65839,9 @@ let escape_to_string s =
6584165839
escape_to_buffer buf s;
6584265840
Ext_buffer.contents buf
6584365841

65844-
(*
65845-
let pp_string ?(escape=true) f s =
65846-
if escape then
65847-
P.string f (escape_to_string s)
65848-
else
65849-
P.string f ("\"" ^ s ^ "\"")
65850-
65851-
*)
65852-
let pp_string f s = P.string f (escape_to_string s)
65842+
let pp_string ?(escape = true) f s =
65843+
if escape then P.string f (escape_to_string s)
65844+
else P.string f ("\"" ^ s ^ "\"")
6585365845
(* let _best_string_quote s =
6585465846
let simple = ref 0 in
6585565847
let double = ref 0 in
@@ -69769,11 +69761,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
6976969761
P.string f s;
6977069762
P.string f "\"";
6977169763
cxt
69772-
| Str (_, s, _) ->
69764+
| Str (_, s, escape) ->
6977369765
(*TODO --
6977469766
when utf8-> it will not escape '\\' which is definitely not we want
6977569767
*)
69776-
Js_dump_string.pp_string f s;
69768+
Js_dump_string.pp_string ~escape f s;
6977769769
cxt
6977869770
| Raw_js_code { code = s; code_info = info } -> (
6977969771
match info with
@@ -85227,7 +85219,7 @@ and translate (x : Lam_constant.t) : J.expression =
8522785219
let translate_arg_cst (cst : External_arg_spec.cst) =
8522885220
match cst with
8522985221
| Arg_int_lit i -> E.int (Int32.of_int i)
85230-
| Arg_string_lit i -> E.str i
85222+
| Arg_string_lit i -> E.str ~escape:false i
8523185223
| Arg_js_literal s -> E.raw_js_code (Exp (Js_literal { comment = None })) s
8523285224

8523385225
end
@@ -85538,7 +85530,7 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
8553885530
match arg.expression_desc with
8553985531
| Str (_, s, _) ->
8554085532
let s = Ext_list.assoc_by_string dispatches s None in
85541-
E.str (* ~escape:false *) s
85533+
E.str s
8554285534
| _ ->
8554385535
E.of_block
8554485536
[

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -65732,9 +65732,7 @@ module Js_dump_string : sig
6573265732
JS lexing convention
6573365733
*)
6573465734
val escape_to_string : string -> string
65735-
65736-
(* val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit *)
65737-
val pp_string : Ext_pp.t -> string -> unit
65735+
val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit
6573865736

6573965737
end = struct
6574065738
#1 "js_dump_string.ml"
@@ -65841,15 +65839,9 @@ let escape_to_string s =
6584165839
escape_to_buffer buf s;
6584265840
Ext_buffer.contents buf
6584365841

65844-
(*
65845-
let pp_string ?(escape=true) f s =
65846-
if escape then
65847-
P.string f (escape_to_string s)
65848-
else
65849-
P.string f ("\"" ^ s ^ "\"")
65850-
65851-
*)
65852-
let pp_string f s = P.string f (escape_to_string s)
65842+
let pp_string ?(escape = true) f s =
65843+
if escape then P.string f (escape_to_string s)
65844+
else P.string f ("\"" ^ s ^ "\"")
6585365845
(* let _best_string_quote s =
6585465846
let simple = ref 0 in
6585565847
let double = ref 0 in
@@ -69769,11 +69761,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
6976969761
P.string f s;
6977069762
P.string f "\"";
6977169763
cxt
69772-
| Str (_, s, _) ->
69764+
| Str (_, s, escape) ->
6977369765
(*TODO --
6977469766
when utf8-> it will not escape '\\' which is definitely not we want
6977569767
*)
69776-
Js_dump_string.pp_string f s;
69768+
Js_dump_string.pp_string ~escape f s;
6977769769
cxt
6977869770
| Raw_js_code { code = s; code_info = info } -> (
6977969771
match info with
@@ -85227,7 +85219,7 @@ and translate (x : Lam_constant.t) : J.expression =
8522785219
let translate_arg_cst (cst : External_arg_spec.cst) =
8522885220
match cst with
8522985221
| Arg_int_lit i -> E.int (Int32.of_int i)
85230-
| Arg_string_lit i -> E.str i
85222+
| Arg_string_lit i -> E.str ~escape:false i
8523185223
| Arg_js_literal s -> E.raw_js_code (Exp (Js_literal { comment = None })) s
8523285224

8523385225
end
@@ -85538,7 +85530,7 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
8553885530
match arg.expression_desc with
8553985531
| Str (_, s, _) ->
8554085532
let s = Ext_list.assoc_by_string dispatches s None in
85541-
E.str (* ~escape:false *) s
85533+
E.str s
8554285534
| _ ->
8554385535
E.of_block
8554485536
[

lib/4.06.1/whole_compiler.ml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -233900,9 +233900,7 @@ module Js_dump_string : sig
233900233900
JS lexing convention
233901233901
*)
233902233902
val escape_to_string : string -> string
233903-
233904-
(* val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit *)
233905-
val pp_string : Ext_pp.t -> string -> unit
233903+
val pp_string : ?escape:bool -> Ext_pp.t -> string -> unit
233906233904

233907233905
end = struct
233908233906
#1 "js_dump_string.ml"
@@ -234009,15 +234007,9 @@ let escape_to_string s =
234009234007
escape_to_buffer buf s;
234010234008
Ext_buffer.contents buf
234011234009

234012-
(*
234013-
let pp_string ?(escape=true) f s =
234014-
if escape then
234015-
P.string f (escape_to_string s)
234016-
else
234017-
P.string f ("\"" ^ s ^ "\"")
234018-
234019-
*)
234020-
let pp_string f s = P.string f (escape_to_string s)
234010+
let pp_string ?(escape = true) f s =
234011+
if escape then P.string f (escape_to_string s)
234012+
else P.string f ("\"" ^ s ^ "\"")
234021234013
(* let _best_string_quote s =
234022234014
let simple = ref 0 in
234023234015
let double = ref 0 in
@@ -237937,11 +237929,11 @@ and expression_desc cxt ~(level : int) f x : cxt =
237937237929
P.string f s;
237938237930
P.string f "\"";
237939237931
cxt
237940-
| Str (_, s, _) ->
237932+
| Str (_, s, escape) ->
237941237933
(*TODO --
237942237934
when utf8-> it will not escape '\\' which is definitely not we want
237943237935
*)
237944-
Js_dump_string.pp_string f s;
237936+
Js_dump_string.pp_string ~escape f s;
237945237937
cxt
237946237938
| Raw_js_code { code = s; code_info = info } -> (
237947237939
match info with
@@ -246796,7 +246788,7 @@ and translate (x : Lam_constant.t) : J.expression =
246796246788
let translate_arg_cst (cst : External_arg_spec.cst) =
246797246789
match cst with
246798246790
| Arg_int_lit i -> E.int (Int32.of_int i)
246799-
| Arg_string_lit i -> E.str i
246791+
| Arg_string_lit i -> E.str ~escape:false i
246800246792
| Arg_js_literal s -> E.raw_js_code (Exp (Js_literal { comment = None })) s
246801246793

246802246794
end
@@ -247107,7 +247099,7 @@ let eval (arg : J.expression) (dispatches : (string * string) list) : E.t =
247107247099
match arg.expression_desc with
247108247100
| Str (_, s, _) ->
247109247101
let s = Ext_list.assoc_by_string dispatches s None in
247110-
E.str (* ~escape:false *) s
247102+
E.str s
247111247103
| _ ->
247112247104
E.of_block
247113247105
[

0 commit comments

Comments
 (0)