Skip to content

Commit 0520249

Browse files
authored
Merge pull request #3148 from BuckleScript/fix_3142
[part2] fix #3142 (with unitests)
2 parents bef06f2 + d307ebb commit 0520249

30 files changed

+60150
-59735
lines changed

jscomp/all.depend

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ common/bs_warnings.cmi :
172172
common/lam_methname.cmi :
173173
common/ml_binary.cmi :
174174
syntax/bs_syntaxerr.cmx : syntax/bs_syntaxerr.cmi
175-
syntax/ast_compatible.cmx : ext/ext_list.cmx syntax/ast_compatible.cmi
175+
syntax/ast_compatible.cmx : ext/ext_pervasives.cmx ext/ext_list.cmx \
176+
syntax/ast_compatible.cmi
176177
syntax/ast_utf8_string.cmx : ext/ext_utf8.cmx ext/ext_char.cmx \
177178
syntax/ast_utf8_string.cmi
178-
syntax/ast_utf8_string_interp.cmx : ext/literals.cmx ext/ext_utf8.cmx \
179-
ext/ext_string.cmx ext/ext_char.cmx common/bs_loc.cmx \
179+
syntax/ast_utf8_string_interp.cmx : ext/ext_utf8.cmx ext/ext_string.cmx \
180+
ext/ext_char.cmx common/bs_loc.cmx syntax/ast_utf8_string.cmx \
180181
syntax/ast_compatible.cmx syntax/ast_utf8_string_interp.cmi
181182
syntax/ast_derive_constructor.cmx :
182183
syntax/ast_derive_util.cmx : ext/ext_list.cmx syntax/ast_compatible.cmx \
@@ -205,15 +206,17 @@ syntax/ast_comb.cmx : ext/ext_list.cmx syntax/ast_literal.cmx \
205206
syntax/ast_core_type.cmx : ext/ext_list.cmx syntax/bs_syntaxerr.cmx \
206207
syntax/ast_literal.cmx syntax/ast_compatible.cmx syntax/ast_comb.cmx \
207208
syntax/ast_core_type.cmi
208-
syntax/bs_ast_invariant.cmx : ext/literals.cmx ext/hash_set_poly.cmx \
209-
ext/ext_string.cmx common/bs_warnings.cmx syntax/bs_ast_iterator.cmx \
210-
syntax/ast_core_type.cmx syntax/bs_ast_invariant.cmi
211-
syntax/ast_attributes.cmx : ext/ext_string.cmx syntax/bs_syntaxerr.cmx \
212-
syntax/bs_ast_invariant.cmx syntax/ast_payload.cmx \
213-
syntax/ast_compatible.cmx syntax/ast_attributes.cmi
214-
syntax/ast_polyvar.cmx : syntax/external_arg_spec.cmx ext/ext_pervasives.cmx \
215-
ext/ext_list.cmx syntax/bs_syntaxerr.cmx syntax/ast_attributes.cmx \
216-
syntax/ast_polyvar.cmi
209+
syntax/bs_ast_invariant.cmx : ext/hash_set_poly.cmx ext/ext_list.cmx \
210+
common/bs_warnings.cmx syntax/bs_ast_iterator.cmx \
211+
syntax/ast_utf8_string_interp.cmx syntax/ast_core_type.cmx \
212+
syntax/bs_ast_invariant.cmi
213+
syntax/ast_attributes.cmx : ext/ext_string.cmx ext/ext_list.cmx \
214+
syntax/bs_syntaxerr.cmx syntax/bs_ast_invariant.cmx \
215+
syntax/ast_payload.cmx syntax/ast_compatible.cmx \
216+
syntax/ast_attributes.cmi
217+
syntax/ast_polyvar.cmx : syntax/external_arg_spec.cmx ext/ext_list.cmx \
218+
syntax/bs_syntaxerr.cmx syntax/ast_compatible.cmx \
219+
syntax/ast_attributes.cmx syntax/ast_polyvar.cmi
217220
syntax/external_ffi_types.cmx : syntax/external_arg_spec.cmx \
218221
ext/ext_string.cmx ext/ext_pervasives.cmx common/bs_version.cmx \
219222
syntax/external_ffi_types.cmi
@@ -240,7 +243,7 @@ syntax/ast_derive_projector.cmx : ext/ext_string.cmx ext/ext_list.cmx \
240243
syntax/ast_derive_js_mapper.cmx : ext/ext_list.cmx syntax/ast_polyvar.cmx \
241244
syntax/ast_literal.cmx syntax/ast_derive_util.cmx syntax/ast_derive.cmx \
242245
syntax/ast_core_type.cmx syntax/ast_compatible.cmx syntax/ast_comb.cmx \
243-
syntax/ast_derive_js_mapper.cmi
246+
syntax/ast_attributes.cmx syntax/ast_derive_js_mapper.cmi
244247
syntax/ast_util.cmx : ext/literals.cmx syntax/external_process.cmx \
245248
ext/ext_string.cmx ext/ext_list.cmx syntax/bs_syntaxerr.cmx \
246249
syntax/bs_ast_mapper.cmx syntax/ast_payload.cmx syntax/ast_pat.cmx \
@@ -271,10 +274,9 @@ syntax/ast_core_type_class_type.cmx : ext/literals.cmx ext/ext_ref.cmx \
271274
ext/ext_list.cmx syntax/bs_ast_mapper.cmx syntax/ast_util.cmx \
272275
syntax/ast_literal.cmx syntax/ast_compatible.cmx syntax/ast_comb.cmx \
273276
syntax/ast_attributes.cmx syntax/ast_core_type_class_type.cmi
274-
syntax/ppx_entry.cmx : ext/string_map.cmx ext/literals.cmx \
275-
common/js_config.cmx ext/ext_string.cmx syntax/bs_ast_mapper.cmx \
276-
syntax/bs_ast_invariant.cmx syntax/ast_util.cmx \
277-
syntax/ast_utf8_string_interp.cmx syntax/ast_utf8_string.cmx \
277+
syntax/ppx_entry.cmx : ext/string_map.cmx common/js_config.cmx \
278+
ext/ext_list.cmx syntax/bs_ast_mapper.cmx syntax/bs_ast_invariant.cmx \
279+
syntax/ast_util.cmx syntax/ast_utf8_string_interp.cmx \
278280
syntax/ast_tuple_pattern_flatten.cmx syntax/ast_tdcls.cmx \
279281
syntax/ast_structure.cmx syntax/ast_primitive.cmx syntax/ast_payload.cmx \
280282
syntax/ast_literal.cmx syntax/ast_exp_extension.cmx \
@@ -482,8 +484,8 @@ core/ocaml_parse.cmx : syntax/ppx_entry.cmx common/js_config.cmx \
482484
ext/ext_path.cmx core/ocaml_parse.cmi
483485
core/lam_pointer_info.cmx : core/lam_pointer_info.cmi
484486
core/lam_tag_info.cmx :
485-
core/lam_constant.cmx : ext/literals.cmx core/lam_tag_info.cmx \
486-
core/lam_pointer_info.cmx ext/ext_string.cmx ext/ext_list.cmx \
487+
core/lam_constant.cmx : core/lam_tag_info.cmx core/lam_pointer_info.cmx \
488+
ext/ext_string.cmx ext/ext_list.cmx syntax/ast_utf8_string_interp.cmx \
487489
core/lam_constant.cmi
488490
core/js_op.cmx : core/lam_tag_info.cmx core/lam_compat.cmx
489491
core/lam_module_ident.cmx : core/js_op.cmx core/j.cmx ext/hashtbl_make.cmx \

