Skip to content

Fix formatting uncurried functions with attributes. #5829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 19, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ These are only breaking changes for unformatted code.
- Fix issue where uncurried was not supported with pipe https://github.com/rescript-lang/rescript-compiler/pull/5803
- Fix printing of nested types in uncurried mode https://github.com/rescript-lang/rescript-compiler/pull/5826
- Fix issue in printing uncurried callbacks https://github.com/rescript-lang/rescript-compiler/pull/5828

- Fix formatting uncurried functions with attributes https://github.com/rescript-lang/rescript-compiler/pull/5829

#### :nail_care: Polish

- 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
Expand Down
19 changes: 12 additions & 7 deletions lib/4.06.1/unstable/js_playground_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -164524,7 +164524,7 @@ and parseUnaryExpr p =
* the operands of the binary expression with opeartor `+` *)
and parseOperandExpr ~context p =
let startPos = p.Parser.startPos in
let attrs = parseAttributes p in
let attrs = ref (parseAttributes p) in
let expr =
match p.Parser.token with
| Assert ->
Expand All @@ -164540,7 +164540,9 @@ and parseOperandExpr ~context p =
*)
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
->
parseAsyncArrowExpression p
let arrowAttrs = !attrs in
let () = attrs := [] in
parseAsyncArrowExpression ~arrowAttrs p
| Await -> parseAwaitExpression p
| Lazy ->
Parser.next p;
Expand All @@ -164556,13 +164558,16 @@ and parseOperandExpr ~context p =
if
context != WhenExpr
&& isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
then parseEs6ArrowExpression ~context p
then
let arrowAttrs = !attrs in
let () = attrs := [] in
parseEs6ArrowExpression ~arrowAttrs ~context p
else parseUnaryExpr p
in
(* let endPos = p.Parser.prevEndPos in *)
{
expr with
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; attrs];
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; !attrs];
(* pexp_loc = mkLoc startPos endPos *)
}

Expand Down Expand Up @@ -165621,12 +165626,12 @@ and parseExprBlock ?first p =
Parser.eatBreadcrumb p;
overParseConstrainedOrCoercedOrArrowExpression p blockExpr

and parseAsyncArrowExpression p =
and parseAsyncArrowExpression ?(arrowAttrs = []) p =
let startPos = p.Parser.startPos in
Parser.expect (Lident "async") p;
let asyncAttr = makeAsyncAttr (mkLoc startPos p.prevEndPos) in
parseEs6ArrowExpression ~arrowAttrs:[asyncAttr] ~arrowStartPos:(Some startPos)
p
parseEs6ArrowExpression ~arrowAttrs:(asyncAttr :: arrowAttrs)
~arrowStartPos:(Some startPos) p

and parseAwaitExpression p =
let awaitLoc = mkLoc p.Parser.startPos p.endPos in
Expand Down
19 changes: 12 additions & 7 deletions lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -177956,7 +177956,7 @@ and parseUnaryExpr p =
* the operands of the binary expression with opeartor `+` *)
and parseOperandExpr ~context p =
let startPos = p.Parser.startPos in
let attrs = parseAttributes p in
let attrs = ref (parseAttributes p) in
let expr =
match p.Parser.token with
| Assert ->
Expand All @@ -177972,7 +177972,9 @@ and parseOperandExpr ~context p =
*)
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
->
parseAsyncArrowExpression p
let arrowAttrs = !attrs in
let () = attrs := [] in
parseAsyncArrowExpression ~arrowAttrs p
| Await -> parseAwaitExpression p
| Lazy ->
Parser.next p;
Expand All @@ -177988,13 +177990,16 @@ and parseOperandExpr ~context p =
if
context != WhenExpr
&& isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
then parseEs6ArrowExpression ~context p
then
let arrowAttrs = !attrs in
let () = attrs := [] in
parseEs6ArrowExpression ~arrowAttrs ~context p
else parseUnaryExpr p
in
(* let endPos = p.Parser.prevEndPos in *)
{
expr with
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; attrs];
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; !attrs];
(* pexp_loc = mkLoc startPos endPos *)
}

