Skip to content

Commit 2bd71a6

Browse files
committed
Fix formatting uncurried functions with attributes.
In `@foo (. x) => 3` the attribute `@foo` went outside `Js.Fn.I1 ` instead of the function contained inside.
1 parent fd1a522 commit 2bd71a6

File tree

9 files changed

+88
-35
lines changed

9 files changed

+88
-35
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ These are only breaking changes for unformatted code.
3939
- Fix issue where uncurried was not supported with pipe https://github.com/rescript-lang/rescript-compiler/pull/5803
4040
- Fix printing of nested types in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/5826
4141
- Fix issue in printing uncurried callbacks https://github.com/rescript-lang/rescript-compiler/pull/5828
42-
42+
- Fix formatting uncurried functions with attributes https://github.com/rescript-lang/rescript-compiler/pull/5829
43+
4344
#### :nail_care: Polish
4445

4546
- Syntax: process uncurried types explicitly in the parser/printer https://github.com/rescript-lang/rescript-compiler/pull/5784 https://github.com/rescript-lang/rescript-compiler/pull/5822

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164524,7 +164524,7 @@ and parseUnaryExpr p =
164524164524
* the operands of the binary expression with opeartor `+` *)
164525164525
and parseOperandExpr ~context p =
164526164526
let startPos = p.Parser.startPos in
164527-
let attrs = parseAttributes p in
164527+
let attrs = ref (parseAttributes p) in
164528164528
let expr =
164529164529
match p.Parser.token with
164530164530
| Assert ->
@@ -164540,7 +164540,9 @@ and parseOperandExpr ~context p =
164540164540
*)
164541164541
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
164542164542
->
164543-
parseAsyncArrowExpression p
164543+
let arrowAttrs = !attrs in
164544+
let () = attrs := [] in
164545+
parseAsyncArrowExpression ~arrowAttrs p
164544164546
| Await -> parseAwaitExpression p
164545164547
| Lazy ->
164546164548
Parser.next p;
@@ -164556,13 +164558,16 @@ and parseOperandExpr ~context p =
164556164558
if
164557164559
context != WhenExpr
164558164560
&& isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
164559-
then parseEs6ArrowExpression ~context p
164561+
then
164562+
let arrowAttrs = !attrs in
164563+
let () = attrs := [] in
164564+
parseEs6ArrowExpression ~arrowAttrs ~context p
164560164565
else parseUnaryExpr p
164561164566
in
164562164567
(* let endPos = p.Parser.prevEndPos in *)
164563164568
{
164564164569
expr with
164565-
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; attrs];
164570+
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; !attrs];
164566164571
(* pexp_loc = mkLoc startPos endPos *)
164567164572
}
164568164573

@@ -165621,12 +165626,12 @@ and parseExprBlock ?first p =
165621165626
Parser.eatBreadcrumb p;
165622165627
overParseConstrainedOrCoercedOrArrowExpression p blockExpr
165623165628

165624-
and parseAsyncArrowExpression p =
165629+
and parseAsyncArrowExpression ?(arrowAttrs = []) p =
165625165630
let startPos = p.Parser.startPos in
165626165631
Parser.expect (Lident "async") p;
165627165632
let asyncAttr = makeAsyncAttr (mkLoc startPos p.prevEndPos) in
165628-
parseEs6ArrowExpression ~arrowAttrs:[asyncAttr] ~arrowStartPos:(Some startPos)
165629-
p
165633+
parseEs6ArrowExpression ~arrowAttrs:(asyncAttr :: arrowAttrs)
165634+
~arrowStartPos:(Some startPos) p
165630165635

165631165636
and parseAwaitExpression p =
165632165637
let awaitLoc = mkLoc p.Parser.startPos p.endPos in

lib/4.06.1/whole_compiler.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177956,7 +177956,7 @@ and parseUnaryExpr p =
177956177956
* the operands of the binary expression with opeartor `+` *)
177957177957
and parseOperandExpr ~context p =
177958177958
let startPos = p.Parser.startPos in
177959-
let attrs = parseAttributes p in
177959+
let attrs = ref (parseAttributes p) in
177960177960
let expr =
177961177961
match p.Parser.token with
177962177962
| Assert ->
@@ -177972,7 +177972,9 @@ and parseOperandExpr ~context p =
177972177972
*)
177973177973
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
177974177974
->
177975-
parseAsyncArrowExpression p
177975+
let arrowAttrs = !attrs in
177976+
let () = attrs := [] in
177977+
parseAsyncArrowExpression ~arrowAttrs p
177976177978
| Await -> parseAwaitExpression p
177977177979
| Lazy ->
177978177980
Parser.next p;
@@ -177988,13 +177990,16 @@ and parseOperandExpr ~context p =
177988177990
if
177989177991
context != WhenExpr
177990177992
&& isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
177991-
then parseEs6ArrowExpression ~context p
177993+
then
177994+
let arrowAttrs = !attrs in
177995+
let () = attrs := [] in
177996+
parseEs6ArrowExpression ~arrowAttrs ~context p
177992177997
else parseUnaryExpr p
177993177998
in
177994177999
(* let endPos = p.Parser.prevEndPos in *)
177995178000
{
177996178001
expr with
177997-
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; attrs];
178002+
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; !attrs];
177998178003
(* pexp_loc = mkLoc startPos endPos *)
177999178004
}
178000178005

