Skip to content

Commit eaed46d

Browse files
committed
change optional field in cstr arg to option type
1 parent 76a736c commit eaed46d

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

jscomp/ml/typedecl.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,20 @@ let transl_declaration env sdecl id =
358358
| (_,_,loc)::_ ->
359359
Location.prerr_warning loc Warnings.Constraint_on_gadt
360360
end;
361+
let has_optional attrs = Ext_list.exists attrs (fun ({txt },_) -> txt = "ns.optional") in
362+
let scstrs =
363+
Ext_list.map scstrs (fun ({pcd_args} as cstr) ->
364+
match pcd_args with
365+
| Pcstr_tuple _ -> cstr
366+
| Pcstr_record lds ->
367+
{cstr with pcd_args = Pcstr_record (Ext_list.map lds (fun ld ->
368+
if has_optional ld.pld_attributes then
369+
let typ = ld.pld_type in
370+
let typ = {typ with ptyp_desc = Ptyp_constr ({txt = Lident "option"; loc=typ.ptyp_loc}, [typ])} in
371+
{ld with pld_type = typ}
372+
else ld
373+
))}
374+
) in
361375
let all_constrs = ref StringSet.empty in
362376
List.iter
363377
(fun {pcd_name = {txt = name}} ->

jscomp/test/record_regression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ var h10 = newrecord$3;
8282

8383
var foo = /* Foo */{
8484
name: "foo",
85-
age: undefined
85+
age: 3
8686
};
8787

8888
exports.f1 = f1;

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37956,6 +37956,20 @@ let transl_declaration env sdecl id =
3795637956
| (_,_,loc)::_ ->
3795737957
Location.prerr_warning loc Warnings.Constraint_on_gadt
3795837958
end;
37959+
let has_optional attrs = Ext_list.exists attrs (fun ({txt },_) -> txt = "ns.optional") in
37960+
let scstrs =
37961+
Ext_list.map scstrs (fun ({pcd_args} as cstr) ->
37962+
match pcd_args with
37963+
| Pcstr_tuple _ -> cstr
37964+
| Pcstr_record lds ->
37965+
{cstr with pcd_args = Pcstr_record (Ext_list.map lds (fun ld ->
37966+
if has_optional ld.pld_attributes then
37967+
let typ = ld.pld_type in
37968+
let typ = {typ with ptyp_desc = Ptyp_constr ({txt = Lident "option"; loc=typ.ptyp_loc}, [typ])} in
37969+
{ld with pld_type = typ}
37970+
else ld
37971+
))}
37972+
) in
3795937973
let all_constrs = ref StringSet.empty in
3796037974
List.iter
3796137975
(fun {pcd_name = {txt = name}} ->

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37956,6 +37956,20 @@ let transl_declaration env sdecl id =
3795637956
| (_,_,loc)::_ ->
3795737957
Location.prerr_warning loc Warnings.Constraint_on_gadt
3795837958
end;
37959+
let has_optional attrs = Ext_list.exists attrs (fun ({txt },_) -> txt = "ns.optional") in
37960+
let scstrs =
37961+
Ext_list.map scstrs (fun ({pcd_args} as cstr) ->
37962+
match pcd_args with
37963+
| Pcstr_tuple _ -> cstr
37964+
| Pcstr_record lds ->
37965+
{cstr with pcd_args = Pcstr_record (Ext_list.map lds (fun ld ->
37966+
if has_optional ld.pld_attributes then
37967+
let typ = ld.pld_type in
37968+
let typ = {typ with ptyp_desc = Ptyp_constr ({txt = Lident "option"; loc=typ.ptyp_loc}, [typ])} in
37969+
{ld with pld_type = typ}
37970+
else ld
37971+
))}
37972+
) in
3795937973
let all_constrs = ref StringSet.empty in
3796037974
List.iter
3796137975
(fun {pcd_name = {txt = name}} ->

lib/4.06.1/whole_compiler.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92954,6 +92954,20 @@ let transl_declaration env sdecl id =
9295492954
| (_,_,loc)::_ ->
9295592955
Location.prerr_warning loc Warnings.Constraint_on_gadt
9295692956
end;
92957+
let has_optional attrs = Ext_list.exists attrs (fun ({txt },_) -> txt = "ns.optional") in
92958+
let scstrs =
92959+
Ext_list.map scstrs (fun ({pcd_args} as cstr) ->
92960+
match pcd_args with
92961+
| Pcstr_tuple _ -> cstr
92962+
| Pcstr_record lds ->
92963+
{cstr with pcd_args = Pcstr_record (Ext_list.map lds (fun ld ->
92964+
if has_optional ld.pld_attributes then
92965+
let typ = ld.pld_type in
92966+
let typ = {typ with ptyp_desc = Ptyp_constr ({txt = Lident "option"; loc=typ.ptyp_loc}, [typ])} in
92967+
{ld with pld_type = typ}
92968+
else ld
92969+
))}
92970+
) in
9295792971
let all_constrs = ref StringSet.empty in
9295892972
List.iter
9295992973
(fun {pcd_name = {txt = name}} ->

0 commit comments

Comments
 (0)