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

Commit 5868f41

Browse files
authored
Fix several printing issues with async including an infinite loop. (#680)
* More async examples. See #679 * Even more examples. * Fix a bunch of async printing issues including an infinite loop. The infinite loop happens when the expression is returned unchanged. Now checking that some progress is made. Fixes #681 * No unnecessary parens with async. * Update CHANGELOG.md * Two more examples that need fixing later.
1 parent 124c14a commit 5868f41

File tree

6 files changed

+44
-17
lines changed

6 files changed

+44
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
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/
4242
- Fix issue where the formatter would delete `async` in a function with labelled arguments.
43+
- Fix several printing issues with `async` including an infinite loop https://github.com/rescript-lang/syntax/pull/680
4344

4445
#### :eyeglasses: Spec Compliance
4546

src/res_parsetree_viewer.ml

Lines changed: 12 additions & 8 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 attrsBefore acc expr =
139+
let rec collect n attrsBefore acc expr =
140140
match expr with
141141
| {
142142
pexp_desc =
@@ -152,13 +152,17 @@ let funExpr expr =
152152
pexp_attributes = [];
153153
} ->
154154
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
155-
collect attrsBefore (parameter :: acc) returnExpr
155+
collect (n + 1) attrsBefore (parameter :: acc) returnExpr
156156
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
157157
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
158158
let param = NewTypes {attrs; locs = stringLocs} in
159-
collect attrsBefore (param :: acc) returnExpr
160-
| {pexp_desc = Pexp_fun _; pexp_attributes = [({txt = "bs"}, _)]} ->
161-
(* stop here, the uncurried attribute always indicates the beginning of an arrow function
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
162166
* e.g. `(. a) => (. b)` instead of `(. a, . b)` *)
163167
(attrsBefore, List.rev acc, expr)
164168
| {
@@ -175,16 +179,16 @@ let funExpr expr =
175179
let parameter =
176180
Parameter {attrs = attrs_other; lbl; defaultExpr; pat = pattern}
177181
in
178-
collect (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
182+
collect (n + 1) (attrs_async @ attrsBefore) (parameter :: acc) returnExpr
179183
| expr -> (attrsBefore, List.rev acc, expr)
180184
in
181185
match expr with
182186
| {
183187
pexp_desc = Pexp_fun (Nolabel, _defaultExpr, _pattern, _returnExpr);
184188
pexp_attributes = attrs;
185189
} as expr ->
186-
collect attrs [] {expr with pexp_attributes = []}
187-
| expr -> collect [] [] expr
190+
collect 0 attrs [] {expr with pexp_attributes = []}
191+
| expr -> collect 0 [] [] expr
188192

189193
let processBracesAttr expr =
190194
match expr.pexp_attributes with

src/res_printer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4698,7 +4698,7 @@ and printExprFunParameters ~customLayout ~inCallback ~async ~uncurried
46984698
let txtDoc =
46994699
let var = printIdentLike stringLoc.txt in
47004700
let var = if hasConstraint then addParens var else var in
4701-
if async then addAsync (Doc.concat [Doc.lparen; var; Doc.rparen]) else var
4701+
if async then addAsync var else var
47024702
in
47034703
printComments txtDoc cmtTbl stringLoc.loc
47044704
(* let f = () => () *)

tests/printer/expr/asyncAwait.res

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ let _ = await {
8282
Js.Promise.resolve(x)
8383
}
8484

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
85+
let f1 = async (~x, ~y) => x + y
86+
let f2 = async (@foo ~x, @bar ~y) => x + y
87+
let f3 = @foo async (@bar ~x as @zz z, ~y) => x + y
88+
let f4 = async x => x
89+
let f5 = async x => async y => 3
90+
let f6 = async (~x1, ~x2) => async y => 3
91+
let f7 = async x => async (~y) => 3
92+
let f8 = async (~x1, ~x2) => async (~y) => 3
93+
let f9 = x => async (~y) => 3
94+
let f10 = x => async y => 3
95+
let f11 = (. ~x) => (. ~y) => 3
96+
97+
let f12 = @a (@b x) => 3
98+
let f13 = @a @b (~x) => 3

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let sequentialAwait = async () => {
88

99
let f = async () => ()
1010
let f = async (. ()) => ()
11-
let f = async (f) => f()
11+
let f = async f => f()
1212
let f = async (a, b) => a + b
1313
let f = async (. a, b) => a + b
1414

@@ -104,6 +104,17 @@ let _ = await {
104104
Js.Promise.resolve(x)
105105
}
106106

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
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/fun.res.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ let f = @attr (@attrOnA a, @attrOnB b) => @attr2 (@attrOnC c, @attrOnD d) => ()
7373
let f = @attr (. a, b) => @attr2 (. c, d) => ()
7474

7575
let f = (@attr ~a, @attr ~b) => ()
76-
let f = (. @attr ~a, . @attr ~b) => ()
76+
let f = (. @attr ~a) => (. @attr ~b) => ()
7777

7878
let f = (
7979
thisIsAVeryLongNaaaaaaaaaaaaaaaaaaameeeeeeeeeeeee,

0 commit comments

Comments
 (0)