Skip to content

Commit cff61c6

Browse files
committed
Single source of truth for uncurried ast operations shared with syntax.
1 parent dc5e533 commit cff61c6

File tree

6 files changed

+36
-77
lines changed

6 files changed

+36
-77
lines changed

jscomp/ml/ast_uncurried.ml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ let arityType ~loc arity =
99
[ Rtag ({ txt = encode_arity_string arity; loc }, [], true, []) ]
1010
Closed None
1111

12+
let arityFromType (typ : Parsetree.core_type) =
13+
match typ.ptyp_desc with
14+
| Ptyp_variant ([Rtag ({txt}, _, _, _)], _, _) -> decode_arity_string txt
15+
| _ -> assert false
16+
1217
let uncurriedType ~loc ~arity tArg =
1318
let tArity = arityType ~loc arity in
1419
Ast_helper.Typ.constr ~loc
@@ -58,6 +63,18 @@ let exprExtractUncurriedFun (expr : Parsetree.expression) =
5863
| Pexp_construct ({ txt = Lident "Function$" }, Some e) -> e
5964
| _ -> assert false
6065

66+
let typeIsUncurriedFun (typ : Parsetree.core_type) =
67+
match typ.ptyp_desc with
68+
| Ptyp_constr ({txt = Lident "function$"}, [{ptyp_desc = Ptyp_arrow _}; _]) ->
69+
true
70+
| _ -> false
71+
72+
let typeExtractUncurriedFun (typ : Parsetree.core_type) =
73+
match typ.ptyp_desc with
74+
| Ptyp_constr ({txt = Lident "function$"}, [tArg; tArity]) ->
75+
(arityFromType tArity, tArg)
76+
| _ -> assert false
77+
6178
(* Typed AST *)
6279

6380
let arity_to_type arity =

