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

Commit 4b2a3b4

Browse files
committed
parse attributes for function and arg respectively
1 parent 5868f41 commit 4b2a3b4

File tree

15 files changed

+80
-953
lines changed

15 files changed

+80
-953
lines changed

src/res_core.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,7 @@ and parseParameter p =
15341534
then
15351535
let startPos = p.Parser.startPos in
15361536
let uncurried = Parser.optional p Token.Dot in
1537+
(* FIXME: stale comments *)
15371538
(* two scenarios:
15381539
* attrs ~lbl ...
15391540
* attrs pattern
@@ -1557,9 +1558,9 @@ and parseParameter p =
15571558
match p.Parser.token with
15581559
| Comma | Equal | Rparen ->
15591560
let loc = mkLoc startPos p.prevEndPos in
1560-
( attrs,
1561+
( [],
15611562
Asttypes.Labelled lblName,
1562-
Ast_helper.Pat.var ~attrs:[propLocAttr] ~loc
1563+
Ast_helper.Pat.var ~attrs:(attrs @ [propLocAttr]) ~loc
15631564
(Location.mkloc lblName loc) )
15641565
| Colon ->
15651566
let lblEnd = p.prevEndPos in
@@ -1569,20 +1570,24 @@ and parseParameter p =
15691570
let pat =
15701571
let pat = Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) in
15711572
let loc = mkLoc startPos p.prevEndPos in
1572-
Ast_helper.Pat.constraint_ ~attrs:[propLocAttr] ~loc pat typ
1573+
Ast_helper.Pat.constraint_ ~attrs:(attrs @ [propLocAttr]) ~loc pat
1574+
typ
15731575
in
1574-
(attrs, Asttypes.Labelled lblName, pat)
1576+
([], Asttypes.Labelled lblName, pat)
15751577
| As ->
15761578
Parser.next p;
15771579
let pat =
15781580
let pat = parseConstrainedPattern p in
1579-
{pat with ppat_attributes = propLocAttr :: pat.ppat_attributes}
1581+
{
1582+
pat with
1583+
ppat_attributes = attrs @ (propLocAttr :: pat.ppat_attributes);
1584+
}
15801585
in
1581-
(attrs, Asttypes.Labelled lblName, pat)
1586+
([], Asttypes.Labelled lblName, pat)
15821587
| t ->
15831588
Parser.err p (Diagnostics.unexpected t p.breadcrumbs);
15841589
let loc = mkLoc startPos p.prevEndPos in
1585-
( attrs,
1590+
( [],
15861591
Asttypes.Labelled lblName,
15871592
Ast_helper.Pat.var ~loc (Location.mkloc lblName loc) ))
15881593
| _ ->

src/res_parsetree_viewer.ml

Lines changed: 14 additions & 33 deletions
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,29 @@ 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;
173-
} ->
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")
155+
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
156+
pexp_attributes;
157+
} -> (
158+
let attrs_async_bs, attrs_other =
159+
pexp_attributes
160+
|> List.partition (fun ({Location.txt}, _) ->
161+
txt = "res.async" || txt = "bs")
178162
in
179163
let parameter =
180164
Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
181165
in
182-
collect (n + 1) (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
166+
match attrs_async_bs with
167+
| [] -> collect attrsBefore (parameter :: acc) returnExpr
168+
| attrs_async_bs ->
169+
(attrs_async_bs @ attrsBefore, List.rev (parameter :: acc), expr))
183170
| expr -> (attrsBefore, List.rev acc, expr)
184171
in
185-
match expr with
186-
| {
187-
pexp_desc = Pexp_fun (Nolabel, _defaultExpr, _pattern, _returnExpr);
188-
pexp_attributes = attrs;
189-
} as expr ->
190-
collect 0 attrs [] {expr with pexp_attributes = []}
191-
| expr -> collect 0 [] [] expr
172+
collect [] [] expr
192173

193174
let processBracesAttr expr =
194175
match expr.pexp_attributes with

tests/conversion/reason/expected/bracedJsx.res.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ let make = () => {
110110
<div role="heading" ariaLevel=1 className=Styles.title> {"Erreur"->ReasonReact.string} </div>
111111
<div
112112
className=Styles.terminal
113-
onClick={event => (event->ReactEvent.Mouse.target)["querySelector"]("input")["focus"]()}
113+
onClick={(event) => (event->ReactEvent.Mouse.target)["querySelector"]("input")["focus"]()}
114114
ref={containerRef->ReactDOMRe.Ref.domRef}
115115
>
116116
{state.history
@@ -132,8 +132,8 @@ let make = () => {
132132
className=Styles.input
133133
autoFocus=true
134134
value=state.input
135-
onChange={event => send(SetValue((event->ReactEvent.Form.target)["value"]))}
136-
onKeyDown={event => {
135+
onChange={(event) => send(SetValue((event->ReactEvent.Form.target)["value"]))}
136+
onKeyDown={(event) => {
137137
if event->ReactEvent.Keyboard.key == "Enter" {
138138
send(RunCommand)
139139
}

tests/conversion/reason/expected/jsxProps.res.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ let handleClick = (href, event) =>
66

77
@react.component
88
let make = (~href, ~className="", ~children) =>
9-
<a href className onClick={event => handleClick(href, event)}> children </a>
9+
<a href className onClick={(event) => handleClick(href, event)}> children </a>
1010

11-
<Animated> ...{x => <div />} </Animated>
11+
<Animated> ...{(x) => <div />} </Animated>
1212

1313
<div> ...element </div>
14-
<div> ...{a => 1} </div>
14+
<div> ...{(a) => 1} </div>
1515
<div> ...<span /> </div>
1616
<div> ...[a, b] </div>
1717
<div> ...{(1, 2)} </div>

tests/conversion/reason/expected/uncurrried.res.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ok
2-
let updateBriefletNarrative = (. updateObj) => Js.log("patented merge algorithm goes here")
2+
// let updateBriefletNarrative = (. updateObj) => Js.log("patented merge algorithm goes here")
33

44
// this is a bug in Reason, the . will be parsed wrong and disappear.
55
/* updateBriefletNarrative(. briefletNarrativeUpdateObj); */
@@ -23,7 +23,7 @@ let x = {
2323

2424
let x = {
2525
// ok
26-
let f = (. a, b) => apply(. a + b)
26+
// let f = (. a, b) => apply(. a + b)
2727
let a = 3
2828
// ok
2929
foo(. a)
@@ -44,7 +44,7 @@ let () = switch something(. x, y) {
4444

4545
let () = {
4646
// ok
47-
let dontDoThisAhome = (. a, b) => (. c, d) => (. e, f) => a + b + c + d + e + f
47+
// let dontDoThisAhome = (. a, b, . c, d, . e, f) => a + b + c + d + e + f
4848
// ok
4949
dontDoThisAhome(. a, b)(. c, d)(. e, f)
5050
}

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

Lines changed: 9 additions & 8 deletions
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)[@attr ][@ns.namedArgLoc ]) ->
50+
fun b -> ((fun ~c:((c)[@attr ][@ns.namedArgLoc ]) -> fun d -> ())
51+
[@bs ]))
52+
[@bs ])
5353
let f =
54-
((fun ~a:((a)[@ns.namedArgLoc ]) ->
54+
((fun ~a:((a)[@attr ][@ns.namedArgLoc ]) ->
5555
fun ((b)[@attrOnB ]) ->
56-
((fun ~c:((c)[@ns.namedArgLoc ]) -> fun ((d)[@attrOnD ]) -> ())
57-
[@bs ][@attr ]))
58-
[@bs ][@attr ])
56+
((fun ~c:((c)[@attr ][@ns.namedArgLoc ]) ->
57+
fun ((d)[@attrOnD ]) -> ())
58+
[@bs ]))
59+
[@bs ])
5960
let f list = list ()
6061
;;match colour with
6162
| Red when

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,23 @@ module V3 = {
1313

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ let x = @attrWithLongName @attrWithLongName @attrWithLongName @attrWithLongName
6161
call(~a: int)
6262
call(~\"let": int)
6363

64-
document.createElementWithOptions(. "div", elementProps(~onClick=_ => Js.log("hello world")))
64+
document.createElementWithOptions(. "div", elementProps(~onClick=(_) => Js.log("hello world")))
6565

6666
f(. 1)
6767
f(. [1, 2, 3])
Lines changed: 0 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,120 +0,0 @@
1-
let sequentialAwait = async () => {
2-
let result1 = await paused("first")
3-
nodeJsAssert.equal(result1, "first")
4-
5-
let result2 = await paused("second")
6-
nodeJsAssert.equal(result2, "second")
7-
}
8-
9-
let f = async () => ()
10-
let f = async (. ()) => ()
11-
let f = async f => f()
12-
let f = async (a, b) => a + b
13-
let f = async (. a, b) => a + b
14-
15-
let maybeSomeValue = switch await fetchData(url) {
16-
| data => Some(data)
17-
| exception JsError(_) => None
18-
}
19-
20-
(await f)(a, b)
21-
-(await f)
22-
(await 1) + (await 2)
23-
24-
lazy (await f())
25-
assert (await f())
26-
27-
(await f).json()
28-
29-
user.data = await fetch()
30-
31-
<Navbar promise={await gc()}> {await weirdReactSuspenseApi} </Navbar>
32-
33-
let inBinaryExpression = (await x)->Js.Promise.resolve + 1
34-
let inBinaryExpression = (await x)->Js.Promise.resolve + (await y)->Js.Promise.resolve
35-
36-
let withCallback = async (. ()) => {
37-
async (. x) => await (x->Js.promise.resolve) + 1
38-
}
39-
40-
let () = (await fetch(url))->(await resolve)
41-
42-
let _ = await (1 + 1)
43-
let _ = (await 1) + 1
44-
let _ = await -1
45-
let _ = await -1
46-
let _ = await !ref
47-
let _ = await f
48-
let _ = await %extension
49-
let _ = await "test"
50-
let _ = await ((a, b) => a + b)
51-
let _ = await (async (a, b) => a + b)
52-
let _ = await (
53-
switch x {
54-
| A => ()
55-
| B => ()
56-
}
57-
)
58-
let _ = await [1, 2, 3]
59-
let _ = await (1, 2, 3)
60-
let _ = await {name: "Steve", age: 32}
61-
let _ = await (user.name = "Steve")
62-
let _ = await (
63-
if 20 {
64-
true
65-
} else {
66-
false
67-
}
68-
)
69-
let _ = await (condition() ? true : false)
70-
let _ = await f(x)
71-
let _ = await f(. x)
72-
let _ = (await (f(x): Js.Promise.t<unit>))
73-
let _ = await (
74-
while true {
75-
infiniteLoop()
76-
}
77-
)
78-
let _ = await (
79-
try ok() catch {
80-
| _ => logError()
81-
}
82-
)
83-
let _ = await (
84-
for i in 0 to 10 {
85-
sideEffect()
86-
}
87-
)
88-
let _ = await (lazy x)
89-
let _ = await (assert x)
90-
let _ = await promises[0]
91-
let _ = await promises["resolved"]
92-
let _ = await promises["resolved"] = sideEffect()
93-
let _ = @attr await (expr)
94-
let _ = await module(Foo)
95-
let _ = await module(Foo: Bar)
96-
let _ = await Promise
97-
let _ = await Promise(status)
98-
99-
// braces are being dropped, because the ast only has space to record braces surrounding the await
100-
let _ = await x
101-
// here braces stay, because of precedence
102-
let _ = await {
103-
let x = 1
104-
Js.Promise.resolve(x)
105-
}
106-
107-
let f1 = async (~x, ~y) => x + y
108-
let f2 = async (@foo ~x, @bar ~y) => x + y
109-
let f3 = async (@bar @foo ~x as @zz z, ~y) => x + y
110-
let f4 = async x => x
111-
let f5 = async x => async y => 3
112-
let f6 = async (~x1, ~x2) => async y => 3
113-
let f7 = async x => async (~y) => 3
114-
let f8 = async (~x1, ~x2) => async (~y) => 3
115-
let f9 = x => async (~y) => 3
116-
let f10 = x => async y => 3
117-
let f11 = (. ~x) => (. ~y) => 3
118-
119-
let f12 = @a x => 3
120-
let f13 = (@a @b ~x) => 3

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ React.useEffect0(() => {
5656
})
5757

5858
<div
59-
onClick={event => {
59+
onClick={(event) => {
6060
switch videoContainerRect {
6161
| Some(videoContainerRect) =>
6262
let newChapter = ({startTime: percent *. duration}: Video.chapter)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let _ = {a}
2626

2727
let _ = {"constant"}
2828

29-
let _ = {() => Js.log("test")}
29+
let _ = {(()) => Js.log("test")}
3030
let _ = {
3131
switch b {
3232
| true => ()

0 commit comments

Comments
 (0)