@@ -179053,12 +179058,12 @@ and parseExprBlock ?first p =
179053179058
Parser.eatBreadcrumb p;
179054179059
overParseConstrainedOrCoercedOrArrowExpression p blockExpr
179055179060

179056-
and parseAsyncArrowExpression p =
179061+
and parseAsyncArrowExpression ?(arrowAttrs = []) p =
179057179062
let startPos = p.Parser.startPos in
179058179063
Parser.expect (Lident "async") p;
179059179064
let asyncAttr = makeAsyncAttr (mkLoc startPos p.prevEndPos) in
179060-
parseEs6ArrowExpression ~arrowAttrs:[asyncAttr] ~arrowStartPos:(Some startPos)
179061-
p
179065+
parseEs6ArrowExpression ~arrowAttrs:(asyncAttr :: arrowAttrs)
179066+
~arrowStartPos:(Some startPos) p
179062179067

179063179068
and parseAwaitExpression p =
179064179069
let awaitLoc = mkLoc p.Parser.startPos p.endPos in

res_syntax/src/res_core.ml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2110,7 +2110,7 @@ and parseUnaryExpr p =
21102110
* the operands of the binary expression with opeartor `+` *)
21112111
and parseOperandExpr ~context p =
21122112
let startPos = p.Parser.startPos in
2113-
let attrs = parseAttributes p in
2113+
let attrs = ref (parseAttributes p) in
21142114
let expr =
21152115
match p.Parser.token with
21162116
| Assert ->
@@ -2126,7 +2126,9 @@ and parseOperandExpr ~context p =
21262126
*)
21272127
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
21282128
->
2129-
parseAsyncArrowExpression p
2129+
let arrowAttrs = !attrs in
2130+
let () = attrs := [] in
2131+
parseAsyncArrowExpression ~arrowAttrs p
21302132
| Await -> parseAwaitExpression p
21312133
| Lazy ->
21322134
Parser.next p;
@@ -2142,13 +2144,16 @@ and parseOperandExpr ~context p =
21422144
if
21432145
context != WhenExpr
21442146
&& isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
2145-
then parseEs6ArrowExpression ~context p
2147+
then
2148+
let arrowAttrs = !attrs in
2149+
let () = attrs := [] in
2150+
parseEs6ArrowExpression ~arrowAttrs ~context p
21462151
else parseUnaryExpr p
21472152
in
21482153
(* let endPos = p.Parser.prevEndPos in *)
21492154
{
21502155
expr with
2151-
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; attrs];
2156+
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; !attrs];
21522157
(* pexp_loc = mkLoc startPos endPos *)
21532158
}
21542159

@@ -3207,12 +3212,12 @@ and parseExprBlock ?first p =
32073212
Parser.eatBreadcrumb p;
32083213
overParseConstrainedOrCoercedOrArrowExpression p blockExpr
32093214

3210-
and parseAsyncArrowExpression p =
3215+
and parseAsyncArrowExpression ?(arrowAttrs = []) p =
32113216
let startPos = p.Parser.startPos in
32123217
Parser.expect (Lident "async") p;
32133218
let asyncAttr = makeAsyncAttr (mkLoc startPos p.prevEndPos) in
3214-
parseEs6ArrowExpression ~arrowAttrs:[asyncAttr] ~arrowStartPos:(Some startPos)
3215-
p
3219+
parseEs6ArrowExpression ~arrowAttrs:(asyncAttr :: arrowAttrs)
3220+
~arrowStartPos:(Some startPos) p
32163221

32173222
and parseAwaitExpression p =
32183223
let awaitLoc = mkLoc p.Parser.startPos p.endPos in

