Skip to content

Commit d307ebb

Browse files
committed
part 2: fix #3142
1 parent c6943ca commit d307ebb

20 files changed

+10775
-12321
lines changed

jscomp/all.depend

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ 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
178179
syntax/ast_utf8_string_interp.cmx : ext/ext_utf8.cmx ext/ext_string.cmx \
@@ -213,9 +214,9 @@ syntax/ast_attributes.cmx : ext/ext_string.cmx ext/ext_list.cmx \
213214
syntax/bs_syntaxerr.cmx syntax/bs_ast_invariant.cmx \
214215
syntax/ast_payload.cmx syntax/ast_compatible.cmx \
215216
syntax/ast_attributes.cmi
216-
syntax/ast_polyvar.cmx : syntax/external_arg_spec.cmx ext/ext_pervasives.cmx \
217-
ext/ext_list.cmx syntax/bs_syntaxerr.cmx syntax/ast_attributes.cmx \
218-
syntax/ast_polyvar.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
219220
syntax/external_ffi_types.cmx : syntax/external_arg_spec.cmx \
220221
ext/ext_string.cmx ext/ext_pervasives.cmx common/bs_version.cmx \
221222
syntax/external_ffi_types.cmi
@@ -242,7 +243,7 @@ syntax/ast_derive_projector.cmx : ext/ext_string.cmx ext/ext_list.cmx \
242243
syntax/ast_derive_js_mapper.cmx : ext/ext_list.cmx syntax/ast_polyvar.cmx \
243244
syntax/ast_literal.cmx syntax/ast_derive_util.cmx syntax/ast_derive.cmx \
244245
syntax/ast_core_type.cmx syntax/ast_compatible.cmx syntax/ast_comb.cmx \
245-
syntax/ast_derive_js_mapper.cmi
246+
syntax/ast_attributes.cmx syntax/ast_derive_js_mapper.cmi
246247
syntax/ast_util.cmx : ext/literals.cmx syntax/external_process.cmx \
247248
ext/ext_string.cmx ext/ext_list.cmx syntax/bs_syntaxerr.cmx \
248249
syntax/bs_ast_mapper.cmx syntax/ast_payload.cmx syntax/ast_pat.cmx \

jscomp/bin/all_ounit_tests.ml

Lines changed: 22 additions & 9 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-
3803-
3804-
val unescaped_js_delimiter : string
38053801

38063802
val native : string
38073803
val bytecode : string
@@ -3932,8 +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"
39373931

39383932
let native = "native"
39393933
let bytecode = "bytecode"
@@ -14609,6 +14603,7 @@ module Ast_compatible : sig
1460914603
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
1461014604

1461114605

14606+
type poly_var_label = string
1461214607
type arg_label = string
1461314608
type label =
1461414609
| Nolabel
@@ -14769,7 +14764,8 @@ type object_field =
1476914764
val object_field : string -> attributes -> core_type -> object_field
1477014765

1477114766

