Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit d7f80a4

Browse files
committed
parse attributes for function and arg respectively
- print attributes respectively
1 parent 5868f41 commit d7f80a4

File tree

9 files changed

+98
-95
lines changed

9 files changed

+98
-95
lines changed

src/res_core.ml

+12-14
Original file line numberDiff line numberDiff line change
@@ -1534,12 +1534,6 @@ and parseParameter p =
15341534
then
15351535
let startPos = p.Parser.startPos in
15361536
let uncurried = Parser.optional p Token.Dot in
1537-
(* two scenarios:
1538-
* attrs ~lbl ...
1539-
* attrs pattern
1540-
* Attributes before a labelled arg, indicate that it's on the whole arrow expr
1541-
* Otherwise it's part of the pattern
1542-
* *)
15431537
let attrs = parseAttributes p in
15441538
if p.Parser.token = Typ then (
15451539
Parser.next p;
@@ -1557,9 +1551,9 @@ and parseParameter p =
15571551
match p.Parser.token with
15581552
| Comma | Equal | Rparen ->
15591553
let loc = mkLoc startPos p.prevEndPos in
1560-
( attrs,
1554+
( [],
15611555
Asttypes.Labelled lblName,
1562-
Ast_helper.Pat.var ~attrs:[propLocAttr] ~loc
1556+
Ast_helper.Pat.var ~attrs:([propLocAttr] @ attrs) ~loc
15631557
(Location.mkloc lblName loc) )
15641558
| Colon ->
15651559
let lblEnd = p.prevEndPos in
@@ -1569,25 +1563,29 @@ and parseParameter p =
15691563
let pat =
15701564
let pat = Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) in
15711565
let loc = mkLoc startPos p.prevEndPos in
1572-
Ast_helper.Pat.constraint_ ~attrs:[propLocAttr] ~loc pat typ
1566+
Ast_helper.Pat.constraint_ ~attrs:([propLocAttr] @ attrs) ~loc pat
1567+
typ
15731568
in
1574-
(attrs, Asttypes.Labelled lblName, pat)
1569+
([], Asttypes.Labelled lblName, pat)
15751570
| As ->
15761571
Parser.next p;
15771572
let pat =
15781573
let pat = parseConstrainedPattern p in
1579-
{pat with ppat_attributes = propLocAttr :: pat.ppat_attributes}
1574+
{
1575+
pat with
1576+
ppat_attributes = (propLocAttr :: attrs) @ pat.ppat_attributes;
1577+
}
15801578
in
1581-
(attrs, Asttypes.Labelled lblName, pat)
1579+
([], Asttypes.Labelled lblName, pat)
15821580
| t ->
15831581
Parser.err p (Diagnostics.unexpected t p.breadcrumbs);
15841582
let loc = mkLoc startPos p.prevEndPos in
1585-
( attrs,
1583+
( [],
15861584
Asttypes.Labelled lblName,
15871585
Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) ))
15881586
| _ ->
15891587
let pattern = parseConstrainedPattern p in
1590-
let attrs = List.concat [attrs; pattern.ppat_attributes] in
1588+
let attrs = List.concat [pattern.ppat_attributes; attrs] in
15911589
([], Asttypes.Nolabel, {pattern with ppat_attributes = attrs})
15921590
in
15931591
match p.Parser.token with

src/res_parsetree_viewer.ml

