Skip to content

Commit 6e345ff

Browse files
committedDec 2, 2022
Refactor: move handling of uncurried types for parser/printer to Res_uncurried.
1 parent ac55bcd commit 6e345ff

12 files changed

+539
-479
lines changed
 

‎lib/4.06.1/unstable/js_compiler.ml

+119-89
Original file line numberDiff line numberDiff line change
@@ -49496,6 +49496,77 @@ let debug t =
4949649496
toString ~width:10 doc |> print_endline
4949749497
[@@live]
4949849498

49499+
end
49500+
module Res_uncurried
49501+
= struct
49502+
#1 "res_uncurried.ml"
49503+
type config = Legacy | Default
49504+
49505+
let init = Legacy
49506+
49507+
let isDefault = function
49508+
| Legacy -> false
49509+
| Default -> true
49510+
49511+
(* For parsing *)
49512+
let fromDotted ~dotted = function
49513+
| Legacy -> dotted
49514+
| Default -> not dotted
49515+
49516+
(* For printing *)
49517+
let getDotted ~uncurried = function
49518+
| Legacy -> uncurried
49519+
| Default -> not uncurried
49520+
49521+
let uncurriedType ~loc ~arity tArg =
49522+
Ast_helper.Typ.constr ~loc
49523+
{txt = Ldot (Ldot (Lident "Js", "Fn"), "arity" ^ string_of_int arity); loc}
49524+
[tArg]
49525+
49526+
let uncurriedFun ~loc ~arity funExpr =
49527+
Ast_helper.Exp.record ~loc
49528+
[
49529+
( {txt = Ldot (Ldot (Lident "Js", "Fn"), "I" ^ string_of_int arity); loc},
49530+
funExpr );
49531+
]
49532+
None
49533+
49534+
let exprIsUncurriedFun (expr : Parsetree.expression) =
49535+
match expr with
49536+
| {
49537+
pexp_desc =
49538+
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, _e)], None);
49539+
} ->
49540+
true
49541+
| _ -> false
49542+
49543+
let exprExtractUncurriedFun (expr : Parsetree.expression) =
49544+
match expr with
49545+
| {
49546+
pexp_desc =
49547+
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, e)], None);
49548+
} ->
49549+
e
49550+
| _ -> assert false
49551+
49552+
let typeIsUncurriedFun (typ : Parsetree.core_type) =
49553+
match typ.ptyp_desc with
49554+
| Ptyp_constr
49555+
({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, [{ptyp_desc = Ptyp_arrow _}])
49556+
->
49557+
true
49558+
| _ -> false
49559+
49560+
let typeExtractUncurriedFun (typ : Parsetree.core_type) =
49561+
match typ.ptyp_desc with
49562+
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), arity)}, [tArg]) ->
49563+
let arity =
49564+
(int_of_string [@doesNotRaise])
49565+
((String.sub [@doesNotRaise]) arity 5 (String.length arity - 5))
49566+
in
49567+
(arity, tArg)
49568+
| _ -> assert false
49569+
4949949570
end
4950049571
module Res_parsetree_viewer : sig
4950149572
#1 "res_parsetree_viewer.mli"
@@ -49659,7 +49730,7 @@ val hasIfLetAttribute : Parsetree.attributes -> bool
4965949730

4966049731
val isRewrittenUnderscoreApplySugar : Parsetree.expression -> bool
4966149732

49662-
val isFunNewtype : Parsetree.expression_desc -> bool
49733+
val isFunNewtype : Parsetree.expression -> bool
4966349734

4966449735
end = struct
4966549736
#1 "res_parsetree_viewer.ml"
@@ -49841,22 +49912,17 @@ let funExpr expr =
4984149912
(* If a fun has an attribute, then it stops here and makes currying.
4984249913
i.e attributes outside of (...), uncurried `(.)` and `async` make currying *)
4984349914
| {pexp_desc = Pexp_fun _} -> (uncurried, attrsBefore, List.rev acc, expr)
49844-
| {
49845-
pexp_desc =
49846-
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, expr)], None);
49847-
}
49848-
when nFun = 0 ->
49915+
| expr when nFun = 0 && Res_uncurried.exprIsUncurriedFun expr ->
49916+
let expr = Res_uncurried.exprExtractUncurriedFun expr in
4984949917
collect ~uncurried:true ~nFun attrsBefore acc expr
4985049918
| expr -> (uncurried, attrsBefore, List.rev acc, expr)
4985149919
in
4985249920
match expr with
4985349921
| {pexp_desc = Pexp_fun _} ->
4985449922
collect ~uncurried:false ~nFun:0 expr.pexp_attributes []
4985549923
{expr with pexp_attributes = []}
49856-
| {
49857-
pexp_desc =
49858-
Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, expr)], None);
49859-
} ->
49924+
| _ when Res_uncurried.exprIsUncurriedFun expr ->
49925+
let expr = Res_uncurried.exprExtractUncurriedFun expr in
4986049926
collect ~uncurried:true ~nFun:0 expr.pexp_attributes []
4986149927
{expr with pexp_attributes = []}
4986249928
| _ -> collect ~uncurried:false ~nFun:0 [] [] expr
@@ -50218,19 +50284,17 @@ let filterPrintableAttributes attrs = List.filter isPrintableAttribute attrs
5021850284
let partitionPrintableAttributes attrs =
5021950285
List.partition isPrintableAttribute attrs
5022050286

