@@ -49677,7 +49677,7 @@ let funExpr expr =
49677
49677
collectNewTypes (stringLoc :: acc) returnExpr
49678
49678
| returnExpr -> (List.rev acc, returnExpr)
49679
49679
in
49680
- let rec collect n attrsBefore acc expr =
49680
+ let rec collect attrsBefore acc expr =
49681
49681
match expr with
49682
49682
| {
49683
49683
pexp_desc =
@@ -49688,48 +49688,26 @@ let funExpr expr =
49688
49688
{pexp_desc = Pexp_apply _} );
49689
49689
} ->
49690
49690
(attrsBefore, List.rev acc, rewriteUnderscoreApply expr)
49691
- | {
49692
- pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
49693
- pexp_attributes = [];
49694
- } ->
49695
- let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49696
- collect (n + 1) attrsBefore (parameter :: acc) returnExpr
49697
49691
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
49698
49692
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
49699
49693
let param = NewTypes {attrs; locs = stringLocs} in
49700
- collect (n + 1) attrsBefore (param :: acc) returnExpr
49701
- | {pexp_desc = Pexp_fun _; pexp_attributes}
49702
- when pexp_attributes
49703
- |> List.exists (fun ({Location.txt}, _) ->
49704
- txt = "bs" || txt = "res.async")
49705
- && n > 0 ->
49706
- (* stop here, the uncurried or async attribute always indicates the beginning of an arrow function
49707
- * e.g. `(. a) => (. b)` instead of `(. a, . b)` *)
49708
- (attrsBefore, List.rev acc, expr)
49694
+ collect attrsBefore (param :: acc) returnExpr
49709
49695
| {
49710
- pexp_desc =
49711
- Pexp_fun
49712
- (((Labelled _ | Optional _) as lbl), defaultExpr, pattern, returnExpr);
49713
- pexp_attributes = attrs;
49696
+ pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
49697
+ pexp_attributes = [];
49714
49698
} ->
49715
- (* Normally attributes are attached to the labelled argument, e.g. (@foo ~x) => ...
49716
- In the case of `@res.async`, pass the attribute to the outside *)
49717
- let attrs_async, attrs_other =
49718
- attrs |> List.partition (fun ({Location.txt}, _) -> txt = "res.async")
49719
- in
49720
- let parameter =
49721
- Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
49722
- in
49723
- collect (n + 1) (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
49699
+ let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49700
+ collect attrsBefore (parameter :: acc) returnExpr
49701
+ | {pexp_desc = Pexp_fun _} -> (attrsBefore, List.rev acc, expr)
49724
49702
| expr -> (attrsBefore, List.rev acc, expr)
49725
49703
in
49726
49704
match expr with
49727
49705
| {
49728
- pexp_desc = Pexp_fun (Nolabel , _defaultExpr, _pattern, _returnExpr);
49706
+ pexp_desc = Pexp_fun (_ , _defaultExpr, _pattern, _returnExpr);
49729
49707
pexp_attributes = attrs;
49730
49708
} as expr ->
49731
- collect 0 attrs [] {expr with pexp_attributes = []}
49732
- | expr -> collect 0 [] [] expr
49709
+ collect attrs [] {expr with pexp_attributes = []}
49710
+ | expr -> collect [] [] expr
49733
49711
49734
49712
let processBracesAttr expr =
49735
49713
match expr.pexp_attributes with
@@ -57920,13 +57898,23 @@ and printExprFunParameters ~customLayout ~inCallback ~async ~uncurried
57920
57898
attrs = [];
57921
57899
lbl = Asttypes.Nolabel;
57922
57900
defaultExpr = None;
57923
- pat = {Parsetree.ppat_desc = Ppat_var stringLoc};
57901
+ pat =
57902
+ {
57903
+ Parsetree.ppat_desc = Ppat_var stringLoc;
57904
+ Parsetree.ppat_attributes = attrs;
57905
+ };
57924
57906
};
57925
57907
]
57926
57908
when not uncurried ->
57927
57909
let txtDoc =
57928
57910
let var = printIdentLike stringLoc.txt in
57929
- let var = if hasConstraint then addParens var else var in
57911
+ let var =
57912
+ match attrs with
57913
+ | [] -> if hasConstraint then addParens var else var
57914
+ | attrs ->
57915
+ let attrs = printAttributes ~customLayout attrs cmtTbl in
57916
+ addParens (Doc.concat [attrs; var])
57917
+ in
57930
57918
if async then addAsync var else var
57931
57919
in
57932
57920
printComments txtDoc cmtTbl stringLoc.loc
@@ -58017,22 +58005,25 @@ and printExpFunParameter ~customLayout parameter cmtTbl =
58017
58005
match (lbl, pattern) with
58018
58006
| Asttypes.Nolabel, pattern -> printPattern ~customLayout pattern cmtTbl
58019
58007
| ( (Asttypes.Labelled lbl | Optional lbl),
58020
- {
58021
- ppat_desc = Ppat_var stringLoc;
58022
- ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
58023
- } )
58008
+ {ppat_desc = Ppat_var stringLoc; ppat_attributes} )
58024
58009
when lbl = stringLoc.txt ->
58025
58010
(* ~d *)
58026
- Doc.concat [Doc.text "~"; printIdentLike lbl]
58011
+ Doc.concat
58012
+ [
58013
+ printAttributes ~customLayout ppat_attributes cmtTbl;
58014
+ Doc.text "~";
58015
+ printIdentLike lbl;
58016
+ ]
58027
58017
| ( (Asttypes.Labelled lbl | Optional lbl),
58028
58018
{
58029
58019
ppat_desc = Ppat_constraint ({ppat_desc = Ppat_var {txt}}, typ);
58030
- ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)] ;
58020
+ ppat_attributes;
58031
58021
} )
58032
58022
when lbl = txt ->
58033
58023
(* ~d: e *)
58034
58024
Doc.concat
58035
58025
[
58026
+ printAttributes ~customLayout ppat_attributes cmtTbl;
58036
58027
Doc.text "~";
58037
58028
printIdentLike lbl;
58038
58029
Doc.text ": ";
@@ -284712,6 +284703,12 @@ and parseTernaryExpr leftOperand p =
284712
284703
and parseEs6ArrowExpression ?context ?parameters p =
284713
284704
let startPos = p.Parser.startPos in
284714
284705
Parser.leaveBreadcrumb p Grammar.Es6ArrowExpr;
284706
+ (* Parsing function parameters and attributes:
284707
+ 1. Basically, attributes outside of `(...)` are added to the function, except
284708
+ the uncurried attribute `(.)` is added to the function. e.g. async, uncurried
284709
+
284710
+ 2. Attributes inside `(...)` are added to the arguments regardless of whether
284711
+ labeled, optional or nolabeled *)
284715
284712
let parameters =
284716
284713
match parameters with
284717
284714
| Some params -> params
@@ -284785,12 +284782,6 @@ and parseParameter p =
284785
284782
then
284786
284783
let startPos = p.Parser.startPos in
284787
284784
let uncurried = Parser.optional p Token.Dot in
284788
- (* two scenarios:
284789
- * attrs ~lbl ...
284790
- * attrs pattern
284791
- * Attributes before a labelled arg, indicate that it's on the whole arrow expr
284792
- * Otherwise it's part of the pattern
284793
- * *)
284794
284785
let attrs = parseAttributes p in
284795
284786
if p.Parser.token = Typ then (
284796
284787
Parser.next p;
@@ -284808,9 +284799,9 @@ and parseParameter p =
284808
284799
match p.Parser.token with
284809
284800
| Comma | Equal | Rparen ->
284810
284801
let loc = mkLoc startPos p.prevEndPos in
284811
- ( attrs ,
284802
+ ( [] ,
284812
284803
Asttypes.Labelled lblName,
284813
- Ast_helper.Pat.var ~attrs:[ propLocAttr] ~loc
284804
+ Ast_helper.Pat.var ~attrs:( propLocAttr :: attrs) ~loc
284814
284805
(Location.mkloc lblName loc) )
284815
284806
| Colon ->
284816
284807
let lblEnd = p.prevEndPos in
@@ -284820,25 +284811,29 @@ and parseParameter p =
284820
284811
let pat =
284821
284812
let pat = Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) in
284822
284813
let loc = mkLoc startPos p.prevEndPos in
284823
- Ast_helper.Pat.constraint_ ~attrs:[propLocAttr] ~loc pat typ
284814
+ Ast_helper.Pat.constraint_ ~attrs:(propLocAttr :: attrs) ~loc pat
284815
+ typ
284824
284816
in
284825
284817
(attrs, Asttypes.Labelled lblName, pat)
284826
284818
| As ->
284827
284819
Parser.next p;
284828
284820
let pat =
284829
284821
let pat = parseConstrainedPattern p in
284830
- {pat with ppat_attributes = propLocAttr :: pat.ppat_attributes}
284822
+ {
284823
+ pat with
284824
+ ppat_attributes = (propLocAttr :: attrs) @ pat.ppat_attributes;
284825
+ }
284831
284826
in
284832
- (attrs , Asttypes.Labelled lblName, pat)
284827
+ ([] , Asttypes.Labelled lblName, pat)
284833
284828
| t ->
284834
284829
Parser.err p (Diagnostics.unexpected t p.breadcrumbs);
284835
284830
let loc = mkLoc startPos p.prevEndPos in
284836
- ( attrs ,
284831
+ ( [] ,
284837
284832
Asttypes.Labelled lblName,
284838
284833
Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) ))
284839
284834
| _ ->
284840
284835
let pattern = parseConstrainedPattern p in
284841
- let attrs = List.concat [attrs; pattern.ppat_attributes] in
284836
+ let attrs = List.concat [pattern.ppat_attributes; attrs ] in
284842
284837
([], Asttypes.Nolabel, {pattern with ppat_attributes = attrs})
284843
284838
in
284844
284839
match p.Parser.token with
0 commit comments