jscomp/bin/all_ounit_tests.ml

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3798,10 +3798,6 @@ val dash_nostdlib : string
37983798

37993799
val reactjs_jsx_ppx_2_exe : string
38003800
val reactjs_jsx_ppx_3_exe : string
3801-
val unescaped_j_delimiter : string
3802-
val escaped_j_delimiter : string
3803-
3804-
val unescaped_js_delimiter : string
38053801

38063802
val native : string
38073803
val bytecode : string
@@ -3932,9 +3928,6 @@ let dash_nostdlib = "-nostdlib"
39323928

39333929
let reactjs_jsx_ppx_2_exe = "reactjs_jsx_ppx_2.exe"
39343930
let reactjs_jsx_ppx_3_exe = "reactjs_jsx_ppx_3.exe"
3935-
let unescaped_j_delimiter = "j"
3936-
let unescaped_js_delimiter = "js"
3937-
let escaped_j_delimiter = "*j" (* not user level syntax allowed *)
39383931

39393932
let native = "native"
39403933
let bytecode = "bytecode"
@@ -14610,6 +14603,7 @@ module Ast_compatible : sig
1461014603
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
1461114604

1461214605

14606+
type poly_var_label = string
1461314607
type arg_label = string
1461414608
type label =
1461514609
| Nolabel
@@ -14770,7 +14764,8 @@ type object_field =
1477014764
val object_field : string -> attributes -> core_type -> object_field
1477114765