50221-
let isFunNewtype = function
50287+
let isFunNewtype expr =
50288+
match expr.pexp_desc with
5022250289
| Pexp_fun _ | Pexp_newtype _ -> true
50223-
| Pexp_record ([({txt = Ldot (Ldot (Lident "Js", "Fn"), name)}, _)], None)
50224-
when String.length name >= 1 && (name.[0] [@doesNotRaise]) = 'I' ->
50225-
true
50226-
| _ -> false
50290+
| _ -> Res_uncurried.exprIsUncurriedFun expr
5022750291

5022850292
let requiresSpecialCallbackPrintingLastArg args =
5022950293
let rec loop args =
5023050294
match args with
5023150295
| [] -> false
50232-
| [(_, {pexp_desc})] when isFunNewtype pexp_desc -> true
50233-
| (_, {pexp_desc}) :: _ when isFunNewtype pexp_desc -> false
50296+
| [(_, expr)] when isFunNewtype expr -> true
50297+
| (_, expr) :: _ when isFunNewtype expr -> false
5023450298
| _ :: rest -> loop rest
5023550299
in
5023650300
loop args
@@ -50239,12 +50303,12 @@ let requiresSpecialCallbackPrintingFirstArg args =
5023950303
let rec loop args =
5024050304
match args with
5024150305
| [] -> true
50242-
| (_, {pexp_desc}) :: _ when isFunNewtype pexp_desc -> false
50306+
| (_, expr) :: _ when isFunNewtype expr -> false
5024350307
| _ :: rest -> loop rest
5024450308
in
5024550309
match args with
50246-
| [(_, {pexp_desc})] when isFunNewtype pexp_desc -> false
50247-
| (_, {pexp_desc}) :: rest when isFunNewtype pexp_desc -> loop rest
50310+
| [(_, expr)] when isFunNewtype expr -> false
50311+
| (_, expr) :: rest when isFunNewtype expr -> loop rest
5024850312
| _ -> false
5024950313

