Skip to content

Commit 1c05bb8

Browse files
committed
Fix issue in printing uncurried callbacks.
They did not print the same way as curried ones.
1 parent b55d5e7 commit 1c05bb8

11 files changed

+277
-154
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ These are only breaking changes for unformatted code.
3838

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
41+
- Fix issue in printing uncurried callbacks
4142

4243
#### :nail_care: Polish
4344

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -49434,7 +49434,7 @@ val processBsAttribute : Parsetree.attributes -> bool * Parsetree.attributes
4943449434

4943549435
type functionAttributesInfo = {
4943649436
async: bool;
49437-
uncurried: bool;
49437+
bs: bool;
4943849438
attributes: Parsetree.attributes;
4943949439
}
4944049440

@@ -49470,7 +49470,7 @@ type funParamKind =
4947049470

4947149471
val funExpr :
4947249472
Parsetree.expression ->
49473-
Parsetree.attributes * funParamKind list * Parsetree.expression
49473+
bool * Parsetree.attributes * funParamKind list * Parsetree.expression
4947449474

4947549475
(* example:
4947649476
* `makeCoordinate({
@@ -49570,6 +49570,8 @@ val hasIfLetAttribute : Parsetree.attributes -> bool
4957049570

4957149571
val isRewrittenUnderscoreApplySugar : Parsetree.expression -> bool
4957249572

49573+
val isFunNewtype : Parsetree.expression_desc -> bool
49574+
4957349575
end = struct
4957449576
#1 "res_parsetree_viewer.ml"
4957549577
open Parsetree
@@ -49633,18 +49635,17 @@ let processBsAttribute attrs =
4963349635

4963449636
type functionAttributesInfo = {
4963549637
async: bool;
49636-
uncurried: bool;
49638+
bs: bool;
4963749639
attributes: Parsetree.attributes;
4963849640
}
4963949641

4964049642
let processFunctionAttributes attrs =
49641-
let rec process async uncurried acc attrs =
49643+
let rec process async bs acc attrs =
4964249644
match attrs with
49643-
| [] -> {async; uncurried; attributes = List.rev acc}
49645+
| [] -> {async; bs; attributes = List.rev acc}
4964449646
| ({Location.txt = "bs"}, _) :: rest -> process async true acc rest
49645-
| ({Location.txt = "res.async"}, _) :: rest ->
49646-
process true uncurried acc rest
49647-
| attr :: rest -> process async uncurried (attr :: acc) rest
49647+
| ({Location.txt = "res.async"}, _) :: rest -> process true bs acc rest
49648+
| attr :: rest -> process async bs (attr :: acc) rest
4964849649
in
4964949650
process false false [] attrs
4965049651

@@ -49711,7 +49712,7 @@ let funExpr expr =
4971149712
collectNewTypes (stringLoc :: acc) returnExpr
4971249713
| returnExpr -> (List.rev acc, returnExpr)
4971349714
in
49714-
let rec collect attrsBefore acc expr =
49715+
let rec collect ~uncurried attrsBefore acc expr =
4971549716
match expr with
4971649717
| {
4971749718
pexp_desc =
@@ -49721,29 +49722,33 @@ let funExpr expr =
4972149722
{ppat_desc = Ppat_var {txt = "__x"}},
4972249723
{pexp_desc = Pexp_apply _} );
4972349724
} ->
49724-
(attrsBefore, List.rev acc, rewriteUnderscoreApply expr)
49725+
(uncurried, attrsBefore, List.rev acc, rewriteUnderscoreApply expr)
4972549726
| {pexp_desc = Pexp_newtype (stringLoc, rest); pexp_attributes = attrs} ->
4972649727
let stringLocs, returnExpr = collectNewTypes [stringLoc] rest in
4972749728
let param = NewTypes {attrs; locs = stringLocs} in
49728-
collect attrsBefore (param :: acc) returnExpr
49729+
collect ~uncurried attrsBefore (param :: acc) returnExpr
4972949730
| {
4973049731
pexp_desc = Pexp_fun (lbl, defaultExpr, pattern, returnExpr);
4973149732
pexp_attributes = [];
4973249733
} ->
4973349734
let parameter = Parameter {attrs = []; lbl; defaultExpr; pat = pattern} in
49734-
collect attrsBefore (parameter :: acc) returnExpr
49735+
collect ~uncurried attrsBefore (parameter :: acc) returnExpr
4973549736
(* If a fun has an attribute, then it stops here and makes currying.
4973649737
i.e attributes outside of (...), uncurried `(.)` and `async` make currying *)
49737-
| {pexp_desc = Pexp_fun _} -> (attrsBefore, List.rev acc, expr)
49738-
| expr -> (attrsBefore, List.rev acc, expr)
49738+
| {pexp_desc = Pexp_fun _} -> (uncurried, attrsBefore, List.rev acc, expr)
49739+
| expr -> (uncurried, attrsBefore, List.rev acc, expr)
4973949740
in
4974049741
match expr with
49742+
| {pexp_desc = Pexp_fun _} ->
49743+
collect ~uncurried:false expr.pexp_attributes []
49744+
{expr with pexp_attributes = []}
4974149745
| {
49742-
pexp_desc = Pexp_fun (_, _defaultExpr, _pattern, _returnExpr);
49743-
pexp_attributes = attrs;
49744-
} as expr ->
49745-
collect attrs [] {expr with pexp_attributes = []}
49746-
| expr -> collect [] [] expr
49746+
pexp_desc =
49747+
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, expr)], None);
49748+
} ->
49749+
collect ~uncurried:true expr.pexp_attributes []
49750+
{expr with pexp_attributes = []}
49751+
| _ -> collect ~uncurried:false [] [] expr
4974749752

4974849753
let processBracesAttr expr =
4974949754
match expr.pexp_attributes with
@@ -50102,12 +50107,19 @@ let filterPrintableAttributes attrs = List.filter isPrintableAttribute attrs
5010250107
let partitionPrintableAttributes attrs =
5010350108
List.partition isPrintableAttribute attrs
5010450109

50110+
let isFunNewtype = function
50111+
| Pexp_fun _ | Pexp_newtype _ -> true
50112+
| Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), name)}, _)], None)
50113+
when String.length name >= 1 && name.[0] = 'I' ->
50114+
true
50115+
| _ -> false
50116+
5010550117
let requiresSpecialCallbackPrintingLastArg args =
5010650118
let rec loop args =
5010750119
match args with
5010850120
| [] -> false
50109-
| [(_, {pexp_desc = Pexp_fun _ | Pexp_newtype _})] -> true
50110-
| (_, {pexp_desc = Pexp_fun _ | Pexp_newtype _}) :: _ -> false
50121+
| [(_, {pexp_desc})] when isFunNewtype pexp_desc -> true
50122+
| (_, {pexp_desc}) :: _ when isFunNewtype pexp_desc -> false
5011150123
| _ :: rest -> loop rest
5011250124
in
5011350125
loop args
@@ -50116,12 +50128,12 @@ let requiresSpecialCallbackPrintingFirstArg args =
5011650128
let rec loop args =
5011750129
match args with
5011850130
| [] -> true
50119-
| (_, {pexp_desc = Pexp_fun _ | Pexp_newtype _}) :: _ -> false
50131+
| (_, {pexp_desc}) :: _ when isFunNewtype pexp_desc -> false
5012050132
| _ :: rest -> loop rest
5012150133
in
5012250134
match args with
50123-
| [(_, {pexp_desc = Pexp_fun _ | Pexp_newtype _})] -> false
50124-
| (_, {pexp_desc = Pexp_fun _ | Pexp_newtype _}) :: rest -> loop rest
50135+
| [(_, {pexp_desc})] when isFunNewtype pexp_desc -> false
50136+
| (_, {pexp_desc}) :: rest when isFunNewtype pexp_desc -> loop rest
5012550137
| _ -> false
5012650138

5012750139
let modExprApply modExpr =
@@ -52489,8 +52501,8 @@ let ternaryOperand expr =
5248952501
} ->
5249052502
Nothing
5249152503
| {pexp_desc = Pexp_constraint _} -> Parenthesized
52492-
| {pexp_desc = Pexp_fun _ | Pexp_newtype _} -> (
52493-
let _attrsOnArrow, _parameters, returnExpr =
52504+
| {pexp_desc} when Res_parsetree_viewer.isFunNewtype pexp_desc -> (
52505+
let _uncurried, _attrsOnArrow, _parameters, returnExpr =
5249452506
ParsetreeViewer.funExpr expr
5249552507
in
5249652508
match returnExpr.pexp_desc with
@@ -54648,7 +54660,6 @@ and printLabelDeclaration ~state (ld : Parsetree.label_declaration) cmtTbl =
5464854660

5464954661
and printTypExpr ~state (typExpr : Parsetree.core_type) cmtTbl =
5465054662
let printArrow ~uncurried ?(arity = max_int) typExpr =
54651-
(* XXX *)
5465254663
let attrsBefore, args, returnType =
5465354664
ParsetreeViewer.arrowType ~arity typExpr
5465454665
in
@@ -55065,7 +55076,9 @@ and printValueBinding ~state ~recFlag (vb : Parsetree.value_binding) cmtTbl i =
5506555076
};
5506655077
pvb_expr = {pexp_desc = Pexp_newtype _} as expr;
5506755078
} -> (
55068-
let _attrs, parameters, returnExpr = ParsetreeViewer.funExpr expr in
55079+
let _uncurried, _attrs, parameters, returnExpr =
55080+
ParsetreeViewer.funExpr expr
55081+
in
5506955082
let abstractType =
5507055083
match parameters with
5507155084
| [NewTypes {locs = vars}] ->
@@ -55703,12 +55716,14 @@ and printIfChain ~state pexp_attributes ifs elseExpr cmtTbl =
5570355716
Doc.concat [printAttributes ~state attrs cmtTbl; ifDocs; elseDoc]
5570455717

5570555718
and printExpression ~state (e : Parsetree.expression) cmtTbl =
55706-
let printArrow ~isUncurried e =
55707-
let attrsOnArrow, parameters, returnExpr = ParsetreeViewer.funExpr e in
55708-
let ParsetreeViewer.{async; uncurried; attributes = attrs} =
55719+
let printArrow e =
55720+
let uncurried, attrsOnArrow, parameters, returnExpr =
55721+
ParsetreeViewer.funExpr e
55722+
in
55723+
let ParsetreeViewer.{async; bs; attributes = attrs} =
5570955724
ParsetreeViewer.processFunctionAttributes attrsOnArrow
5571055725
in
55711-
let uncurried = uncurried || isUncurried in
55726+
let uncurried = uncurried || bs in
5571255727
let returnExpr, typConstraint =
5571355728
match returnExpr.pexp_desc with
5571455729
| Pexp_constraint (expr, typ) ->
@@ -56033,11 +56048,15 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl =
5603356048
printExpressionWithComments ~state
5603456049
(ParsetreeViewer.rewriteUnderscoreApply e)
5603556050
cmtTbl
56036-
| Pexp_fun _ | Pexp_newtype _ -> printArrow ~isUncurried:false e
56051+
| Pexp_fun _
5603756052
| Pexp_record
56038-
([({txt = Ldot (Ldot (Lident "Js", "Fn"), name)}, funExpr)], None)
56039-
when String.length name >= 1 && name.[0] = 'I' ->
56040-
printArrow ~isUncurried:true funExpr
56053+
( [
56054+
( {txt = Ldot (Ldot (Lident "Js", "Fn"), _)},
56055+
{pexp_desc = Pexp_fun _} );
56056+
],
56057+
None )
56058+
| Pexp_newtype _ ->
56059+
printArrow e
5604156060
| Pexp_record (rows, spreadExpr) ->
5604256061
if rows = [] then
5604356062
Doc.concat
@@ -56411,10 +56430,13 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl =
5641156430
| _ -> exprWithAwait
5641256431

5641356432
and printPexpFun ~state ~inCallback e cmtTbl =
56414-
let attrsOnArrow, parameters, returnExpr = ParsetreeViewer.funExpr e in
56415-
let ParsetreeViewer.{async; uncurried; attributes = attrs} =
56433+
let uncurried, attrsOnArrow, parameters, returnExpr =
56434+
ParsetreeViewer.funExpr e
56435+
in
56436+
let ParsetreeViewer.{async; bs; attributes = attrs} =
5641656437
ParsetreeViewer.processFunctionAttributes attrsOnArrow
5641756438
in
56439+
let uncurried = bs || uncurried in
5641856440
let returnExpr, typConstraint =
5641956441
match returnExpr.pexp_desc with
5642056442
| Pexp_constraint (expr, typ) ->

0 commit comments

Comments
 (0)