@@ -1568,6 +1568,78 @@ and printLabelDeclaration ~customLayout (ld : Parsetree.label_declaration)
1568
1568
])
1569
1569
1570
1570
and printTypExpr ~customLayout (typExpr : Parsetree.core_type ) cmtTbl =
1571
+ let printArrow ~uncurried typExpr =
1572
+ let attrsBefore, args, returnType = ParsetreeViewer. arrowType typExpr in
1573
+ let returnTypeNeedsParens =
1574
+ match returnType.ptyp_desc with
1575
+ | Ptyp_alias _ -> true
1576
+ | _ -> false
1577
+ in
1578
+ let returnDoc =
1579
+ let doc = printTypExpr ~custom Layout returnType cmtTbl in
1580
+ if returnTypeNeedsParens then Doc. concat [Doc. lparen; doc; Doc. rparen]
1581
+ else doc
1582
+ in
1583
+ match args with
1584
+ | [] -> Doc. nil
1585
+ | [([] , Nolabel , n)] when not uncurried ->
1586
+ let hasAttrsBefore = not (attrsBefore = [] ) in
1587
+ let attrs =
1588
+ if hasAttrsBefore then
1589
+ printAttributes ~custom Layout ~inline: true attrsBefore cmtTbl
1590
+ else Doc. nil
1591
+ in
1592
+ let typDoc =
1593
+ let doc = printTypExpr ~custom Layout n cmtTbl in
1594
+ match n.ptyp_desc with
1595
+ | Ptyp_arrow _ | Ptyp_tuple _ | Ptyp_alias _ -> addParens doc
1596
+ | _ -> doc
1597
+ in
1598
+ Doc. group
1599
+ (Doc. concat
1600
+ [
1601
+ Doc. group attrs;
1602
+ Doc. group
1603
+ (if hasAttrsBefore then
1604
+ Doc. concat
1605
+ [
1606
+ Doc. lparen;
1607
+ Doc. indent
1608
+ (Doc. concat
1609
+ [Doc. softLine; typDoc; Doc. text " => " ; returnDoc]);
1610
+ Doc. softLine;
1611
+ Doc. rparen;
1612
+ ]
1613
+ else Doc. concat [typDoc; Doc. text " => " ; returnDoc]);
1614
+ ])
1615
+ | args ->
1616
+ let attrs =
1617
+ printAttributes ~custom Layout ~inline: true attrsBefore cmtTbl
1618
+ in
1619
+ let renderedArgs =
1620
+ Doc. concat
1621
+ [
1622
+ attrs;
1623
+ Doc. text " (" ;
1624
+ Doc. indent
1625
+ (Doc. concat
1626
+ [
1627
+ Doc. softLine;
1628
+ (if uncurried then Doc. concat [Doc. dot; Doc. space]
1629
+ else Doc. nil);
1630
+ Doc. join
1631
+ ~sep: (Doc. concat [Doc. comma; Doc. line])
1632
+ (List. map
1633
+ (fun tp -> printTypeParameter ~custom Layout tp cmtTbl)
1634
+ args);
1635
+ ]);
1636
+ Doc. trailingComma;
1637
+ Doc. softLine;
1638
+ Doc. text " )" ;
1639
+ ]
1640
+ in
1641
+ Doc. group (Doc. concat [renderedArgs; Doc. text " => " ; returnDoc])
1642
+ in
1571
1643
let renderedType =
1572
1644
match typExpr.ptyp_desc with
1573
1645
| Ptyp_any -> Doc. text " _"
@@ -1594,6 +1666,11 @@ and printTypExpr ~customLayout (typExpr : Parsetree.core_type) cmtTbl =
1594
1666
(* object printings *)
1595
1667
| Ptyp_object (fields , openFlag ) ->
1596
1668
printObject ~custom Layout ~inline: false fields openFlag cmtTbl
1669
+ | Ptyp_arrow _ -> printArrow ~uncurried: false typExpr
1670
+ | Ptyp_constr ({txt = Ldot (Ldot (Lident " Js" , " Fn" ), arity)}, [tArg])
1671
+ when String. length arity > = 5
1672
+ && (String. sub [@ doesNotRaise]) arity 0 5 = " arity" ->
1673
+ printArrow ~uncurried: true tArg
1597
1674
| Ptyp_constr (longidentLoc, [{ptyp_desc = Ptyp_object (fields, openFlag)}])
1598
1675
->
1599
1676
(* for foo<{"a": b}>, when the object is long and needs a line break, we
@@ -1641,78 +1718,6 @@ and printTypExpr ~customLayout (typExpr : Parsetree.core_type) cmtTbl =
1641
1718
Doc. softLine;
1642
1719
Doc. greaterThan;
1643
1720
]))
1644
- | Ptyp_arrow _ -> (
1645
- let attrsBefore, args, returnType = ParsetreeViewer. arrowType typExpr in
1646
- let returnTypeNeedsParens =
1647
- match returnType.ptyp_desc with
1648
- | Ptyp_alias _ -> true
1649
- | _ -> false
1650
- in
1651
- let returnDoc =
1652
- let doc = printTypExpr ~custom Layout returnType cmtTbl in
1653
- if returnTypeNeedsParens then Doc. concat [Doc. lparen; doc; Doc. rparen]
1654
- else doc
1655
- in
1656
- let isUncurried, attrs =
1657
- ParsetreeViewer. processUncurriedAttribute attrsBefore
1658
- in
1659
- match args with
1660
- | [] -> Doc. nil
1661
- | [([] , Nolabel , n)] when not isUncurried ->
1662
- let hasAttrsBefore = not (attrs = [] ) in
1663
- let attrs =
1664
- if hasAttrsBefore then
1665
- printAttributes ~custom Layout ~inline: true attrsBefore cmtTbl
1666
- else Doc. nil
1667
- in
1668
- let typDoc =
1669
- let doc = printTypExpr ~custom Layout n cmtTbl in
1670
- match n.ptyp_desc with
1671
- | Ptyp_arrow _ | Ptyp_tuple _ | Ptyp_alias _ -> addParens doc
1672
- | _ -> doc
1673
- in
1674
- Doc. group
1675
- (Doc. concat
1676
- [
1677
- Doc. group attrs;
1678
- Doc. group
1679
- (if hasAttrsBefore then
1680
- Doc. concat
1681
- [
1682
- Doc. lparen;
1683
- Doc. indent
1684
- (Doc. concat
1685
- [Doc. softLine; typDoc; Doc. text " => " ; returnDoc]);
1686
- Doc. softLine;
1687
- Doc. rparen;
1688
- ]
1689
- else Doc. concat [typDoc; Doc. text " => " ; returnDoc]);
1690
- ])
1691
- | args ->
1692
- let attrs = printAttributes ~custom Layout ~inline: true attrs cmtTbl in
1693
- let renderedArgs =
1694
- Doc. concat
1695
- [
1696
- attrs;
1697
- Doc. text " (" ;
1698
- Doc. indent
1699
- (Doc. concat
1700
- [
1701
- Doc. softLine;
1702
- (if isUncurried then Doc. concat [Doc. dot; Doc. space]
1703
- else Doc. nil);
1704
- Doc. join
1705
- ~sep: (Doc. concat [Doc. comma; Doc. line])
1706
- (List. map
1707
- (fun tp -> printTypeParameter ~custom Layout tp cmtTbl)
1708
- args);
1709
- ]);
1710
- Doc. trailingComma;
1711
- Doc. softLine;
1712
- Doc. text " )" ;
1713
- ]
1714
- in
1715
- Doc. group (Doc. concat [renderedArgs; Doc. text " => " ; returnDoc]))
1716
1721
| Ptyp_tuple types ->
1717
1722
printTupleType ~custom Layout ~inline: false types cmtTbl
1718
1723
| Ptyp_poly ([] , typ ) -> printTypExpr ~custom Layout typ cmtTbl
@@ -1912,10 +1917,6 @@ and printObjectField ~customLayout (field : Parsetree.object_field) cmtTbl =
1912
1917
* type t = (~foo: string, ~bar: float=?, unit) => unit
1913
1918
* i.e. ~foo: string, ~bar: float *)
1914
1919
and printTypeParameter ~customLayout (attrs , lbl , typ ) cmtTbl =
1915
- let isUncurried, attrs = ParsetreeViewer. processUncurriedAttribute attrs in
1916
- let uncurried =
1917
- if isUncurried then Doc. concat [Doc. dot; Doc. space] else Doc. nil
1918
- in
1919
1920
let attrs = printAttributes ~custom Layout attrs cmtTbl in
1920
1921
let label =
1921
1922
match lbl with
@@ -1941,11 +1942,7 @@ and printTypeParameter ~customLayout (attrs, lbl, typ) cmtTbl =
1941
1942
Doc. group
1942
1943
(Doc. concat
1943
1944
[
1944
- uncurried;
1945
- attrs;
1946
- label;
1947
- printTypExpr ~custom Layout typ cmtTbl;
1948
- optionalIndicator;
1945
+ attrs; label; printTypExpr ~custom Layout typ cmtTbl; optionalIndicator;
1949
1946
])
1950
1947
in
1951
1948
printComments doc cmtTbl loc
0 commit comments