Skip to content

Commit 7c0a343

Browse files
committed
Add support for printing @optional records.
See corresponding PR in syntax: https://github.com/rescript-lang/syntax/actions/runs/2542072235
1 parent cada027 commit 7c0a343

File tree

7 files changed

+41
-31
lines changed

7 files changed

+41
-31
lines changed

jscomp/ml/oprint.ml

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@ let cautious f ppf arg =
2222
try f ppf arg with
2323
Ellipsis -> fprintf ppf "..."
2424

25-
2625
let out_ident = ref pp_print_string
2726
let map_primitive_name = ref (fun x -> x)
2827

29-
3028
let print_lident ppf = function
3129
| "::" -> !out_ident ppf "(::)"
3230
| s -> !out_ident ppf s
@@ -294,7 +292,7 @@ and print_simple_out_type ppf =
294292
| Otyp_constr (Oide_dot (Oide_dot (Oide_ident "Js_OO", "Callback" ), _),
295293
[tyl])
296294
->
297-
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
295+
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
298296
| Otyp_constr (id, tyl) ->
299297
pp_open_box ppf 0;
300298
print_typargs ppf tyl;
@@ -391,8 +389,8 @@ and print_typargs ppf =
391389
pp_print_char ppf ')';
392390
pp_close_box ppf ();
393391
pp_print_space ppf ()
394-
and print_out_label ppf (name, mut, arg) =
395-
fprintf ppf "@[<2>%s%s :@ %a@];" (if mut then "mutable " else "") name
392+
and print_out_label ppf (name, mut, opt, arg) =
393+
fprintf ppf "@[<2>%s%s%s :@ %a@];" (if opt then "@optional " else "") (if mut then "mutable " else "") name
396394
print_out_type arg
397395

398396
let out_type = ref print_out_type

jscomp/ml/outcometree.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type out_type =
6161
| Otyp_constr of out_ident * out_type list
6262
| Otyp_manifest of out_type * out_type
6363
| Otyp_object of (string * out_type) list * bool option
64-
| Otyp_record of (string * bool * out_type) list
64+
| Otyp_record of (string * bool * bool * out_type) list
6565
| Otyp_stuff of string
6666
| Otyp_sum of (string * out_type list * out_type option) list
6767
| Otyp_tuple of out_type list

jscomp/ml/printtyp.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,11 @@ and tree_of_constructor cd =
926926
(name, args, Some ret)
927927

928928
and tree_of_label l =
929-
(Ident.name l.ld_id, l.ld_mutable = Mutable, tree_of_typexp false l.ld_type)
929+
let opt = l.ld_attributes |> List.exists (fun ({txt}, _) -> txt = "optional") in
930+
let typ = match l.ld_type.desc with
931+
| Tconstr (p, [t1], _) when Path.same p Predef.path_option -> t1
932+
| _ -> l.ld_type in
933+
(Ident.name l.ld_id, l.ld_mutable = Mutable, opt, tree_of_typexp false typ)
930934

931935
let tree_of_type_declaration id decl rs =
932936
Osig_type (tree_of_type_decl id decl, tree_of_rec rs)

lib/4.06.1/unstable/js_compiler.ml

+9-7
Original file line numberDiff line numberDiff line change
@@ -5042,7 +5042,7 @@ type out_type =
50425042
| Otyp_constr of out_ident * out_type list
50435043
| Otyp_manifest of out_type * out_type
50445044
| Otyp_object of (string * out_type) list * bool option
5045-
| Otyp_record of (string * bool * out_type) list
5045+
| Otyp_record of (string * bool * bool * out_type) list
50465046
| Otyp_stuff of string
50475047
| Otyp_sum of (string * out_type list * out_type option) list
50485048
| Otyp_tuple of out_type list
@@ -31579,11 +31579,9 @@ let cautious f ppf arg =
3157931579
try f ppf arg with
3158031580
Ellipsis -> fprintf ppf "..."
3158131581

31582-
3158331582
let out_ident = ref pp_print_string
3158431583
let map_primitive_name = ref (fun x -> x)
3158531584