Expand Down Expand Up @@ -179053,12 +179058,12 @@ and parseExprBlock ?first p =
Parser.eatBreadcrumb p;
overParseConstrainedOrCoercedOrArrowExpression p blockExpr

and parseAsyncArrowExpression p =
and parseAsyncArrowExpression ?(arrowAttrs = []) p =
let startPos = p.Parser.startPos in
Parser.expect (Lident "async") p;
let asyncAttr = makeAsyncAttr (mkLoc startPos p.prevEndPos) in
parseEs6ArrowExpression ~arrowAttrs:[asyncAttr] ~arrowStartPos:(Some startPos)
p
parseEs6ArrowExpression ~arrowAttrs:(asyncAttr :: arrowAttrs)
~arrowStartPos:(Some startPos) p

and parseAwaitExpression p =
let awaitLoc = mkLoc p.Parser.startPos p.endPos in
Expand Down
19 changes: 12 additions & 7 deletions res_syntax/src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2110,7 +2110,7 @@ and parseUnaryExpr p =
* the operands of the binary expression with opeartor `+` *)
and parseOperandExpr ~context p =
let startPos = p.Parser.startPos in
let attrs = parseAttributes p in
let attrs = ref (parseAttributes p) in
let expr =
match p.Parser.token with
| Assert ->
Expand All @@ -2126,7 +2126,9 @@ and parseOperandExpr ~context p =
*)
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
->
parseAsyncArrowExpression p
let arrowAttrs = !attrs in
let () = attrs := [] in
parseAsyncArrowExpression ~arrowAttrs p
| Await -> parseAwaitExpression p
| Lazy ->
Parser.next p;
Expand All @@ -2142,13 +2144,16 @@ and parseOperandExpr ~context p =
if
context != WhenExpr
&& isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
then parseEs6ArrowExpression ~context p
then
let arrowAttrs = !attrs in
let () = attrs := [] in
parseEs6ArrowExpression ~arrowAttrs ~context p
else parseUnaryExpr p
in
(* let endPos = p.Parser.prevEndPos in *)
{
expr with
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; attrs];
pexp_attributes = List.concat [expr.Parsetree.pexp_attributes; !attrs];
(* pexp_loc = mkLoc startPos endPos *)
}

Expand Down Expand Up @@ -3207,12 +3212,12 @@ and parseExprBlock ?first p =
Parser.eatBreadcrumb p;
overParseConstrainedOrCoercedOrArrowExpression p blockExpr

and parseAsyncArrowExpression p =
and parseAsyncArrowExpression ?(arrowAttrs = []) p =
let startPos = p.Parser.startPos in
Parser.expect (Lident "async") p;
let asyncAttr = makeAsyncAttr (mkLoc startPos p.prevEndPos) in
parseEs6ArrowExpression ~arrowAttrs:[asyncAttr] ~arrowStartPos:(Some startPos)
p
parseEs6ArrowExpression ~arrowAttrs:(asyncAttr :: arrowAttrs)
~arrowStartPos:(Some startPos) p