5025050314
let modExprApply modExpr =
@@ -52612,7 +52676,7 @@ let ternaryOperand expr =
5261252676
} ->
5261352677
Nothing
5261452678
| {pexp_desc = Pexp_constraint _} -> Parenthesized
52615-
| {pexp_desc} when Res_parsetree_viewer.isFunNewtype pexp_desc -> (
52679+
| _ when Res_parsetree_viewer.isFunNewtype expr -> (
5261652680
let _uncurried, _attrsOnArrow, _parameters, returnExpr =
5261752681
ParsetreeViewer.funExpr expr
5261852682
in
@@ -53029,28 +53093,6 @@ let isKeywordTxt str =
5302953093

5303053094
let catch = Lident "catch"
5303153095

53032-
end
53033-
module Res_uncurried
53034-
= struct
53035-
#1 "res_uncurried.ml"
53036-
type t = Legacy | Default
53037-
53038-
let init = Legacy
53039-
53040-
let isDefault = function
53041-
| Legacy -> false
53042-
| Default -> true
53043-
53044-
(* For parsing *)
53045-
let fromDotted ~dotted = function
53046-
| Legacy -> dotted
53047-
| Default -> not dotted
53048-
53049-
(* For printing *)
53050-
let getDotted ~uncurried = function
53051-
| Legacy -> uncurried
53052-
| Default -> not uncurried
53053-
5305453096
end
5305553097
module Res_utf8 : sig
5305653098
#1 "res_utf8.mli"
@@ -53819,9 +53861,9 @@ let printOptionalLabel attrs =
5381953861
module State = struct
5382053862
let customLayoutThreshold = 2
5382153863

53822-
type t = {customLayout: int; mutable res_uncurried: Res_uncurried.t}
53864+
type t = {customLayout: int; mutable uncurried_config: Res_uncurried.config}
5382353865

53824-
let init = {customLayout = 0; res_uncurried = Res_uncurried.init}
53866+
let init = {customLayout = 0; uncurried_config = Res_uncurried.init}
5382553867

5382653868
let nextCustomLayout t = {t with customLayout = t.customLayout + 1}
5382753869

@@ -54797,7 +54839,9 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
5479754839
ParsetreeViewer.arrowType ~arity typExpr
5479854840
in
5479954841
let dotted, attrsBefore =
54800-
let dotted = state.res_uncurried |> Res_uncurried.getDotted ~uncurried in
54842+
let dotted =
54843+
state.uncurried_config |> Res_uncurried.getDotted ~uncurried
54844+
in
5480154845
(* Converting .ml code to .res requires processing uncurried attributes *)
5480254846
let hasBs, attrs = ParsetreeViewer.processBsAttribute attrsBefore in
5480354847
(dotted || hasBs, attrs)
@@ -54824,11 +54868,8 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
5482454868
let typDoc =
5482554869
let doc = printTypExpr ~state n cmtTbl in
5482654870
match n.ptyp_desc with
54827-
| Ptyp_constr
54828-
( {txt = Ldot (Ldot (Lident "Js", "Fn"), _)},
54829-
[{ptyp_desc = Ptyp_arrow _}] )
54830-
| Ptyp_arrow _ | Ptyp_tuple _ | Ptyp_alias _ ->
54831-
addParens doc
54871+
| Ptyp_arrow _ | Ptyp_tuple _ | Ptyp_alias _ -> addParens doc
54872+
| _ when Res_uncurried.typeIsUncurriedFun n -> addParens doc
5483254873
| _ -> doc
5483354874
in
5483454875
Doc.group
@@ -54900,13 +54941,8 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
5490054941
| Ptyp_object (fields, openFlag) ->
5490154942
printObject ~state ~inline:false fields openFlag cmtTbl
5490254943
| Ptyp_arrow _ -> printArrow ~uncurried:false typExpr
54903-
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), arity)}, [tArg])
54904-
when String.length arity >= 5
54905-
&& (String.sub [@doesNotRaise]) arity 0 5 = "arity" ->
54906-
let arity =
54907-
(int_of_string [@doesNotRaise])
54908-
((String.sub [@doesNotRaise]) arity 5 (String.length arity - 5))
54909-
in
54944+
| Ptyp_constr _ when Res_uncurried.typeIsUncurriedFun typExpr ->
54945+
let arity, tArg = Res_uncurried.typeExtractUncurriedFun typExpr in
5491054946
printArrow ~uncurried:true ~arity tArg
5491154947
| Ptyp_constr (longidentLoc, [{ptyp_desc = Ptyp_object (fields, openFlag)}])
5491254948
->
@@ -55919,6 +55955,17 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl =
5591955955
in
5592055956
let printedExpression =
5592155957
match e.pexp_desc with
55958+
| Pexp_fun
55959+
( Nolabel,
55960+
None,
55961+
{ppat_desc = Ppat_var {txt = "__x"}},
55962+
{pexp_desc = Pexp_apply _} ) ->
55963+
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
55964+
printExpressionWithComments ~state
55965+
(ParsetreeViewer.rewriteUnderscoreApply e)
55966+
cmtTbl
55967+
| _ when Res_uncurried.exprIsUncurriedFun e -> printArrow e
55968+
| Pexp_fun _ | Pexp_newtype _ -> printArrow e
5592255969
| Parsetree.Pexp_constant c ->
5592355970
printConstant ~templateLiteral:(ParsetreeViewer.isTemplateLiteral e) c
5592455971
| Pexp_construct _ when ParsetreeViewer.hasJsxAttribute e.pexp_attributes ->
@@ -56165,24 +56212,6 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl =
5616556212
]
5616656213
in
5616756214
Doc.group (Doc.concat [variantName; args])
56168-
| Pexp_fun
56169-
( Nolabel,
56170-
None,
56171-
{ppat_desc = Ppat_var {txt = "__x"}},
56172-
{pexp_desc = Pexp_apply _} ) ->
56173-
(* (__x) => f(a, __x, c) -----> f(a, _, c) *)
56174-
printExpressionWithComments ~state
56175-
(ParsetreeViewer.rewriteUnderscoreApply e)
56176-
cmtTbl
56177-
| Pexp_fun _
56178-
| Pexp_record
56179-
( [
56180-
( {txt = Ldot (Ldot (Lident "Js", "Fn"), _)},
56181-
{pexp_desc = Pexp_fun _} );
56182-
],
56183-
None )
56184-
| Pexp_newtype _ ->
56185-
printArrow e
5618656215
| Pexp_record (rows, spreadExpr) ->
5618756216
if rows = [] then
5618856217
Doc.concat
@@ -57198,7 +57227,7 @@ and printPexpApply ~state expr cmtTbl =
5719857227
let uncurried, attrs =
5719957228
ParsetreeViewer.processUncurriedAppAttribute expr.pexp_attributes
5720057229
in
57201-
let dotted = state.res_uncurried |> Res_uncurried.getDotted ~uncurried in
57230+
let dotted = state.uncurried_config |> Res_uncurried.getDotted ~uncurried in
5720257231
let callExprDoc =
5720357232
let doc = printExpressionWithComments ~state callExpr cmtTbl in
5720457233
match Parens.callExpr callExpr with
@@ -57973,7 +58002,7 @@ and printCase ~state (case : Parsetree.case) cmtTbl =
5797358002