31586-
3158731585
let print_lident ppf = function
3158831586
| "::" -> !out_ident ppf "(::)"
3158931587
| s -> !out_ident ppf s
@@ -31851,7 +31849,7 @@ and print_simple_out_type ppf =
3185131849
| Otyp_constr (Oide_dot (Oide_dot (Oide_ident "Js_OO", "Callback" ), _),
3185231850
[tyl])
3185331851
->
31854-
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
31852+
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
3185531853
| Otyp_constr (id, tyl) ->
3185631854
pp_open_box ppf 0;
3185731855
print_typargs ppf tyl;
@@ -31948,8 +31946,8 @@ and print_typargs ppf =
3194831946
pp_print_char ppf ')';
3194931947
pp_close_box ppf ();
3195031948
pp_print_space ppf ()
31951-
and print_out_label ppf (name, mut, arg) =
31952-
fprintf ppf "@[<2>%s%s :@ %a@];" (if mut then "mutable " else "") name
31949+
and print_out_label ppf (name, mut, opt, arg) =
31950+
fprintf ppf "@[<2>%s%s%s :@ %a@];" (if opt then "@optional " else "") (if mut then "mutable " else "") name
3195331951
print_out_type arg
3195431952

3195531953
let out_type = ref print_out_type
@@ -33363,7 +33361,11 @@ and tree_of_constructor cd =
3336333361
(name, args, Some ret)
3336433362

3336533363
and tree_of_label l =
33366-
(Ident.name l.ld_id, l.ld_mutable = Mutable, tree_of_typexp false l.ld_type)
33364+
let opt = l.ld_attributes |> List.exists (fun ({txt}, _) -> txt = "optional") in
33365+
let typ = match l.ld_type.desc with
33366+
| Tconstr (p, [t1], _) when Path.same p Predef.path_option -> t1
33367+
| _ -> l.ld_type in
33368+
(Ident.name l.ld_id, l.ld_mutable = Mutable, opt, tree_of_typexp false typ)
3336733369

3336833370
let tree_of_type_declaration id decl rs =
3336933371
Osig_type (tree_of_type_decl id decl, tree_of_rec rs)

lib/4.06.1/unstable/js_playground_compiler.ml

+11-8
Original file line numberDiff line numberDiff line change
@@ -5042,7 +5042,7 @@ type out_type =
50425042
| Otyp_constr of out_ident * out_type list
50435043
| Otyp_manifest of out_type * out_type
50445044
| Otyp_object of (string * out_type) list * bool option
5045-
| Otyp_record of (string * bool * out_type) list
5045+
| Otyp_record of (string * bool * bool * out_type) list
50465046
| Otyp_stuff of string
50475047
| Otyp_sum of (string * out_type list * out_type option) list
50485048
| Otyp_tuple of out_type list
@@ -31579,11 +31579,9 @@ let cautious f ppf arg =
3157931579
try f ppf arg with
3158031580
Ellipsis -> fprintf ppf "..."
3158131581

31582-
3158331582
let out_ident = ref pp_print_string
3158431583
let map_primitive_name = ref (fun x -> x)
3158531584

31586-
3158731585
let print_lident ppf = function
3158831586
| "::" -> !out_ident ppf "(::)"
3158931587
| s -> !out_ident ppf s
@@ -31851,7 +31849,7 @@ and print_simple_out_type ppf =
3185131849
| Otyp_constr (Oide_dot (Oide_dot (Oide_ident "Js_OO", "Callback" ), _),
3185231850
[tyl])
3185331851
->
31854-
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
31852+
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
3185531853
| Otyp_constr (id, tyl) ->
3185631854
pp_open_box ppf 0;
3185731855
print_typargs ppf tyl;
@@ -31948,8 +31946,8 @@ and print_typargs ppf =
3194831946
pp_print_char ppf ')';
3194931947
pp_close_box ppf ();
3195031948
pp_print_space ppf ()
31951-
and print_out_label ppf (name, mut, arg) =
31952-
fprintf ppf "@[<2>%s%s :@ %a@];" (if mut then "mutable " else "") name
31949+
and print_out_label ppf (name, mut, opt, arg) =
31950+
fprintf ppf "@[<2>%s%s%s :@ %a@];" (if opt then "@optional " else "") (if mut then "mutable " else "") name
3195331951
print_out_type arg
3195431952

3195531953
let out_type = ref print_out_type
@@ -33363,7 +33361,11 @@ and tree_of_constructor cd =
3336333361
(name, args, Some ret)
3336433362

3336533363
and tree_of_label l =
33366-
(Ident.name l.ld_id, l.ld_mutable = Mutable, tree_of_typexp false l.ld_type)
33364+
let opt = l.ld_attributes |> List.exists (fun ({txt}, _) -> txt = "optional") in
33365+
let typ = match l.ld_type.desc with
33366+
| Tconstr (p, [t1], _) when Path.same p Predef.path_option -> t1
33367+
| _ -> l.ld_type in
33368+
(Ident.name l.ld_id, l.ld_mutable = Mutable, opt, tree_of_typexp false typ)
3336733369