and parseAwaitExpression p =
let awaitLoc = mkLoc p.Parser.startPos p.endPos in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ type unested = (. (. string) => unit) => unit
let uannpoly: (. 'a) => string = xx
let uannint: (. int) => string = xx

let _ = @att (. x) => 34
let _ = @att async (. x) => 34
let _ = preserveAttr(@att (. x) => 34)
let _ = preserveAttr(@att async (. x) => 34)

@@uncurried

let cApp = foo(. 3)
Expand Down Expand Up @@ -80,3 +85,8 @@ let pipe1 = 3->f

let uannpoly: 'a => string = xx
let uannint: int => string = xx

let _ = @att x => 34
let _ = @att async x => 34
let _ = preserveAttr(@att x => 34)
let _ = preserveAttr(@att async x => 34)
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ type nonrec cnested = (string -> unit) -> unit
type nonrec unested = ((string -> unit) Js.Fn.arity1 -> unit) Js.Fn.arity1
let (uannpoly : ('a -> string) Js.Fn.arity1) = xx
let (uannint : (int -> string) Js.Fn.arity1) = xx
let _ = { Js.Fn.I1 = ((fun x -> 34)[@att ]) }
let _ = { Js.Fn.I1 = ((fun x -> 34)[@res.async ][@att ]) }
let _ = preserveAttr { Js.Fn.I1 = ((fun x -> 34)[@att ]) }
let _ = preserveAttr { Js.Fn.I1 = ((fun x -> 34)[@res.async ][@att ]) }
[@@@uncurried ]
let cApp = foo 3
let uApp = ((foo 3)[@bs ])
Expand Down Expand Up @@ -92,4 +96,9 @@ type nonrec cnested = (string -> unit) -> unit
type nonrec unested = ((string -> unit) Js.Fn.arity1 -> unit) Js.Fn.arity1
let pipe1 = 3 |.u f
let (uannpoly : ('a -> string) Js.Fn.arity1) = xx
let (uannint : (int -> string) Js.Fn.arity1) = xx
let (uannint : (int -> string) Js.Fn.arity1) = xx
let _ = { Js.Fn.I1 = ((fun x -> 34)[@att ]) }
let _ = { Js.Fn.I1 = ((fun x -> 34)[@res.async ][@att ]) }
let _ = ((preserveAttr { Js.Fn.I1 = ((fun x -> 34)[@att ]) })[@bs ])
let _ = ((preserveAttr { Js.Fn.I1 = ((fun x -> 34)[@res.async ][@att ]) })
[@bs ])
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@ let f =
(fun a -> fun b -> { Js.Fn.I2 = (fun c -> fun d -> ((a + b) + c) + d) })
}
let f =
(({
Js.Fn.I1 =
(fun a ->
((fun b ->
(({
Js.Fn.I1 =
(fun c -> ((fun d -> ())[@ns.braces ][@attr4 ]))
})
[@attr3 ]))
[@ns.braces ][@attr2 ]))
})
[@attr ])
{
Js.Fn.I1 =
((fun a ->
((fun b ->
{
Js.Fn.I1 = ((fun c -> ((fun d -> ())[@ns.braces ][@attr4 ]))
[@attr3 ])
})
[@ns.braces ][@attr2 ]))[@attr ])
}
let f =
{
Js.Fn.I2 =
Expand Down
10 changes: 10 additions & 0 deletions res_syntax/tests/printer/expr/UncurriedByDefault.res
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ let _ = setTimeout(() => {
resolve(1)
}, 100)

let _ = @att (. x) => 34
let _ = @att async (. x) => 34
let _ = preserveAttr(@att (. x) => 34)
let _ = preserveAttr(@att async (. x) => 34)

@@uncurried

let cApp = foo(. 3)
Expand Down Expand Up @@ -92,3 +97,8 @@ let _ = setTimeout(() => {
let _ = setTimeout(. (. ()) => {
resolve(. 1)
}, 100)

let _ = @att x => 34
let _ = @att async x => 34
let _ = preserveAttr(@att x => 34)
let _ = preserveAttr(@att async x => 34)
10 changes: 10 additions & 0 deletions res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ let _ = setTimeout(() => {
resolve(1)
}, 100)

let _ = @att (. x) => 34
let _ = @att async (. x) => 34
let _ = preserveAttr(@att (. x) => 34)
let _ = preserveAttr(@att async (. x) => 34)

@@uncurried

let cApp = foo(. 3)
Expand Down Expand Up @@ -92,3 +97,8 @@ let _ = setTimeout(() => {
let _ = setTimeout(. (. ()) => {
resolve(. 1)
}, 100)

let _ = @att x => 34
let _ = @att async x => 34
let _ = preserveAttr(@att x => 34)
let _ = preserveAttr(@att async x => 34)