res_syntax/src/reactjs_jsx_v4.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -752,8 +752,8 @@ let transformStructureItem ~config mapper item =
752752
check_string_int_attribute_iter.structure_item
753753
check_string_int_attribute_iter item;
754754
let pval_type =
755-
if Res_uncurried.typeIsUncurriedFun pval_type then
756-
let _arity, t = Res_uncurried.typeExtractUncurriedFun pval_type in
755+
if Ast_uncurried.typeIsUncurriedFun pval_type then
756+
let _arity, t = Ast_uncurried.typeExtractUncurriedFun pval_type in
757757
t
758758
else pval_type
759759
in
@@ -825,8 +825,8 @@ let transformStructureItem ~config mapper item =
825825
config.hasReactComponent <- true;
826826
let rec removeArityRecord expr =
827827
match expr.pexp_desc with
828-
| _ when Res_uncurried.exprIsUncurriedFun expr ->
829-
Res_uncurried.exprExtractUncurriedFun expr
828+
| _ when Ast_uncurried.exprIsUncurriedFun expr ->
829+
Ast_uncurried.exprExtractUncurriedFun expr
830830
| Pexp_apply (forwardRef, [(label, e)]) ->
831831
{
832832
expr with
@@ -1248,8 +1248,8 @@ let transformSignatureItem ~config _mapper item =
12481248
React_jsx_common.raiseErrorMultipleReactComponent ~loc:psig_loc
12491249
else config.hasReactComponent <- true;
12501250
let pval_type =
1251-
if Res_uncurried.typeIsUncurriedFun pval_type then
1252-
let _arity, t = Res_uncurried.typeExtractUncurriedFun pval_type in
1251+
if Ast_uncurried.typeIsUncurriedFun pval_type then
1252+
let _arity, t = Ast_uncurried.typeExtractUncurriedFun pval_type in
12531253
t
12541254
else pval_type
12551255
in

res_syntax/src/res_core.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ and parseEs6ArrowExpression ?(arrowAttrs = []) ?(arrowStartPos = None) ?context
15991599
&& (termParamNum = 1
16001600
|| not (p.uncurried_config |> Res_uncurried.isDefault))
16011601
then
1602-
(termParamNum - 1, Res_uncurried.uncurriedFun ~loc ~arity funExpr, 1)
1602+
(termParamNum - 1, Ast_uncurried.uncurriedFun ~loc ~arity funExpr, 1)
16031603
else (termParamNum - 1, funExpr, arity + 1)
16041604
| TypeParameter {dotted = _; attrs; locs = newtypes; pos = startPos} ->
16051605
( termParamNum,
@@ -3917,7 +3917,7 @@ and parsePolyTypeExpr p =
39173917
let loc = mkLoc typ.Parsetree.ptyp_loc.loc_start p.prevEndPos in
39183918
let tFun = Ast_helper.Typ.arrow ~loc Asttypes.Nolabel typ returnType in
39193919
if p.uncurried_config |> Res_uncurried.isDefault then
3920-
Res_uncurried.uncurriedType ~loc ~arity:1 tFun
3920+
Ast_uncurried.uncurriedType ~loc ~arity:1 tFun
39213921
else tFun
39223922
| _ -> Ast_helper.Typ.var ~loc:var.loc var.txt)
39233923
| _ -> assert false)
@@ -4267,7 +4267,7 @@ and parseEs6ArrowType ~attrs p =
42674267
then
42684268
let loc = mkLoc startPos endPos in
42694269
let tArg = Ast_helper.Typ.arrow ~loc ~attrs argLbl typ t in
4270-
(paramNum - 1, Res_uncurried.uncurriedType ~loc ~arity tArg, 1)
4270+
(paramNum - 1, Ast_uncurried.uncurriedType ~loc ~arity tArg, 1)
42714271
else
42724272
( paramNum - 1,
42734273
Ast_helper.Typ.arrow ~loc:(mkLoc startPos endPos) ~attrs argLbl
@@ -4330,7 +4330,7 @@ and parseArrowTypeRest ~es6Arrow ~startPos typ p =
43304330
let loc = mkLoc startPos p.prevEndPos in
43314331
let arrowTyp = Ast_helper.Typ.arrow ~loc Asttypes.Nolabel typ returnType in
43324332
if p.uncurried_config |> Res_uncurried.isDefault then
4333-
Res_uncurried.uncurriedType ~loc ~arity:1 arrowTyp
4333+
Ast_uncurried.uncurriedType ~loc ~arity:1 arrowTyp
43344334
else arrowTyp
43354335
| _ -> typ
43364336

res_syntax/src/res_parsetree_viewer.ml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,17 @@ let funExpr expr =
176176
(* If a fun has an attribute, then it stops here and makes currying.
177177
i.e attributes outside of (...), uncurried `(.)` and `async` make currying *)
178178
| {pexp_desc = Pexp_fun _} -> (uncurried, attrsBefore, List.rev acc, expr)
179-
| expr when nFun = 0 && Res_uncurried.exprIsUncurriedFun expr ->
180-
let expr = Res_uncurried.exprExtractUncurriedFun expr in
179+
| expr when nFun = 0 && Ast_uncurried.exprIsUncurriedFun expr ->
180+
let expr = Ast_uncurried.exprExtractUncurriedFun expr in
181181
collect ~uncurried:true ~nFun attrsBefore acc expr
182182
| expr -> (uncurried, attrsBefore, List.rev acc, expr)
183183
in
184184
match expr with
185185
| {pexp_desc = Pexp_fun _} ->
186186
collect ~uncurried:false ~nFun:0 expr.pexp_attributes []
187187
{expr with pexp_attributes = []}
188-
| _ when Res_uncurried.exprIsUncurriedFun expr ->
189-
let expr = Res_uncurried.exprExtractUncurriedFun expr in
188+
| _ when Ast_uncurried.exprIsUncurriedFun expr ->
189+
let expr = Ast_uncurried.exprExtractUncurriedFun expr in
190190
collect ~uncurried:true ~nFun:0 expr.pexp_attributes []
191191
{expr with pexp_attributes = []}
192192
| _ -> collect ~uncurried:false ~nFun:0 [] [] expr
@@ -551,7 +551,7 @@ let partitionPrintableAttributes attrs =
551551
let isFunNewtype expr =
552552
match expr.pexp_desc with
553553
| Pexp_fun _ | Pexp_newtype _ -> true
554-
| _ -> Res_uncurried.exprIsUncurriedFun expr
554+
| _ -> Ast_uncurried.exprIsUncurriedFun expr
555555

556556
let requiresSpecialCallbackPrintingLastArg args =
557557
let rec loop args =

res_syntax/src/res_printer.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
15831583
let doc = printTypExpr ~state n cmtTbl in
15841584
match n.ptyp_desc with
15851585
| Ptyp_arrow _ | Ptyp_tuple _ | Ptyp_alias _ -> addParens doc
1586-
| _ when Res_uncurried.typeIsUncurriedFun n -> addParens doc
1586+
| _ when Ast_uncurried.typeIsUncurriedFun n -> addParens doc
15871587
| _ -> doc
15881588
in
15891589
Doc.group
@@ -1655,8 +1655,8 @@ and printTypExpr ~(state : State.t) (typExpr : Parsetree.core_type) cmtTbl =
16551655
| Ptyp_object (fields, openFlag) ->
16561656
printObject ~state ~inline:false fields openFlag cmtTbl
16571657
| Ptyp_arrow _ -> printArrow ~uncurried:false typExpr
1658-
| Ptyp_constr _ when Res_uncurried.typeIsUncurriedFun typExpr ->
1659-
let arity, tArg = Res_uncurried.typeExtractUncurriedFun typExpr in
1658+
| Ptyp_constr _ when Ast_uncurried.typeIsUncurriedFun typExpr ->
1659+
let arity, tArg = Ast_uncurried.typeExtractUncurriedFun typExpr in
16601660
printArrow ~uncurried:true ~arity tArg
16611661
| Ptyp_constr (longidentLoc, [{ptyp_desc = Ptyp_object (fields, openFlag)}])
16621662
->
@@ -2678,7 +2678,7 @@ and printExpression ~state (e : Parsetree.expression) cmtTbl =
26782678
printExpressionWithComments ~state
26792679
(ParsetreeViewer.rewriteUnderscoreApply e)
26802680
cmtTbl
2681-
| _ when Res_uncurried.exprIsUncurriedFun e -> printArrow e
2681+
| _ when Ast_uncurried.exprIsUncurriedFun e -> printArrow e
26822682
| Pexp_fun _ | Pexp_newtype _ -> printArrow e
26832683
| Parsetree.Pexp_constant c ->
26842684
printConstant ~templateLiteral:(ParsetreeViewer.isTemplateLiteral e) c

res_syntax/src/res_uncurried.ml

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,3 @@ let fromDotted ~dotted = function
1515
let getDotted ~uncurried = function
1616
| Legacy -> uncurried
1717
| Default -> not uncurried
18-
19-
let encode_arity_string arity = "Has_arity" ^ string_of_int arity
20-
let decode_arity_string arity_s =
21-
(int_of_string [@doesNotRaise])
22-
((String.sub [@doesNotRaise]) arity_s 9 (String.length arity_s - 9))
23-
24-
let arityType ~loc arity =
25-
Ast_helper.Typ.variant ~loc
26-
[Rtag ({txt = encode_arity_string arity; loc}, [], true, [])]
27-
Closed None
28-
29-
let arityFromType (typ : Parsetree.core_type) =
30-
match typ.ptyp_desc with
31-
| Ptyp_variant ([Rtag ({txt}, _, _, _)], _, _) -> decode_arity_string txt
32-
| _ -> assert false
33-
34-
let uncurriedType ~loc ~arity tArg =
35-
let tArity = arityType ~loc arity in
36-
Ast_helper.Typ.constr ~loc {txt = Lident "function$"; loc} [tArg; tArity]
37-
38-
let arity_to_attributes arity =
39-
[
40-
( Location.mknoloc "res.arity",
41-
Parsetree.PStr
42-
[
43-
Ast_helper.Str.eval
44-
(Ast_helper.Exp.constant
45-
(Pconst_integer (string_of_int arity, None)));
46-
] );
47-
]
48-
49-
let uncurriedFun ~loc ~arity funExpr =
50-
Ast_helper.Exp.construct ~loc
51-
~attrs:(arity_to_attributes arity)
52-
{txt = Lident "Function$"; loc}
53-
(Some funExpr)
54-
55-
let exprIsUncurriedFun (expr : Parsetree.expression) =
56-
match expr.pexp_desc with
57-
| Pexp_construct ({txt = Lident "Function$"}, Some _) -> true
58-
| _ -> false
59-
60-
let exprExtractUncurriedFun (expr : Parsetree.expression) =
61-
match expr.pexp_desc with
62-
| Pexp_construct ({txt = Lident "Function$"}, Some e) -> e
63-
| _ -> assert false
64-
65-
let typeIsUncurriedFun (typ : Parsetree.core_type) =
66-
match typ.ptyp_desc with
67-
| Ptyp_constr ({txt = Lident "function$"}, [{ptyp_desc = Ptyp_arrow _}; _]) ->
68-
true
69-
| _ -> false
70-
71-
let typeExtractUncurriedFun (typ : Parsetree.core_type) =
72-
match typ.ptyp_desc with
73-
| Ptyp_constr ({txt = Lident "function$"}, [tArg; tArity]) ->
74-
(arityFromType tArity, tArg)
75-
| _ -> assert false

0 commit comments

Comments
 (0)