1477214766

14773-
14767+
val hash_label : poly_var_label -> int
14768+
val label_of_name : poly_var_label -> string
1477414769
end = struct
1477514770
#1 "ast_compatible.ml"
1477614771
(* Copyright (C) 2018 Authors of BuckleScript
@@ -14803,6 +14798,7 @@ open Parsetree
1480314798
let default_loc = Location.none
1480414799

1480514800

14801+
type poly_var_label = string
1480614802
type arg_label = string
1480714803
type label =
1480814804
| Nolabel
@@ -15023,6 +15019,12 @@ type object_field =
1502315019

1502415020
let object_field l attrs ty =
1502515021
(l,attrs,ty)
15022+
15023+
15024+
15025+
let hash_label : poly_var_label -> int = Ext_pervasives.hash_variant
15026+
external label_of_name : poly_var_label -> string = "%identity"
15027+
1502615028
end
1502715029
module Bs_loc : sig
1502815030
#1 "bs_loc.mli"
@@ -15176,8 +15178,22 @@ type exn += Error of pos * pos * error
1517615178
val empty_segment : segment -> bool
1517715179

1517815180
val transform_test : string -> segment list
15179-
val transform_interp : Location.t -> string -> Parsetree.expression
1518015181

15182+
15183+
15184+
val transform :
15185+
Parsetree.expression ->
15186+
string ->
15187+
string ->
15188+
Parsetree.expression
15189+
15190+
val is_unicode_string :
15191+
string ->
15192+
bool
15193+
15194+
val is_unescaped :
15195+
string ->
15196+
bool
1518115197
end = struct
1518215198
#1 "ast_utf8_string_interp.ml"
1518315199
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -15566,8 +15582,11 @@ let to_string_ident : Longident.t =
1556615582
Ldot (Ldot (Lident "Js", "String"), "make")
1556715583

1556815584

15585+
let escaped_j_delimiter = "*j" (* not user level syntax allowed *)
15586+
let unescaped_j_delimiter = "j"
15587+
let unescaped_js_delimiter = "js"
1556915588

15570-
let escaped = Some Literals.escaped_j_delimiter
15589+
let escaped = Some escaped_j_delimiter
1557115590

1557215591
let concat_exp
1557315592
(a : Parsetree.expression)
@@ -15622,8 +15641,7 @@ let transform_interp loc s =
1562215641
let rev_segments = cxt.segments in
1562315642
match rev_segments with
1562415643
| [] ->
15625-
Ast_compatible.const_exp_string ~loc
15626-
"" ~delimiter:Literals.escaped_j_delimiter
15644+
Ast_compatible.const_exp_string ~loc "" ?delimiter:escaped
1562715645
| [ segment] ->
1562815646
aux loc segment
1562915647
| a::rest ->
@@ -15637,6 +15655,25 @@ let transform_interp loc s =
1563715655
Location.raise_errorf ~loc:(update border start pos loc )
1563815656
"%a" pp_error error
1563915657

15658+
15659+
let transform (e : Parsetree.expression) s delim : Parsetree.expression =
15660+
if Ext_string.equal delim unescaped_js_delimiter then
15661+
let js_str = Ast_utf8_string.transform e.pexp_loc s in
15662+
{ e with pexp_desc =
15663+
Pexp_constant (
15664+
15665+
Const_string
15666+
15667+
(js_str, escaped))}
15668+
else if Ext_string.equal delim unescaped_j_delimiter then
15669+
transform_interp e.pexp_loc s
15670+
else e
15671+
15672+
let is_unicode_string opt = Ext_string.equal opt escaped_j_delimiter
15673+
15674+
let is_unescaped s =
15675+
Ext_string.equal s unescaped_j_delimiter
15676+
|| Ext_string.equal s unescaped_js_delimiter
1564015677
end
1564115678
module Ounit_unicode_tests
1564215679
= struct

jscomp/core/lam_constant.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ let rec convert_constant ( const : Lambda.structured_constant) : t =
9191
| Const_base (Const_int i) -> (Const_int i)
9292
| Const_base (Const_char i) -> (Const_char i)
9393
| Const_base (Const_string(i,opt)) ->
94-
begin match opt with
94+
(match opt with
9595
| Some opt when
96-
Ext_string.equal opt Literals.escaped_j_delimiter ->
96+
Ast_utf8_string_interp.is_unicode_string opt ->
9797
Const_unicode i
9898
| _ ->
99-
Const_string i
100-
end
99+
Const_string i)
100+
101101
| Const_base (Const_float i) -> (Const_float i)
102102
| Const_base (Const_int32 i) -> (Const_int32 i)
103103
| Const_base (Const_int64 i) -> (Const_int64 i)

jscomp/ext/literals.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,6 @@ let dash_nostdlib = "-nostdlib"
117117

118118
let reactjs_jsx_ppx_2_exe = "reactjs_jsx_ppx_2.exe"
119119
let reactjs_jsx_ppx_3_exe = "reactjs_jsx_ppx_3.exe"
120-
let unescaped_j_delimiter = "j"
121-
let unescaped_js_delimiter = "js"
122-
let escaped_j_delimiter = "*j" (* not user level syntax allowed *)
123120

124121
let native = "native"
125122
let bytecode = "bytecode"

jscomp/ext/literals.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,6 @@ val dash_nostdlib : string
118118

119119
val reactjs_jsx_ppx_2_exe : string
120120
val reactjs_jsx_ppx_3_exe : string
121-
val unescaped_j_delimiter : string
122-
val escaped_j_delimiter : string
123-
124-
val unescaped_js_delimiter : string
125121

126122
val native : string
127123
val bytecode : string

jscomp/syntax/ast_attributes.ml

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ let iter_process_derive_type attrs =
199199
it is worse in bs.uncurry since it will introduce
200200
inconsistency in arity
201201
*)
202-
let iter_process_bs_string_int_unwrap_uncurry attrs =
202+
let iter_process_bs_string_int_unwrap_uncurry (attrs : Parsetree.attributes) =
203203
let st = ref `Nothing in
204204
let assign v (({loc;_}, _ ) as attr : attr) =
205205
if !st = `Nothing then
@@ -208,8 +208,7 @@ let iter_process_bs_string_int_unwrap_uncurry attrs =
208208
st := v ;
209209
end
210210
else Bs_syntaxerr.err loc Conflict_attributes in
211-
List.iter
212-
(fun (({txt ; loc}, (payload : _ ) ) as attr : attr) ->
211+
Ext_list.iter attrs (fun (({txt ; loc}, (payload : _ ) ) as attr) ->
213212
match txt with
214213
| "bs.string"
215214
-> assign `String attr
@@ -223,8 +222,8 @@ let iter_process_bs_string_int_unwrap_uncurry attrs =
223222
->
224223
assign (`Uncurry (Ast_payload.is_single_int payload)) attr
225224
| _ -> ()
226-
) attrs;
227-
!st
225+
) ;
226+
!st
228227

