Skip to content

Commit 8510c84

Browse files
committed
not dump the undefined value of optional field
in the inlined record
1 parent 7987f47 commit 8510c84

10 files changed

+102
-74
lines changed

jscomp/core/js_dump.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,12 @@ and expression_desc cxt ~(level : int) f x : cxt =
766766
in
767767
if p.num_nonconst = 1 && not !Js_config.debug then
768768
pp_comment_option f (Some p.name);
769+
let objs = if List.length p.optional_labels <> 0 then
770+
Ext_list.array_list_filter_map p.fields el (fun f x ->
771+
match x.expression_desc with
772+
| Undefined when List.mem f p.optional_labels -> None
773+
| _ -> Some (Js_op.Lit f, x))
774+
else objs in
769775
expression_desc cxt ~level f (Object objs)
770776
| Caml_block (el, _, tag, Blk_constructor p) ->
771777
let not_is_cons = p.name <> Literals.cons in

jscomp/main/builtin_cmj_datasets.ml

Lines changed: 9 additions & 9 deletions
Large diffs are not rendered by default.

jscomp/ml/lambda.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type record_repr =
4040

4141
type tag_info =
4242
| Blk_constructor of {name : string ; num_nonconst : int ; tag : int }
43-
| Blk_record_inlined of { name : string ; num_nonconst : int; tag : int; fields : string array; mutable_flag : Asttypes.mutable_flag }
43+
| Blk_record_inlined of { name : string ; num_nonconst : int; tag : int; optional_labels: string list; fields : string array; mutable_flag : Asttypes.mutable_flag }
4444
| Blk_tuple
4545
| Blk_poly_var of string
4646
| Blk_record of {fields : string array; mutable_flag : Asttypes.mutable_flag; record_repr : record_repr}
@@ -96,9 +96,9 @@ let blk_record_ext = ref (fun fields mutable_flag ->
9696
Blk_record_ext {fields = all_labels_info; mutable_flag }
9797
)
9898

99-
let blk_record_inlined = ref (fun fields name num_nonconst ~tag mutable_flag ->
99+
let blk_record_inlined = ref (fun fields name num_nonconst optional_labels ~tag mutable_flag ->
100100
let fields = fields |> Array.map (fun (x,_) -> x.Types.lbl_name) in
101-
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag}
101+
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag; optional_labels}
102102
)
103103

104104
let ref_tag_info : tag_info =

jscomp/ml/lambda.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type record_repr =
4040

4141
type tag_info =
4242
| Blk_constructor of {name : string ; num_nonconst : int; tag : int}
43-
| Blk_record_inlined of { name : string ; num_nonconst : int ; tag : int; fields : string array; mutable_flag : mutable_flag}
43+
| Blk_record_inlined of { name : string ; num_nonconst : int ; tag : int; optional_labels: string list; fields : string array; mutable_flag : mutable_flag }
4444
| Blk_tuple
4545
| Blk_poly_var of string
4646
| Blk_record of {fields : string array; mutable_flag : mutable_flag; record_repr : record_repr }
@@ -85,6 +85,7 @@ val blk_record_inlined :
8585
(Types.label_description* Typedtree.record_label_definition) array ->
8686
string ->
8787
int ->
88+
string list ->
8889
tag:int ->
8990
mutable_flag ->
9091
tag_info