3336833370
let tree_of_type_declaration id decl rs =
3336933371
Osig_type (tree_of_type_decl id decl, tree_of_rec rs)
@@ -287635,10 +287637,11 @@ and printOutConstructorDoc (name, args, gadt) =
287635287637
in
287636287638
Doc.group (Doc.concat [Doc.text name; argsDoc; gadtDoc])
287637287639

287638-
and printRecordDeclRowDoc (name, mut, arg) =
287640+
and printRecordDeclRowDoc (name, mut, opt, arg) =
287639287641
Doc.group
287640287642
(Doc.concat
287641287643
[
287644+
(if opt then Doc.text "@optional " else Doc.nil);
287642287645
(if mut then Doc.text "mutable " else Doc.nil);
287643287646
printIdentLike ~allowUident:false name;
287644287647
Doc.text ": ";

lib/4.06.1/whole_compiler.ml

+11-8
Original file line numberDiff line numberDiff line change
@@ -151102,7 +151102,7 @@ type out_type =
151102151102
| Otyp_constr of out_ident * out_type list
151103151103
| Otyp_manifest of out_type * out_type
151104151104
| Otyp_object of (string * out_type) list * bool option
151105-
| Otyp_record of (string * bool * out_type) list
151105+
| Otyp_record of (string * bool * bool * out_type) list
151106151106
| Otyp_stuff of string
151107151107
| Otyp_sum of (string * out_type list * out_type option) list
151108151108
| Otyp_tuple of out_type list
@@ -206110,11 +206110,9 @@ let cautious f ppf arg =
206110206110
try f ppf arg with
206111206111
Ellipsis -> fprintf ppf "..."
206112206112

206113-
206114206113
let out_ident = ref pp_print_string
206115206114
let map_primitive_name = ref (fun x -> x)
206116206115

206117-
206118206116
let print_lident ppf = function
206119206117
| "::" -> !out_ident ppf "(::)"
206120206118
| s -> !out_ident ppf s
@@ -206382,7 +206380,7 @@ and print_simple_out_type ppf =
206382206380
| Otyp_constr (Oide_dot (Oide_dot (Oide_ident "Js_OO", "Callback" ), _),
206383206381
[tyl])
206384206382
->
206385-
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
206383+
fprintf ppf "@[<0>(%a@ [@bs.this])@]" print_out_type_1 tyl
206386206384
| Otyp_constr (id, tyl) ->
206387206385
pp_open_box ppf 0;
206388206386
print_typargs ppf tyl;
@@ -206479,8 +206477,8 @@ and print_typargs ppf =
206479206477
pp_print_char ppf ')';
206480206478
pp_close_box ppf ();
206481206479
pp_print_space ppf ()
206482-
and print_out_label ppf (name, mut, arg) =
206483-
fprintf ppf "@[<2>%s%s :@ %a@];" (if mut then "mutable " else "") name
206480+
and print_out_label ppf (name, mut, opt, arg) =
206481+
fprintf ppf "@[<2>%s%s%s :@ %a@];" (if opt then "@optional " else "") (if mut then "mutable " else "") name
206484206482
print_out_type arg
206485206483

206486206484
let out_type = ref print_out_type
@@ -207894,7 +207892,11 @@ and tree_of_constructor cd =
207894207892
(name, args, Some ret)
207895207893

207896207894
and tree_of_label l =
207897-
(Ident.name l.ld_id, l.ld_mutable = Mutable, tree_of_typexp false l.ld_type)
207895+
let opt = l.ld_attributes |> List.exists (fun ({txt}, _) -> txt = "optional") in
207896+
let typ = match l.ld_type.desc with
207897+
| Tconstr (p, [t1], _) when Path.same p Predef.path_option -> t1
207898+
| _ -> l.ld_type in
207899+
(Ident.name l.ld_id, l.ld_mutable = Mutable, opt, tree_of_typexp false typ)
207898207900

207899207901
let tree_of_type_declaration id decl rs =
207900207902
Osig_type (tree_of_type_decl id decl, tree_of_rec rs)
@@ -294279,10 +294281,11 @@ and printOutConstructorDoc (name, args, gadt) =
294279294281
in
294280294282
Doc.group (Doc.concat [Doc.text name; argsDoc; gadtDoc])
294281294283

294282-
and printRecordDeclRowDoc (name, mut, arg) =
294284+
and printRecordDeclRowDoc (name, mut, opt, arg) =
294283294285
Doc.group
294284294286
(Doc.concat
294285294287
[
294288+
(if opt then Doc.text "@optional " else Doc.nil);
294286294289
(if mut then Doc.text "mutable " else Doc.nil);
294287294290
printIdentLike ~allowUident:false name;
294288294291
Doc.text ": ";

0 commit comments

Comments
 (0)