229228

230229
let iter_process_bs_string_as (attrs : t) : string option =
@@ -248,17 +247,37 @@ let iter_process_bs_string_as (attrs : t) : string option =
248247
) ;
249248
!st
250249

251-
let has_bs_optional (attrs : t) : bool =
252-
List.exists
250+
let iter_process_bs_string_as_ast (attrs : t) : Parsetree.expression option =
251+
let st = ref None in
252+
Ext_list.iter attrs
253253
(fun
254-
(({txt ; loc}, _payload ) as attr : attr) ->
254+
(({txt ; loc}, payload ) as attr ) ->
255+
match txt with
256+
| "bs.as"
257+
->
258+
if !st = None then
259+
match Ast_payload.is_single_string_as_ast payload with
260+
| None ->
261+
Bs_syntaxerr.err loc Expect_string_literal
262+
| Some _ as v ->
263+
Bs_ast_invariant.mark_used_bs_attribute attr ;
264+
st:= v
265+
else
266+
Bs_syntaxerr.err loc Duplicated_bs_as
267+
| _ -> ()
268+
) ;
269+
!st
270+
271+
let has_bs_optional (attrs : t) : bool =
272+
Ext_list.exists attrs (fun
273+
(({txt ; }, _ ) as attr) ->
255274
match txt with
256275
| "bs.optional"
257276
->
258277
Bs_ast_invariant.mark_used_bs_attribute attr ;
259278
true
260279
| _ -> false
261-
) attrs
280+
)
262281

