Skip to content

Commit 0e3c3c1

Browse files
committed
Another way to do it
1 parent 31228bf commit 0e3c3c1

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

jscomp/syntax/src/res_parsetree_viewer.ml

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let print_ct ct =
2525
| {ptyp_desc = Ptyp_variant _} -> print_endline "Ptyp_variant"
2626
| _ -> print_endline "Something else"
2727

28-
let arrow_type ?(arity = max_int) ct =
28+
let arrow_type ?(arity = max_int) ?(attrs = []) ct =
2929
let has_as_attr attrs =
3030
Ext_list.exists attrs (fun (x, _) -> x.Asttypes.txt = "as")
3131
in
@@ -70,10 +70,11 @@ let arrow_type ?(arity = max_int) ct =
7070
| typ -> (attrs_before, List.rev acc, typ)
7171
in
7272
match ct with
73-
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs} as
74-
typ ->
73+
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = attrs1}
74+
as typ ->
75+
let attrs = List.concat [attrs; attrs1] in
7576
process attrs [] {typ with ptyp_attributes = []} arity
76-
| typ -> process [] [] typ arity
77+
| typ -> process attrs [] typ arity
7778

7879
let functor_type modtype =
7980
let rec process acc modtype =

jscomp/syntax/src/res_parsetree_viewer.mli

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* we restructure the tree into (a, b, c) and its returnType d *)
44
val arrow_type :
55
?arity:int ->
6+
?attrs:Parsetree.attributes ->
67
Parsetree.core_type ->
78
Parsetree.attributes
89
* (Parsetree.attributes * Asttypes.arg_label * Parsetree.core_type) list

jscomp/syntax/src/res_printer.ml

+6-8
Original file line numberDiff line numberDiff line change
@@ -1591,17 +1591,13 @@ and print_label_declaration ~state (ld : Parsetree.label_declaration) cmt_tbl =
15911591
])
15921592

15931593
and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl =
1594-
let parent_has_attrs =
1594+
let parent_attrs =
15951595
let attrs = ParsetreeViewer.filter_parsing_attrs typ_expr.ptyp_attributes in
1596-
if Ast_uncurried.core_type_is_uncurried_fun typ_expr && not (attrs = [])
1597-
then
1598-
let arity, _ = Ast_uncurried.core_type_extract_uncurried_fun typ_expr in
1599-
arity > 0
1600-
else false
1596+
if Ast_uncurried.core_type_is_uncurried_fun typ_expr then attrs else []
16011597
in
16021598
let print_arrow ?(arity = max_int) typ_expr =
16031599
let attrs_before, args, return_type =
1604-
ParsetreeViewer.arrow_type ~arity typ_expr
1600+
ParsetreeViewer.arrow_type ~arity ~attrs:parent_attrs typ_expr
16051601
in
16061602
let return_type_needs_parens =
16071603
match return_type.ptyp_desc with
@@ -1634,7 +1630,7 @@ and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl =
16341630
[
16351631
Doc.group attrs;
16361632
Doc.group
1637-
(if has_attrs_before || parent_has_attrs then
1633+
(if has_attrs_before then
16381634
Doc.concat
16391635
[
16401636
Doc.lparen;
@@ -1849,6 +1845,8 @@ and print_typ_expr ~(state : State.t) (typ_expr : Parsetree.core_type) cmt_tbl =
18491845
in
18501846
let should_print_its_own_attributes =
18511847
match typ_expr.ptyp_desc with
1848+
| Ptyp_constr _ when Ast_uncurried.core_type_is_uncurried_fun typ_expr ->
1849+
true
18521850
| Ptyp_arrow _ (* es6 arrow types print their own attributes *) -> true
18531851
| _ -> false
18541852
in

0 commit comments

Comments
 (0)