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

Commit f3b0573

Browse files
IwanKaramazowMaxim
and
Maxim
authored
Remove printing of arrow functions with "multiple" dots (#668)
* Remove printing of arrow functions with "multiple" dots Arrow functions with multiple dots `(. a, . b) => a + b` are considered an anti-pattern. This syntax *will be removed* in the future. As a first step, the printer will know print this as multiple arrow functions. (Like in JavaScript) **Before** ```rescript let f = (. a, . b) => a + b type f = (. int, . int) => int ``` **After** ```rescript let f = (. a) => (. b) => a + b type f = (. int) => (. int) => int ``` * Remove processing of "res.async" in `arrowType`. Type expressions can't use the "async" keyword. * Add Changelog entry for consecutive arrow function printing Co-authored-by: Maxim <[email protected]>
1 parent 5fd1d92 commit f3b0573

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
- Fix issue where formatter erases tail comments inside JSX tag https://github.com/rescript-lang/syntax/issues/663
3636
- Fix issue where the JSX prop has type annotation of the first class module https://github.com/rescript-lang/syntax/pull/666
3737

38+
#### :eyeglasses: Spec Compliance
39+
40+
- Functions with consecutive dots now print as multiple arrow functions like in JavaScript.
41+
3842
## ReScript 10.0
3943

4044
- Fix printing for inline nullary functor types [#477](https://github.com/rescript-lang/syntax/pull/477)

src/res_parsetree_viewer.ml

+9-10
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ let arrowType ct =
1010
let arg = ([], lbl, typ1) in
1111
process attrsBefore (arg :: acc) typ2
1212
| {
13-
ptyp_desc = Ptyp_arrow ((Nolabel as lbl), typ1, typ2);
14-
ptyp_attributes = [({txt = "bs" | "res.async"}, _)] as attrs;
13+
ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2);
14+
ptyp_attributes = [({txt = "bs"}, _)];
1515
} ->
16-
let arg = (attrs, lbl, typ1) in
17-
process attrsBefore (arg :: acc) typ2
16+
(* stop here, the uncurried attribute always indicates the beginning of an arrow function
17+
* e.g. `(. int) => (. int)` instead of `(. int, . int)` *)
18+
(attrsBefore, List.rev acc, typ)
1819
| {ptyp_desc = Ptyp_arrow (Nolabel, _typ1, _typ2); ptyp_attributes = _attrs}
1920
as returnType ->
2021
let args = List.rev acc in
@@ -156,12 +157,10 @@ let funExpr expr =
156157
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
157158
let param = NewTypes {attrs; locs = stringLocs} in
158159
collect attrsBefore (param :: acc) returnExpr
159-
| {
160-
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
161-
pexp_attributes = [({txt = "bs"}, _)] as attrs;
162-
} ->
163-
let parameter = Parameter {attrs; lbl; defaultExpr; pat = pattern} in
164-
collect attrsBefore (parameter :: 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
162+
* e.g. `(. a) => (. b)` instead of `(. a, . b)` *)
163+
(attrsBefore, List.rev acc, expr)
165164
| {
166165
pexp_desc =
167166
Pexp_fun

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -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/printer/expr/expected/fun.res.txt

+4
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,7 @@ let query = (~url, ()): ((unit, unit) => unit) => {
352352
let query = (~url, ()): ((unit, unit, unit) => unit) => {
353353
((), (), ()) => Js.log("Queried " ++ url)
354354
}
355+
356+
let f = (. a) => (. b) => a + b
357+
let f = (. a, b) => (. b, c) => a + b + c + d
358+
let f = (. a, b) => (. b, c) => (. e, f, g) => a + b + c + d + e + f + g

tests/printer/expr/fun.res

+4
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,7 @@ let query = (~url, ()): (unit => unit => unit) => {
293293
let query = (~url, ()): (unit => unit => unit => unit) => {
294294
() => () => () => Js.log("Queried " ++ url)
295295
}
296+
297+
let f = (. a) => (. b) => a + b
298+
let f = (. a, b) => (. b, c) => a + b + c + d
299+
let f = (. a, b) => (. b , c) => (. e , f, g) => a + b + c + d + e + f + g

tests/printer/typexpr/expected/arrow.res.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ float
205205
// uncurried
206206
type t = (. int) => int
207207
type t = (. int, int) => int
208-
type t = (. int, . int) => int
209-
type t = (. int, int, . int, int) => int
208+
type t = (. int) => (. int) => int
209+
type t = (. int, int) => (. int, int) => int
210210

211211
type t = (. @attr int) => unit
212-
type t = (. @attr int, . @attr2 int) => unit
213-
type t = (. @attrOnInt int, @attrOnInt int, . @attrOnInt int, @attrOnInt int) => int
212+
type t = (. @attr int) => (. @attr2 int) => unit
213+
type t = (. @attrOnInt int, @attrOnInt int) => (. @attrOnInt int, @attrOnInt int) => int
214214
type t = (. @attr ~x: int, ~y: int, . @attr ~z: int, @attr ~omega: int) => unit
215215

216216
@val external requestAnimationFrame: (float => unit) => unit = "requestAnimationFrame"

0 commit comments

Comments
 (0)