+10-32
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ let funExpr expr =
136136
collectNewTypes (stringLoc :: acc) returnExpr
137137
| returnExpr -> (List.rev acc, returnExpr)
138138
in
139-
let rec collect n attrsBefore acc expr =
139+
let rec collect attrsBefore acc expr =
140140
match expr with
141141
| {
142142
pexp_desc =
@@ -147,48 +147,26 @@ let funExpr expr =
147147
{pexp_desc = Pexp_apply _} );
148148
} ->
149149
(attrsBefore, List.rev acc, rewriteUnderscoreApply expr)
150-
| {
151-
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
152-
pexp_attributes = [];
153-
} ->
154-
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
155-
collect (n + 1) attrsBefore (parameter :: acc) returnExpr
156150
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
157151
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
158152
let param = NewTypes {attrs; locs = stringLocs} in
159-
collect (n + 1) attrsBefore (param :: acc) returnExpr
160-
| {pexp_desc = Pexp_fun _; pexp_attributes}
161-
when pexp_attributes
162-
|> List.exists (fun ({Location.txt}, _) ->
163-
txt = "bs" || txt = "res.async")
164-
&& n > 0 ->
165-
(* stop here, the uncurried or async attribute always indicates the beginning of an arrow function
166-
* e.g. `(. a) => (. b)` instead of `(. a, . b)` *)
167-
(attrsBefore, List.rev acc, expr)
153+
collect attrsBefore (param :: acc) returnExpr
168154
| {
169-
pexp_desc =
170-
Pexp_fun
171-
(((Labelled _ | Optional _) as lbl), defaultExpr, pattern, returnExpr);
172-
pexp_attributes = attrs;
155+
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
156+
pexp_attributes = [];
173157
} ->
174-
(* Normally attributes are attached to the labelled argument, e.g. (@foo ~x) => ...
175-
In the case of `@res.async`, pass the attribute to the outside *)
176-
let attrs_async, attrs_other =
177-
attrs |> List.partition (fun ({Location.txt}, _) -> txt = "res.async")
178-
in
179-
let parameter =
180-
Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
181-
in
182-
collect (n + 1) (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
158+
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
159+
collect attrsBefore (parameter :: acc) returnExpr
160+
| {pexp_desc = Pexp_fun _} -> (attrsBefore, List.rev acc, expr)
183161
| expr -> (attrsBefore, List.rev acc, expr)
184162
in
185163
match expr with
186164
| {
187-
pexp_desc = Pexp_fun (Nolabel, _defaultExpr, _pattern, _returnExpr);
165+
pexp_desc = Pexp_fun (_, _defaultExpr, _pattern, _returnExpr);
188166
pexp_attributes = attrs;
189167
} as expr ->
190-
collect 0 attrs [] {expr with pexp_attributes = []}
191-
| expr -> collect 0 [] [] expr
168+
collect attrs [] {expr with pexp_attributes = []}
169+
| expr -> collect [] [] expr
192170

193171
let processBracesAttr expr =
194172
match expr.pexp_attributes with

src/res_printer.ml

+21-8
Original file line numberDiff line numberDiff line change
@@ -4691,13 +4691,23 @@ and printExprFunParameters ~customLayout ~inCallback ~async ~uncurried
46914691
attrs = [];
46924692
lbl = Asttypes.Nolabel;
46934693
defaultExpr = None;
4694-
pat = {Parsetree.ppat_desc = Ppat_var stringLoc};
4694+
pat =
4695+
{
4696+
Parsetree.ppat_desc = Ppat_var stringLoc;
4697+
Parsetree.ppat_attributes = attrs;
4698+
};
46954699
};
46964700
]
46974701
when not uncurried ->
46984702
let txtDoc =
46994703
let var = printIdentLike stringLoc.txt in
4700-
let var = if hasConstraint then addParens var else var in
4704+
let var =
4705+
match attrs with
4706+
| [] -> if hasConstraint then addParens var else var
4707+
| attrs ->
4708+
let attrs = printAttributes ~customLayout attrs cmtTbl in
4709+
addParens (Doc.concat [attrs; var])
4710+
in
47014711
if async then addAsync var else var
47024712
in
47034713
printComments txtDoc cmtTbl stringLoc.loc
@@ -4788,22 +4798,25 @@ and printExpFunParameter ~customLayout parameter cmtTbl =
47884798
match (lbl, pattern) with
47894799
| Asttypes.Nolabel, pattern -> printPattern ~customLayout pattern cmtTbl
47904800
| ( (Asttypes.Labelled lbl | Optional lbl),
4791-
{
4792-
ppat_desc = Ppat_var stringLoc;
4793-
ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
4794-
} )
4801+
{ppat_desc = Ppat_var stringLoc; ppat_attributes} )
47954802
when lbl = stringLoc.txt ->
47964803
(* ~d *)
4797-
Doc.concat [Doc.text "~"; printIdentLike lbl]
4804+
Doc.concat
4805+
[
4806+
printAttributes ~customLayout ppat_attributes cmtTbl;
4807+
Doc.text "~";
4808+
printIdentLike lbl;
4809+
]
47984810
| ( (Asttypes.Labelled lbl | Optional lbl),
47994811
{
48004812
ppat_desc = Ppat_constraint ({ppat_desc = Ppat_var {txt}}, typ);
4801-
ppat_attributes = [] | [({Location.txt = "ns.namedArgLoc"}, _)];
4813+
ppat_attributes;
48024814
} )
48034815
when lbl = txt ->
48044816
(* ~d: e *)
48054817
Doc.concat
48064818
[
4819+
printAttributes ~customLayout ppat_attributes cmtTbl;
48074820
Doc.text "~";
48084821
printIdentLike lbl;
48094822
Doc.text ": ";

tests/parsing/grammar/expressions/expected/arrow.res.txt

+9-8
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,17 @@ let f = ((fun a -> fun b -> fun c -> ())[@bs ])
4646
let f = ((fun a -> fun b -> ((fun c -> fun d -> ())[@bs ]))[@bs ])
4747
let f = ((fun a -> ((fun b -> ((fun c -> ())[@bs ]))[@bs ]))[@bs ])
4848
let f =
49-
((fun ~a:((a)[@ns.namedArgLoc ]) ->
50-
fun b -> ((fun ~c:((c)[@ns.namedArgLoc ]) -> fun d -> ())
51-
[@bs ][@attr ]))
52-
[@bs ][@attr ])
49+
((fun ~a:((a)[@ns.namedArgLoc ][@attr ]) ->
50+
fun b -> ((fun ~c:((c)[@ns.namedArgLoc ][@attr ]) -> fun d -> ())
51+
[@bs ]))
52+
[@bs ])
5353
let f =
54-
((fun ~a:((a)[@ns.namedArgLoc ]) ->
54+
((fun ~a:((a)[@ns.namedArgLoc ][@attr ]) ->
5555
fun ((b)[@attrOnB ]) ->
56-
((fun ~c:((c)[@ns.namedArgLoc ]) -> fun ((d)[@attrOnD ]) -> ())
57-
[@bs ][@attr ]))
58-
[@bs ][@attr ])
56+
((fun ~c:((c)[@ns.namedArgLoc ][@attr ]) ->
57+
fun ((d)[@attrOnD ]) -> ())
58+
[@bs ]))
59+
[@bs ])
5960
let f list = list ()
6061
;;match colour with
6162
| Red when

tests/ppx/react/expected/fileLevelConfig.res.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ module V3 = {
55

66
@react.component
77
let make =
8-
(@warning("-16") ~msg) => {
8+
@warning("-16")
9+
(~msg) => {
910
ReactDOMRe.createDOMElementVariadic("div", [{msg->React.string}])
1011
}
1112
let make = {

tests/ppx/react/expected/forwardRef.res.txt

+20-17
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,28 @@ module V3 = {
1313

1414
@react.component
1515
let make =
16-
(@warning("-16") ~className=?, @warning("-16") ~children) =>
16+
@warning("-16")
17+
(~className=?) =>
1718
@warning("-16")
18-
ref =>
19-
ReactDOMRe.createDOMElementVariadic(
20-
"div",
21-
[
22-
ReactDOMRe.createDOMElementVariadic(
23-
"input",
24-
~props=ReactDOMRe.domProps(
25-
~type_="text",
26-
~className?,
27-
~ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(ReactDOM.Ref.domRef)},
28-
(),
19+
(~children) =>
20+
@warning("-16")
21+
ref =>
22+
ReactDOMRe.createDOMElementVariadic(
23+
"div",
24+
[
25+
ReactDOMRe.createDOMElementVariadic(
26+
"input",
27+
~props=ReactDOMRe.domProps(
28+
~type_="text",
29+
~className?,
30+
~ref=?{Js.Nullable.toOption(ref)->Belt.Option.map(ReactDOM.Ref.domRef)},
31+
(),
32+
),
33+
[],
2934
),
30-
[],
31-
),
32-
children,
33-
],
34-
)
35+
children,
36+
],
37+
)
3538
let make = React.forwardRef({
3639
let \"ForwardRef$V3$FancyInput" = (
3740
\"Props": {"className": option<'className>, "children": 'children},

tests/ppx/react/expected/innerModule.res.txt

+14-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ module Bar = {
44

55
@react.component
66
let make =
7-
(@warning("-16") ~a, @warning("-16") ~b, _) => {
8-
Js.log("This function should be named `InnerModule.react$Bar`")
9-
ReactDOMRe.createDOMElementVariadic("div", [])
10-
}
7+
@warning("-16")
8+
(~a) =>
9+
@warning("-16")
10+
(~b, _) => {
11+
Js.log("This function should be named `InnerModule.react$Bar`")
12+
ReactDOMRe.createDOMElementVariadic("div", [])
13+
}
1114
let make = {
1215
let \"InnerModule$Bar" = (\"Props": {"a": 'a, "b": 'b}) =>
1316
make(~b=\"Props"["b"], ~a=\"Props"["a"], ())
@@ -17,10 +20,13 @@ module Bar = {
1720

1821
@react.component
1922
let component =
20-
(@warning("-16") ~a, @warning("-16") ~b, _) => {
21-
Js.log("This function should be named `InnerModule.react$Bar$component`")
22-
ReactDOMRe.createDOMElementVariadic("div", [])
23-
}
23+
@warning("-16")
24+
(~a) =>
25+
@warning("-16")
26+
(~b, _) => {
27+
Js.log("This function should be named `InnerModule.react$Bar$component`")
28+
ReactDOMRe.createDOMElementVariadic("div", [])
29+
}
2430
let component = {
2531
let \"InnerModule$Bar$component" = (\"Props": {"a": 'a, "b": 'b}) =>
2632
component(~b=\"Props"["b"], ~a=\"Props"["a"], ())

tests/ppx/react/expected/topLevel.res.txt

+7-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ module V3 = {
55

66
@react.component
77
let make =
8-
(@warning("-16") ~a, @warning("-16") ~b, _) => {
9-
Js.log("This function should be named 'TopLevel.react'")
10-
ReactDOMRe.createDOMElementVariadic("div", [])
11-
}
8+
@warning("-16")
9+
(~a) =>
10+
@warning("-16")
11+
(~b, _) => {
12+
Js.log("This function should be named 'TopLevel.react'")
13+
ReactDOMRe.createDOMElementVariadic("div", [])
14+
}
1215
let make = {
1316
let \"TopLevel$V3" = (\"Props": {"a": 'a, "b": 'b}) =>
1417
make(~b=\"Props"["b"], ~a=\"Props"["a"], ())

tests/printer/expr/expected/asyncAwait.res.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ let _ = await {
106106

107107
let f1 = async (~x, ~y) => x + y
108108
let f2 = async (@foo ~x, @bar ~y) => x + y
109-
let f3 = async (@bar @foo ~x as @zz z, ~y) => x + y
109+
let f3 = @foo async (~x as @bar @zz z, ~y) => x + y
110110
let f4 = async x => x
111111
let f5 = async x => async y => 3
112112
let f6 = async (~x1, ~x2) => async y => 3
@@ -116,5 +116,5 @@ let f9 = x => async (~y) => 3
116116
let f10 = x => async y => 3
117117
let f11 = (. ~x) => (. ~y) => 3
118118

119-
let f12 = @a x => 3
120-
let f13 = (@a @b ~x) => 3
119+
let f12 = @a (@b x) => 3
120+
let f13 = @a @b (~x) => 3

0 commit comments

Comments
 (0)