5797458003
and printExprFunParameters ~state ~inCallback ~async ~uncurried ~hasConstraint
5797558004
parameters cmtTbl =
57976-
let dotted = state.res_uncurried |> Res_uncurried.getDotted ~uncurried in
58005+
let dotted = state.uncurried_config |> Res_uncurried.getDotted ~uncurried in
5797758006
match parameters with
5797858007
(* let f = _ => () *)
5797958008
| [
@@ -58520,10 +58549,10 @@ and printAttribute ?(standalone = false) ~state
5852058549
let id =
5852158550
match id.txt with
5852258551
| "uncurried" ->
58523-
state.res_uncurried <- Res_uncurried.Default;
58552+
state.uncurried_config <- Res_uncurried.Default;
5852458553
id
5852558554
| "toUncurried" ->
58526-
state.res_uncurried <- Res_uncurried.Default;
58555+
state.uncurried_config <- Res_uncurried.Default;
5852758556
{id with txt = "uncurried"}
5852858557
| _ -> id
5852958558
in
@@ -154127,9 +154156,10 @@ let transformStructureItem ~config mapper item =
154127154156
check_string_int_attribute_iter.structure_item
154128154157
check_string_int_attribute_iter item;
154129154158
let pval_type =
154130-
match pval_type.ptyp_desc with
154131-
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, [t]) -> t
154132-
| _ -> pval_type
154159+
if Res_uncurried.typeIsUncurriedFun pval_type then
154160+
let _arity, t = Res_uncurried.typeExtractUncurriedFun pval_type in
154161+
t
154162+
else pval_type
154133154163
in
154134154164
let coreTypeOfAttr = React_jsx_common.coreTypeOfAttrs pval_attributes in
154135154165
let typVarsOfCoreType =
@@ -154199,9 +154229,8 @@ let transformStructureItem ~config mapper item =
154199154229
config.hasReactComponent <- true;
154200154230
let rec removeArityRecord expr =
154201154231
match expr.pexp_desc with
154202-
| Pexp_record
154203-
([({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, e)], None) ->
154204-
e
154232+
| _ when Res_uncurried.exprIsUncurriedFun expr ->
154233+
Res_uncurried.exprExtractUncurriedFun expr
154205154234
| Pexp_apply (forwardRef, [(label, e)]) ->
154206154235
{
154207154236
expr with
@@ -154626,9 +154655,10 @@ let transformSignatureItem ~config _mapper item =
154626154655
React_jsx_common.raiseErrorMultipleReactComponent ~loc:psig_loc
154627154656
else config.hasReactComponent <- true;
154628154657
let pval_type =
154629-
match pval_type.ptyp_desc with
154630-
| Ptyp_constr ({txt = Ldot (Ldot (Lident "Js", "Fn"), _)}, [t]) -> t
154631-
| _ -> pval_type
154658+
if Res_uncurried.typeIsUncurriedFun pval_type then
154659+
let _arity, t = Res_uncurried.typeExtractUncurriedFun pval_type in
154660+
t
154661+
else pval_type
154632154662
in
154633154663
check_string_int_attribute_iter.signature_item
154634154664
check_string_int_attribute_iter item;

0 commit comments

Comments
 (0)