Skip to content

Commit ce3d532

Browse files
committed
reuse more logic
1 parent da56f8e commit ce3d532

File tree

1 file changed

+11
-26
lines changed

1 file changed

+11
-26
lines changed

jscomp/ml/variant_coercion.ml

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,28 @@
1-
let find_attribute_payload name (attributes : Parsetree.attribute list) =
2-
attributes
3-
|> List.find_map (fun (attr : Parsetree.attribute) ->
4-
match attr with
5-
| {txt}, payload when txt = name -> Some payload
6-
| _ -> None)
7-
8-
let find_as_attribute_payload (attributes : Parsetree.attribute list) =
9-
find_attribute_payload "as" attributes
10-
111
(* TODO: Improve error messages? Say why we can't coerce. *)
122

133
let check_constructors (constructors : Types.constructor_declaration list) check
144
=
155
List.for_all
166
(fun (c : Types.constructor_declaration) ->
17-
check c.cd_args (find_as_attribute_payload c.cd_attributes))
7+
check c.cd_args (Ast_untagged_variants.process_tag_type c.cd_attributes))
188
constructors
199

2010
let can_coerce_to_string (constructors : Types.constructor_declaration list) =
2111
check_constructors constructors (fun args payload ->
2212
match (args, payload) with
23-
| Cstr_tuple [], None -> true
24-
| Cstr_tuple [], Some payload
25-
when Ast_payload.is_single_string payload |> Option.is_some ->
26-
true
13+
| Cstr_tuple [], (None | Some (String _)) -> true
2714
| _ -> false)
2815

2916
let can_coerce_to_int (constructors : Types.constructor_declaration list) =
3017
check_constructors constructors (fun args payload ->
3118
match (args, payload) with
32-
| Cstr_tuple [], Some payload
33-
when Ast_payload.is_single_int payload |> Option.is_some ->
34-
true
19+
| Cstr_tuple [], Some (Int _) -> true
3520
| _ -> false)
3621

3722
let can_coerce_to_float (constructors : Types.constructor_declaration list) =
3823
check_constructors constructors (fun args payload ->
3924
match (args, payload) with
40-
| Cstr_tuple [], Some payload
41-
when Ast_payload.is_single_float payload |> Option.is_some ->
42-
true
25+
| Cstr_tuple [], Some (Float _) -> true
4326
| _ -> false)
4427

4528
let can_coerce_path (path : Path.t) =
@@ -66,7 +49,8 @@ let is_variant_typedecl
6649
let variant_representation_matches (c1_attrs : Parsetree.attributes)
6750
(c2_attrs : Parsetree.attributes) =
6851
match
69-
(Ast_untagged_variants.process_tag_type c1_attrs, Ast_untagged_variants.process_tag_type c2_attrs)
52+
( Ast_untagged_variants.process_tag_type c1_attrs,
53+
Ast_untagged_variants.process_tag_type c2_attrs )
7054
with
7155
| None, None -> true
7256
| Some s1, Some s2 when s1 = s2 -> true
@@ -76,7 +60,8 @@ let variant_configuration_can_be_coerced (a1 : Parsetree.attributes)
7660
(a2 : Parsetree.attributes) =
7761
let unboxed =
7862
match
79-
(Ast_untagged_variants.process_untagged a1, Ast_untagged_variants.process_untagged a2)
63+
( Ast_untagged_variants.process_untagged a1,
64+
Ast_untagged_variants.process_untagged a2 )
8065
with
8166
| true, true | false, false -> true
8267
| _ -> false
@@ -85,11 +70,11 @@ let variant_configuration_can_be_coerced (a1 : Parsetree.attributes)
8570
else
8671
let tag =
8772
match
88-
(Ast_untagged_variants.process_tag_name a1,
73+
( Ast_untagged_variants.process_tag_name a1,
8974
Ast_untagged_variants.process_tag_name a2 )
9075
with
91-
| Some (tag1), Some (tag2) when tag1 = tag2 -> true
76+
| Some tag1, Some tag2 when tag1 = tag2 -> true
9277
| None, None -> true
9378
| _ -> false
9479
in
95-
if not tag then false else true
80+
if not tag then false else true

0 commit comments

Comments
 (0)