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

Commit 124c14a

Browse files
authored
Fix issue where the formatter would delete async in a function with… (#677)
* Fix issue where the formatter would delete `async` in a function with labelled arguments. * More async formatting examples with named arguments.
1 parent a3c27ef commit 124c14a

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
- Fix issue where formatter erases tail comments inside JSX tag https://github.com/rescript-lang/syntax/issues/663
4040
- Fix issue where the JSX prop has type annotation of the first class module https://github.com/rescript-lang/syntax/pull/666
4141
- Fix issue where a spread `...x` in non-last position would not be reported as syntax error https://github.com/rescript-lang/syntax/pull/673/
42+
- Fix issue where the formatter would delete `async` in a function with labelled arguments.
4243

4344
#### :eyeglasses: Spec Compliance
4445

src/res_parsetree_viewer.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,15 @@ let funExpr expr =
167167
(((Labelled _ | Optional _) as lbl), defaultExpr, pattern, returnExpr);
168168
pexp_attributes = attrs;
169169
} ->
170-
let parameter = Parameter {attrs; lbl; defaultExpr; pat = pattern} in
171-
collect attrsBefore (parameter :: acc) returnExpr
170+
(* Normally attributes are attached to the labelled argument, e.g. (@foo ~x) => ...
171+
In the case of `@res.async`, pass the attribute to the outside *)
172+
let attrs_async, attrs_other =
173+
attrs |> List.partition (fun ({Location.txt}, _) -> txt = "res.async")
174+
in
175+
let parameter =
176+
Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
177+
in
178+
collect (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
172179
| expr -> (attrsBefore, List.rev acc, expr)
173180
in
174181
match expr with

tests/printer/expr/asyncAwait.res

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,8 @@ let _ = await {x}
8080
let _ = await {
8181
let x = 1
8282
Js.Promise.resolve(x)
83-
}
83+
}
84+
85+
let f = async (~x, ~y) => x + y
86+
let f = async (@foo ~x, @bar ~y) => x + y
87+
let f = @foo async ( @bar ~x as @zz z, ~y) => x + y

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,7 @@ let _ = await {
103103
let x = 1
104104
Js.Promise.resolve(x)
105105
}
106+
107+
let f = async (~x, ~y) => x + y
108+
let f = async (@foo ~x, @bar ~y) => x + y
109+
let f = async (@bar @foo ~x as @zz z, ~y) => x + y

0 commit comments

Comments
 (0)