jscomp/ml/translcore.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,10 +1138,10 @@ and transl_record loc env fields repres opt_init_expr =
11381138
| Record_optional_labels _ ->
11391139
Lconst
11401140
(Const_block (!Lambda.blk_record fields mut Record_optional, cl))
1141-
| Record_inlined { tag; name; num_nonconsts } ->
1141+
| Record_inlined { tag; name; num_nonconsts; optional_labels } ->
11421142
Lconst
11431143
(Const_block
1144-
( !Lambda.blk_record_inlined fields name num_nonconsts ~tag
1144+
( !Lambda.blk_record_inlined fields name num_nonconsts optional_labels ~tag
11451145
mut,
11461146
cl ))
11471147
| Record_unboxed _ ->
@@ -1160,10 +1160,10 @@ and transl_record loc env fields repres opt_init_expr =
11601160
ll,
11611161
loc )
11621162
| Record_float_unused -> assert false
1163-
| Record_inlined { tag; name; num_nonconsts } ->
1163+
| Record_inlined { tag; name; num_nonconsts; optional_labels } ->
11641164
Lprim
11651165
( Pmakeblock
1166-
(!Lambda.blk_record_inlined fields name num_nonconsts ~tag
1166+
(!Lambda.blk_record_inlined fields name num_nonconsts optional_labels ~tag
11671167
mut),
11681168
ll,
11691169
loc )

jscomp/test/record_regression.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ var h = newrecord$2;
8181
var h10 = newrecord$3;
8282

8383
var foo1 = /* Foo */{
84-
name: "foo",
85-
age: undefined
84+
name: "foo"
8685
};
8786

8887
var foo2 = /* Foo */{

lib/4.06.1/unstable/all_ounit_tests.ml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49749,7 +49749,7 @@ type record_repr =
4974949749

4975049750
type tag_info =
4975149751
| Blk_constructor of {name : string ; num_nonconst : int; tag : int}
49752-
| Blk_record_inlined of { name : string ; num_nonconst : int ; tag : int; fields : string array; mutable_flag : mutable_flag}
49752+
| Blk_record_inlined of { name : string ; num_nonconst : int ; tag : int; optional_labels: string list; fields : string array; mutable_flag : mutable_flag }
4975349753
| Blk_tuple
4975449754
| Blk_poly_var of string
4975549755
| Blk_record of {fields : string array; mutable_flag : mutable_flag; record_repr : record_repr }
@@ -49794,6 +49794,7 @@ val blk_record_inlined :
4979449794
(Types.label_description* Typedtree.record_label_definition) array ->
4979549795
string ->
4979649796
int ->
49797+
string list ->
4979749798
tag:int ->
4979849799
mutable_flag ->
4979949800
tag_info
@@ -50144,7 +50145,7 @@ type record_repr =
5014450145

5014550146
type tag_info =
5014650147
| Blk_constructor of {name : string ; num_nonconst : int ; tag : int }
50147-
| Blk_record_inlined of { name : string ; num_nonconst : int; tag : int; fields : string array; mutable_flag : Asttypes.mutable_flag }
50148+
| Blk_record_inlined of { name : string ; num_nonconst : int; tag : int; optional_labels: string list; fields : string array; mutable_flag : Asttypes.mutable_flag }
5014850149
| Blk_tuple
5014950150
| Blk_poly_var of string
5015050151
| Blk_record of {fields : string array; mutable_flag : Asttypes.mutable_flag; record_repr : record_repr}
@@ -50200,9 +50201,9 @@ let blk_record_ext = ref (fun fields mutable_flag ->
5020050201
Blk_record_ext {fields = all_labels_info; mutable_flag }
5020150202
)
5020250203

50203-
let blk_record_inlined = ref (fun fields name num_nonconst ~tag mutable_flag ->
50204+
let blk_record_inlined = ref (fun fields name num_nonconst optional_labels ~tag mutable_flag ->
5020450205
let fields = fields |> Array.map (fun (x,_) -> x.Types.lbl_name) in
50205-
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag}
50206+
Blk_record_inlined {fields; name; num_nonconst; tag; mutable_flag; optional_labels}
5020650207
)
5020750208

5020850209
let ref_tag_info : tag_info =

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 24 additions & 17 deletions
Large diffs are not rendered by default.

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 24 additions & 17 deletions
Large diffs are not rendered by default.

lib/4.06.1/whole_compiler.ml

Lines changed: 24 additions & 17 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)