Skip to content

Commit e112fec

Browse files
committed
change optional field in cstr arg to option type
1 parent dbdb2c5 commit e112fec

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
@@ -37960,6 +37960,20 @@ let transl_declaration env sdecl id =
3796037960
| (_,_,loc)::_ ->
3796137961
Location.prerr_warning loc Warnings.Constraint_on_gadt
3796237962
end;
37963+
let has_optional attrs = Ext_list.exists attrs (fun ({txt },_) -> txt = "ns.optional") in
37964+
let scstrs =
37965+
Ext_list.map scstrs (fun ({pcd_args} as cstr) ->
37966+
match pcd_args with
37967+
| Pcstr_tuple _ -> cstr
37968+
| Pcstr_record lds ->
37969+
{cstr with pcd_args = Pcstr_record (Ext_list.map lds (fun ld ->
37970+
if has_optional ld.pld_attributes then
37971+
let typ = ld.pld_type in
37972+
let typ = {typ with ptyp_desc = Ptyp_constr ({txt = Lident "option"; loc=typ.ptyp_loc}, [typ])} in
37973+
{ld with pld_type = typ}
37974+
else ld
37975+
))}
37976+
) in
3796337977
let all_constrs = ref StringSet.empty in
3796437978
List.iter
3796537979
(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
@@ -37960,6 +37960,20 @@ let transl_declaration env sdecl id =
3796037960
| (_,_,loc)::_ ->
3796137961
Location.prerr_warning loc Warnings.Constraint_on_gadt
3796237962
end;
37963+
let has_optional attrs = Ext_list.exists attrs (fun ({txt },_) -> txt = "ns.optional") in
37964+
let scstrs =
37965+
Ext_list.map scstrs (fun ({pcd_args} as cstr) ->
37966+
match pcd_args with
37967+
| Pcstr_tuple _ -> cstr
37968+
| Pcstr_record lds ->
37969+
{cstr with pcd_args = Pcstr_record (Ext_list.map lds (fun ld ->
37970+
if has_optional ld.pld_attributes then
37971+
let typ = ld.pld_type in
37972+
let typ = {typ with ptyp_desc = Ptyp_constr ({txt = Lident "option"; loc=typ.ptyp_loc}, [typ])} in
37973+
{ld with pld_type = typ}
37974+
else ld
37975+
))}
37976+
) in
3796337977
let all_constrs = ref StringSet.empty in
3796437978
List.iter
3796537979
(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
@@ -214132,6 +214132,20 @@ let transl_declaration env sdecl id =
214132214132
| (_,_,loc)::_ ->
214133214133
Location.prerr_warning loc Warnings.Constraint_on_gadt
214134214134
end;
214135+
let has_optional attrs = Ext_list.exists attrs (fun ({txt },_) -> txt = "ns.optional") in
214136+
let scstrs =
214137+
Ext_list.map scstrs (fun ({pcd_args} as cstr) ->
214138+
match pcd_args with
214139+
| Pcstr_tuple _ -> cstr
214140+
| Pcstr_record lds ->
214141+
{cstr with pcd_args = Pcstr_record (Ext_list.map lds (fun ld ->
214142+
if has_optional ld.pld_attributes then
214143+
let typ = ld.pld_type in
214144+
let typ = {typ with ptyp_desc = Ptyp_constr ({txt = Lident "option"; loc=typ.ptyp_loc}, [typ])} in
214145+
{ld with pld_type = typ}
214146+
else ld
214147+
))}
214148+
) in
214135214149
let all_constrs = ref StringSet.empty in
214136214150
List.iter
214137214151
(fun {pcd_name = {txt = name}} ->

0 commit comments

Comments
 (0)