res_syntax/tests/parsing/grammar/expressions/UncurriedByDefault.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ type unested = (. (. string) => unit) => unit
3737
let uannpoly: (. 'a) => string = xx
3838
let uannint: (. int) => string = xx
3939

40+
let _ = @att (. x) => 34
41+
let _ = @att async (. x) => 34
42+
let _ = preserveAttr(@att (. x) => 34)
43+
let _ = preserveAttr(@att async (. x) => 34)
44+
4045
@@uncurried
4146

4247
let cApp = foo(. 3)
@@ -80,3 +85,8 @@ let pipe1 = 3->f
8085

8186
let uannpoly: 'a => string = xx
8287
let uannint: int => string = xx
88+
89+
let _ = @att x => 34
90+
let _ = @att async x => 34
91+
let _ = preserveAttr(@att x => 34)
92+
let _ = preserveAttr(@att async x => 34)

res_syntax/tests/parsing/grammar/expressions/expected/UncurriedByDefault.res.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ type nonrec cnested = (string -> unit) -> unit
4444
type nonrec unested = ((string -> unit) Js.Fn.arity1 -> unit) Js.Fn.arity1
4545
let (uannpoly : ('a -> string) Js.Fn.arity1) = xx
4646
let (uannint : (int -> string) Js.Fn.arity1) = xx
47+
let _ = { Js.Fn.I1 = ((fun x -> 34)[@att ]) }
48+
let _ = { Js.Fn.I1 = ((fun x -> 34)[@res.async ][@att ]) }
49+
let _ = preserveAttr { Js.Fn.I1 = ((fun x -> 34)[@att ]) }
50+
let _ = preserveAttr { Js.Fn.I1 = ((fun x -> 34)[@res.async ][@att ]) }
4751
[@@@uncurried ]
4852
let cApp = foo 3
4953
let uApp = ((foo 3)[@bs ])
@@ -92,4 +96,9 @@ type nonrec cnested = (string -> unit) -> unit
9296
type nonrec unested = ((string -> unit) Js.Fn.arity1 -> unit) Js.Fn.arity1
9397
let pipe1 = 3 |.u f
9498
let (uannpoly : ('a -> string) Js.Fn.arity1) = xx
95-
let (uannint : (int -> string) Js.Fn.arity1) = xx
99+
let (uannint : (int -> string) Js.Fn.arity1) = xx
100+
let _ = { Js.Fn.I1 = ((fun x -> 34)[@att ]) }
101+
let _ = { Js.Fn.I1 = ((fun x -> 34)[@res.async ][@att ]) }
102+
let _ = ((preserveAttr { Js.Fn.I1 = ((fun x -> 34)[@att ]) })[@bs ])
103+
let _ = ((preserveAttr { Js.Fn.I1 = ((fun x -> 34)[@res.async ][@att ]) })
104+
[@bs ])

res_syntax/tests/parsing/grammar/expressions/expected/uncurried.res.txt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ let f =
66
(fun a -> fun b -> { Js.Fn.I2 = (fun c -> fun d -> ((a + b) + c) + d) })
77
}
88
let f =
9-
(({
10-
Js.Fn.I1 =
11-
(fun a ->
12-
((fun b ->
13-
(({
14-
Js.Fn.I1 =
15-
(fun c -> ((fun d -> ())[@ns.braces ][@attr4 ]))
16-
})
17-
[@attr3 ]))
18-
[@ns.braces ][@attr2 ]))
19-
})
20-
[@attr ])
9+
{
10+
Js.Fn.I1 =
11+
((fun a ->
12+
((fun b ->
13+
{
14+
Js.Fn.I1 = ((fun c -> ((fun d -> ())[@ns.braces ][@attr4 ]))
15+
[@attr3 ])
16+
})
17+
[@ns.braces ][@attr2 ]))[@attr ])
18+
}
2119
let f =
2220
{
2321
Js.Fn.I2 =

res_syntax/tests/printer/expr/UncurriedByDefault.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ let _ = setTimeout(() => {
4444
resolve(1)
4545
}, 100)
4646

47+
let _ = @att (. x) => 34
48+
let _ = @att async (. x) => 34
49+
let _ = preserveAttr(@att (. x) => 34)
50+
let _ = preserveAttr(@att async (. x) => 34)
51+
4752
@@uncurried
4853

4954
let cApp = foo(. 3)
@@ -92,3 +97,8 @@ let _ = setTimeout(() => {
9297
let _ = setTimeout(. (. ()) => {
9398
resolve(. 1)
9499
}, 100)
100+
101+
let _ = @att x => 34
102+
let _ = @att async x => 34
103+
let _ = preserveAttr(@att x => 34)
104+
let _ = preserveAttr(@att async x => 34)

res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ let _ = setTimeout(() => {
4444
resolve(1)
4545
}, 100)
4646

47+
let _ = @att (. x) => 34
48+
let _ = @att async (. x) => 34
49+
let _ = preserveAttr(@att (. x) => 34)
50+
let _ = preserveAttr(@att async (. x) => 34)
51+
4752
@@uncurried
4853

4954
let cApp = foo(. 3)
@@ -92,3 +97,8 @@ let _ = setTimeout(() => {
9297
let _ = setTimeout(. (. ()) => {
9398
resolve(. 1)
9499
}, 100)
100+
101+
let _ = @att x => 34
102+
let _ = @att async x => 34
103+
let _ = preserveAttr(@att x => 34)
104+
let _ = preserveAttr(@att async x => 34)

0 commit comments

Comments
 (0)