Skip to content

Commit 82fc9c8

Browse files
committed
refactor away the need for res.inlineRecordReference
1 parent e77ea5c commit 82fc9c8

File tree

5 files changed

+25
-38
lines changed

5 files changed

+25
-38
lines changed

compiler/syntax/src/res_core.ml

+14-9
Original file line numberDiff line numberDiff line change
@@ -4080,12 +4080,19 @@ and parse_atomic_typ_expr ?current_type_name_path ?inline_types ~attrs p =
40804080
~constr_name:constr p
40814081
in
40824082
let number_of_inline_records_in_args =
4083-
args
4084-
|> List.filter (fun (c : Parsetree.core_type) ->
4085-
c.ptyp_attributes
4086-
|> List.exists (fun (({txt}, _) : Parsetree.attribute) ->
4087-
txt = "res.inlineRecordReference"))
4088-
|> List.length
4083+
match inline_types with
4084+
| None -> 0
4085+
| Some inline_types ->
4086+
let inline_types = !inline_types in
4087+
args
4088+
|> List.filter (fun (c : Parsetree.core_type) ->
4089+
match c.ptyp_desc with
4090+
| Ptyp_constr ({txt = Lident typename}, _)
4091+
when String.contains typename '.' ->
4092+
inline_types
4093+
|> List.exists (fun (name, _, _) -> name = typename)
4094+
| _ -> false)
4095+
|> List.length
40894096
in
40904097
if number_of_inline_records_in_args > 1 then
40914098
Parser.err ~start_pos ~end_pos:p.prev_end_pos p
@@ -4196,9 +4203,7 @@ and parse_record_or_object_type ?current_type_name_path ?inline_types ~attrs p =
41964203
(inline_type_name, loc, Parsetree.Ptype_record labels) :: !inline_types;
41974204

41984205
let lid = Location.mkloc (Longident.Lident inline_type_name) loc in
4199-
Ast_helper.Typ.constr
4200-
~attrs:[(Location.mknoloc "res.inlineRecordReference", PStr [])]
4201-
~loc lid []
4206+
Ast_helper.Typ.constr ~loc lid []
42024207
| _ ->
42034208
let () =
42044209
match p.token with

compiler/syntax/src/res_parsetree_viewer.ml

+3-12
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,6 @@ let has_inline_record_definition_attribute attrs =
7979
| _ -> false)
8080
attrs
8181

82-
let has_inline_record_reference_attribute attrs =
83-
List.exists
84-
(function
85-
| {Location.txt = "res.inlineRecordReference"}, _ -> true
86-
| _ -> false)
87-
attrs
88-
8982
let has_res_pat_variant_spread_attribute attrs =
9083
List.exists
9184
(function
@@ -213,7 +206,7 @@ let filter_parsing_attrs attrs =
213206
( "res.braces" | "ns.braces" | "res.iflet" | "res.ternary"
214207
| "res.await" | "res.template" | "res.taggedTemplate"
215208
| "res.patVariantSpread" | "res.dictPattern"
216-
| "res.inlineRecordReference" | "res.inlineRecordDefinition" );
209+
| "res.inlineRecordDefinition" );
217210
},
218211
_ ) ->
219212
false
@@ -367,8 +360,7 @@ let has_attributes attrs =
367360
| ( {
368361
Location.txt =
369362
( "res.braces" | "ns.braces" | "res.iflet" | "res.ternary"
370-
| "res.await" | "res.template" | "res.inlineRecordReference"
371-
| "res.inlineRecordDefinition" );
363+
| "res.await" | "res.template" | "res.inlineRecordDefinition" );
372364
},
373365
_ ) ->
374366
false
@@ -563,8 +555,7 @@ let is_printable_attribute attr =
563555
| ( {
564556
Location.txt =
565557
( "res.iflet" | "res.braces" | "ns.braces" | "JSX" | "res.await"
566-
| "res.template" | "res.ternary" | "res.inlineRecordReference"
567-
| "res.inlineRecordDefinition" );
558+
| "res.template" | "res.ternary" | "res.inlineRecordDefinition" );
568559
},
569560
_ ) ->
570561
false

compiler/syntax/src/res_parsetree_viewer.mli

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ val functor_type :
1616

1717
val has_await_attribute : Parsetree.attributes -> bool
1818
val has_inline_record_definition_attribute : Parsetree.attributes -> bool
19-
val has_inline_record_reference_attribute : Parsetree.attributes -> bool
2019
val has_res_pat_variant_spread_attribute : Parsetree.attributes -> bool
2120
val has_dict_pattern_attribute : Parsetree.attributes -> bool
2221

compiler/syntax/src/res_printer.ml

+6-13
Original file line numberDiff line numberDiff line change
@@ -550,16 +550,6 @@ module State = struct
550550
let should_break_callback t = t.custom_layout > custom_layout_threshold
551551
end
552552

553-
let is_inline_record_definition attrs =
554-
attrs
555-
|> List.exists (fun (({txt}, _) : Parsetree.attribute) ->
556-
txt = "res.inlineRecordDefinition")
557-
558-
let is_inline_record_reference attrs =
559-
attrs
560-
|> List.exists (fun (({txt}, _) : Parsetree.attribute) ->
561-
txt = "res.inlineRecordReference")
562-
563553
let rec print_structure ~state (s : Parsetree.structure) t =
564554
match s with
565555
| [] -> print_comments_inside_file t
@@ -582,12 +572,14 @@ and print_structure_item ~state (si : Parsetree.structure_item) cmt_tbl =
582572
| Pstr_type (Recursive, type_declarations)
583573
when type_declarations
584574
|> List.find_opt (fun (td : Parsetree.type_declaration) ->
585-
is_inline_record_definition td.ptype_attributes)
575+
Res_parsetree_viewer.has_inline_record_definition_attribute
576+
td.ptype_attributes)
586577
|> Option.is_some ->
587578
let inline_record_definitions, regular_declarations =
588579
type_declarations
589580
|> List.partition (fun (td : Parsetree.type_declaration) ->
590-
is_inline_record_definition td.ptype_attributes)
581+
Res_parsetree_viewer.has_inline_record_definition_attribute
582+
td.ptype_attributes)
591583
in
592584
print_type_declarations ~inline_record_definitions ~state
593585
~rec_flag:
@@ -1722,7 +1714,8 @@ and print_typ_expr ?inline_record_definitions ~(state : State.t)
17221714
print_object ~state ~inline:false fields open_flag cmt_tbl
17231715
| Ptyp_arrow {arity} -> print_arrow ~arity typ_expr
17241716
| Ptyp_constr ({txt = Lident inline_record_name}, [])
1725-
when is_inline_record_reference typ_expr.ptyp_attributes -> (
1717+
when inline_record_definitions |> Option.is_some
1718+
&& String.contains inline_record_name '.' -> (
17261719
let inline_record_definitions =
17271720
match inline_record_definitions with
17281721
| None -> []

tests/syntax_tests/data/parsing/errors/typeDef/expected/inlineRecord.res.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ type nonrec entity =
3030
type user.address = {
3131
street: string ;
3232
country: string }[@@res.inlineRecordDefinition ]
33-
and user =
34-
{
33+
and user = {
3534
name: string ;
36-
address: ((user.address)[@res.inlineRecordReference ]) }
35+
address: user.address }
3736
let make
3837
[arity:1](props :
3938
< handleClick: Click.t -> unit (a:1) ;value: string > )

0 commit comments

Comments
 (0)