14772-
14767+
val hash_label : poly_var_label -> int
14768+
val label_of_name : poly_var_label -> string
1477314769
end = struct
1477414770
#1 "ast_compatible.ml"
1477514771
(* Copyright (C) 2018 Authors of BuckleScript
@@ -14802,6 +14798,7 @@ open Parsetree
1480214798
let default_loc = Location.none
1480314799

1480414800

14801+
type poly_var_label = string
1480514802
type arg_label = string
1480614803
type label =
1480714804
| Nolabel
@@ -15022,6 +15019,12 @@ type object_field =
1502215019

1502315020
let object_field l attrs ty =
1502415021
(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+
1502515028
end
1502615029
module Bs_loc : sig
1502715030
#1 "bs_loc.mli"
@@ -15187,6 +15190,10 @@ val transform :
1518715190
val is_unicode_string :
1518815191
string ->
1518915192
bool
15193+
15194+
val is_unescaped :
15195+
string ->
15196+
bool
1519015197
end = struct
1519115198
#1 "ast_utf8_string_interp.ml"
1519215199
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -15576,6 +15583,8 @@ let to_string_ident : Longident.t =
1557615583

1557715584

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

1558015589
let escaped = Some escaped_j_delimiter
1558115590

@@ -15648,19 +15657,23 @@ let transform_interp loc s =
1564815657

1564915658

1565015659
let transform (e : Parsetree.expression) s delim : Parsetree.expression =
15651-
if Ext_string.equal delim Literals.unescaped_js_delimiter then
15660+
if Ext_string.equal delim unescaped_js_delimiter then
1565215661
let js_str = Ast_utf8_string.transform e.pexp_loc s in
1565315662
{ e with pexp_desc =
1565415663
Pexp_constant (
1565515664

1565615665
Const_string
1565715666

1565815667
(js_str, escaped))}
15659-
else if Ext_string.equal delim Literals.unescaped_j_delimiter then
15668+
else if Ext_string.equal delim unescaped_j_delimiter then
1566015669
transform_interp e.pexp_loc s
1566115670
else e
1566215671

1566315672
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
1566415677
end
1566515678
module Ounit_unicode_tests
1566615679
= struct

jscomp/syntax/ast_attributes.ml

Lines changed: 25 additions & 5 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,6 +247,27 @@ let iter_process_bs_string_as (attrs : t) : string option =
248247
) ;
249248
!st
250249

250+
let iter_process_bs_string_as_ast (attrs : t) : Parsetree.expression option =
251+
let st = ref None in
252+
Ext_list.iter attrs
253+
(fun
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+
251271
let has_bs_optional (attrs : t) : bool =
252272
Ext_list.exists attrs (fun
253273
(({txt ; }, _ ) as attr) ->

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

jscomp/syntax/ast_compatible.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

2525
#if OCAML_VERSION =~ ">4.3.0" then
26+
type poly_var_label = Asttypes.label Asttypes.loc
2627
type arg_label = Asttypes.arg_label
2728
type label = arg_label
2829
external convert: arg_label -> label = "%identity"
2930
#else
31+
type poly_var_label = string
3032
type arg_label = string
3133
type label =
3234
| Nolabel
@@ -190,4 +192,5 @@ val object_field : Asttypes.label Asttypes.loc -> attributes -> core_type -> ob
190192
val object_field : string -> attributes -> core_type -> object_field
191193
#end
192194

193-
195+
val hash_label : poly_var_label -> int
196+
val label_of_name : poly_var_label -> string

jscomp/syntax/ast_derive_js_mapper.ml

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -242,64 +242,70 @@ let init () =
242242
if createType then eraseTypeStr:: newTypeStr :: rest else rest
243243
| Ptype_abstract ->
244244
(match Ast_polyvar.is_enum_polyvar tdcl with
245-
| Some row_fields ->
246-
let attr =
247-
Ast_polyvar.map_row_fields_into_strings loc row_fields
248-
in
245+
| Some row_fields ->
249246
let expConstantArray =
250247
Exp.ident {loc; txt = Longident.Lident constantArray} in
251-
begin match attr with
252-
| NullString result ->
253-
let result_len = List.length result in
254-
let exp_len = Ast_compatible.const_exp_int result_len in
255-
let v = [
256-
eraseTypeStr;
257-
Ast_comb.single_non_rec_value
258-
{loc; txt = constantArray}
259-
(Exp.array
260-
(Ext_list.map (List.sort (fun (a,_) (b,_) -> compare (a:int) b) result)
261-
(fun (i,str) ->
262-
Exp.tuple
263-
[
264-
Ast_compatible.const_exp_int i;
265-
Ast_compatible.const_exp_string str
266-
]
267-
) ));
268-
(
269-
toJsBody
270-
(coerceResultToNewType
271-
(search
272-
exp_len
273-
exp_param
274-
expConstantArray
275-
))
276-
);
277-
Ast_comb.single_non_rec_value
278-
patFromJs
279-
(Ast_compatible.fun_
280-
(Pat.var pat_param)
281-
(if createType then
282-
revSearchAssert
283-
exp_len
284-
expConstantArray
285-
(exp_param +: newType)
286-
+>
287-
core_type
288-
else
289-
revSearch
290-
exp_len
291-
expConstantArray
292-
exp_param
293-
+>
294-
Ast_core_type.lift_option_type core_type
295-
)
248+
let result : _ list =
249+
Ext_list.map row_fields (fun tag ->
250+
match tag with
251+
| Rtag (label, attrs, _, []) ->
252+
(Ast_compatible.hash_label label,
253+
match Ast_attributes.iter_process_bs_string_as_ast attrs with
254+
| Some name ->
255+
name
256+
| None ->
257+
Ast_compatible.const_exp_string(Ast_compatible.label_of_name label)
296258
)
297-
] in
298-
if createType then
299-
newTypeStr :: v
300-
else v
301-
| _ -> assert false
302-
end
259+
| _ -> assert false (* checked by [is_enum_polyvar] *)
260+
) in
261+
let result_len = List.length result in
262+
let exp_len = Ast_compatible.const_exp_int result_len in
263+
let v = [
264+
eraseTypeStr;
265+
Ast_comb.single_non_rec_value
266+
{loc; txt = constantArray}
267+
(Exp.array
268+
(Ext_list.map (List.sort (fun (a,_) (b,_) -> compare (a:int) b) result)
269+
(fun (i,str) ->
270+
Exp.tuple
271+
[
272+
Ast_compatible.const_exp_int i;
273+
str
274+
]
275+
) ));
276+
(
277+
toJsBody
278+
(coerceResultToNewType
279+
(search
280+
exp_len
281+
exp_param
282+
expConstantArray
283+
))
284+
);
285+
Ast_comb.single_non_rec_value
286+
patFromJs
287+
(Ast_compatible.fun_
288+
(Pat.var pat_param)
289+
(if createType then
290+
revSearchAssert
291+
exp_len
292+
expConstantArray
293+
(exp_param +: newType)
294+
+>
295+
core_type
296+
else
297+
revSearch
298+
exp_len
299+
expConstantArray
300+
exp_param
301+
+>
302+
Ast_core_type.lift_option_type core_type
303+
)
304+
)
305+
] in
306+
if createType then
307+
newTypeStr :: v
308+
else v
303309
| None ->
304310
U.notApplicable
305311
tdcl.Parsetree.ptype_loc

0 commit comments

Comments
 (0)