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

Fix issue where the formatter would delete async in a function with… #677

Merged
merged 2 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- Fix issue where formatter erases tail comments inside JSX tag https://github.com/rescript-lang/syntax/issues/663
- Fix issue where the JSX prop has type annotation of the first class module https://github.com/rescript-lang/syntax/pull/666
- 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/
- Fix issue where the formatter would delete `async` in a function with labelled arguments.

#### :eyeglasses: Spec Compliance

Expand Down
11 changes: 9 additions & 2 deletions src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,15 @@ let funExpr expr =
(((Labelled _ | Optional _) as lbl), defaultExpr, pattern, returnExpr);
pexp_attributes = attrs;
} ->
let parameter = Parameter {attrs; lbl; defaultExpr; pat = pattern} in
collect attrsBefore (parameter :: acc) returnExpr
(* Normally attributes are attached to the labelled argument, e.g. (@foo ~x) => ...
In the case of `@res.async`, pass the attribute to the outside *)
let attrs_async, attrs_other =
attrs |> List.partition (fun ({Location.txt}, _) -> txt = "res.async")
in
let parameter =
Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
in
collect (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
| expr -> (attrsBefore, List.rev acc, expr)
in
match expr with
Expand Down
6 changes: 5 additions & 1 deletion tests/printer/expr/asyncAwait.res
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,8 @@ let _ = await {x}
let _ = await {
let x = 1
Js.Promise.resolve(x)
}
}

let f = async (~x, ~y) => x + y
let f = async (@foo ~x, @bar ~y) => x + y
let f = @foo async ( @bar ~x as @zz z, ~y) => x + y
4 changes: 4 additions & 0 deletions tests/printer/expr/expected/asyncAwait.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,7 @@ let _ = await {
let x = 1
Js.Promise.resolve(x)
}

let f = async (~x, ~y) => x + y
let f = async (@foo ~x, @bar ~y) => x + y
let f = async (@bar @foo ~x as @zz z, ~y) => x + y