263282

264283

@@ -283,11 +302,11 @@ let iter_process_bs_int_as attrs =
283302
) attrs; !st
284303

285304

286-
let iter_process_bs_string_or_int_as attrs =
305+
let iter_process_bs_string_or_int_as (attrs : Parsetree.attributes) =
287306
let st = ref None in
288-
List.iter
307+
Ext_list.iter attrs
289308
(fun
290-
(({txt ; loc}, payload ) as attr : attr) ->
309+
(({txt ; loc}, payload ) as attr) ->
291310
match txt with
292311
| "bs.as"
293312
->
@@ -311,7 +330,7 @@ let iter_process_bs_string_or_int_as attrs =
311330
Bs_syntaxerr.err loc Duplicated_bs_as
312331
| _ -> ()
313332

314-
) attrs;
333+
) ;
315334
!st
316335

317336
let locg = Location.none

jscomp/syntax/ast_attributes.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ val iter_process_bs_string_int_unwrap_uncurry :
6262
val iter_process_bs_string_as :
6363
t -> string option
6464

65+
val iter_process_bs_string_as_ast :
66+
t ->
67+
Parsetree.expression option
68+
6569
val has_bs_optional :
6670
t -> bool
6771

jscomp/syntax/ast_compatible.ml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ open Parsetree
2828
let default_loc = Location.none
2929

3030
#if OCAML_VERSION =~ ">4.03.0" then
31+
type poly_var_label = Asttypes.label Asttypes.loc
32+
3133
type arg_label = Asttypes.arg_label =
3234
| Nolabel
3335
| Labelled of string
@@ -37,6 +39,7 @@ let is_arg_label_simple (s : arg_label) = s = (Nolabel : arg_label)
3739
type label = arg_label
3840
external convert : arg_label -> label = "%identity"
3941
#else
42+
type poly_var_label = string
4043
type arg_label = string
4144
type label =
4245
| Nolabel
@@ -323,4 +326,13 @@ type object_field =
323326
let object_field l attrs ty =
324327
#if OCAML_VERSION =~ ">4.03.0" then
325328
Parsetree.Otag
326-
#end (l,attrs,ty)
329+
#end (l,attrs,ty)
330+
331+
332+
#if OCAML_VERSION =~ ">4.03.0" then
333+
let hash_label (x : poly_var_label) : int = Ext_pervasives.hash_variant x.txt
334+
let label_of_name (x : poly_var_label) : string = x.txt
335+
#else
336+
let hash_label : poly_var_label -> int = Ext_pervasives.hash_variant
337+
external label_of_name : poly_var_label -> string = "%identity"
338+
#end

0 commit comments

Comments
 (0)