From 2bd71a6dadc2dcf51b3086ab35504c20702828e4 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sat, 19 Nov 2022 10:21:23 +0100 Subject: [PATCH] Fix formatting uncurried functions with attributes. In `@foo (. x) => 3` the attribute `@foo` went outside `Js.Fn.I1 ` instead of the function contained inside. --- CHANGELOG.md | 3 ++- lib/4.06.1/unstable/js_playground_compiler.ml | 19 ++++++++++------ lib/4.06.1/whole_compiler.ml | 19 ++++++++++------ res_syntax/src/res_core.ml | 19 ++++++++++------ .../expressions/UncurriedByDefault.res | 10 +++++++++ .../expected/UncurriedByDefault.res.txt | 11 +++++++++- .../expressions/expected/uncurried.res.txt | 22 +++++++++---------- .../tests/printer/expr/UncurriedByDefault.res | 10 +++++++++ .../expr/expected/UncurriedByDefault.res.txt | 10 +++++++++ 9 files changed, 88 insertions(+), 35 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79725ddf38..d87c86130b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/4.06.1/unstable/js_playground_compiler.ml b/lib/4.06.1/unstable/js_playground_compiler.ml index 41d8027dc3..1f44615640 100644 --- a/lib/4.06.1/unstable/js_playground_compiler.ml +++ b/lib/4.06.1/unstable/js_playground_compiler.ml @@ -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 -> @@ -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; @@ -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 *) } @@ -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 diff --git a/lib/4.06.1/whole_compiler.ml b/lib/4.06.1/whole_compiler.ml index 6ed555ffa2..ba4d946e27 100644 --- a/lib/4.06.1/whole_compiler.ml +++ b/lib/4.06.1/whole_compiler.ml @@ -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 -> @@ -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; @@ -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 *) } @@ -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 diff --git a/res_syntax/src/res_core.ml b/res_syntax/src/res_core.ml index 78d2c384fe..4833d3ebec 100644 --- a/res_syntax/src/res_core.ml +++ b/res_syntax/src/res_core.ml @@ -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 -> @@ -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; @@ -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 *) } @@ -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 diff --git a/res_syntax/tests/parsing/grammar/expressions/UncurriedByDefault.res b/res_syntax/tests/parsing/grammar/expressions/UncurriedByDefault.res index 88a212df99..3b61aa41ac 100644 --- a/res_syntax/tests/parsing/grammar/expressions/UncurriedByDefault.res +++ b/res_syntax/tests/parsing/grammar/expressions/UncurriedByDefault.res @@ -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) @@ -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) diff --git a/res_syntax/tests/parsing/grammar/expressions/expected/UncurriedByDefault.res.txt b/res_syntax/tests/parsing/grammar/expressions/expected/UncurriedByDefault.res.txt index 3cfa51f80b..aff8d8dac9 100644 --- a/res_syntax/tests/parsing/grammar/expressions/expected/UncurriedByDefault.res.txt +++ b/res_syntax/tests/parsing/grammar/expressions/expected/UncurriedByDefault.res.txt @@ -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 ]) @@ -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 \ No newline at end of file +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 ]) \ No newline at end of file diff --git a/res_syntax/tests/parsing/grammar/expressions/expected/uncurried.res.txt b/res_syntax/tests/parsing/grammar/expressions/expected/uncurried.res.txt index ccfd5dc7c7..290944856f 100644 --- a/res_syntax/tests/parsing/grammar/expressions/expected/uncurried.res.txt +++ b/res_syntax/tests/parsing/grammar/expressions/expected/uncurried.res.txt @@ -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 = diff --git a/res_syntax/tests/printer/expr/UncurriedByDefault.res b/res_syntax/tests/printer/expr/UncurriedByDefault.res index 57b68248e0..b10dabd023 100644 --- a/res_syntax/tests/printer/expr/UncurriedByDefault.res +++ b/res_syntax/tests/printer/expr/UncurriedByDefault.res @@ -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) @@ -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) diff --git a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt index 289375f314..0714ad2fc4 100644 --- a/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt +++ b/res_syntax/tests/printer/expr/expected/UncurriedByDefault.res.txt @@ -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) @@ -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)