Skip to content

Commit 57e546c

Browse files
committed
Remove function converter.
1 parent 568a0fb commit 57e546c

File tree

13 files changed

+23
-315
lines changed

13 files changed

+23
-315
lines changed

jscomp/gentype/Converter.ml

Lines changed: 3 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ open GenTypeCommon
22

33
type t =
44
| CircularC of string * t
5-
| FunctionC of functionC
65
| IdentC
76
| OptionC of t
87
| PromiseC of t
@@ -12,41 +11,9 @@ and groupedArgConverter =
1211
| ArgConverter of t
1312
| GroupConverter of (string * optional * t) list
1413

15-
and functionC = {
16-
funArgConverters: groupedArgConverter list;
17-
componentName: string option;
18-
isHook: bool;
19-
retConverter: t;
20-
typeVars: string list;
21-
uncurried: bool;
22-
}
23-
2414
let rec toString converter =
2515
match converter with
2616
| CircularC (s, c) -> "circular(" ^ s ^ " " ^ toString c ^ ")"
27-
| FunctionC {funArgConverters; retConverter; uncurried} ->
28-
"fn"
29-
^ (match uncurried with
30-
| true -> "Uncurried"
31-
| false -> "")
32-
^ "("
33-
^ (funArgConverters
34-
|> List.map (fun groupedArgConverter ->
35-
match groupedArgConverter with
36-
| ArgConverter conv -> "(" ^ "_" ^ ":" ^ toString conv ^ ")"
37-
| GroupConverter groupConverters ->
38-
"{|"
39-
^ (groupConverters
40-
|> List.map (fun (s, optional, argConverter) ->
41-
s
42-
^ (match optional = Optional with
43-
| true -> "?"
44-
| false -> "")
45-
^ ":" ^ toString argConverter)
46-
|> String.concat ", ")
47-
^ "|}")
48-
|> String.concat ", ")
49-
^ " -> " ^ toString retConverter ^ ")"
5017
| IdentC -> "id"
5118
| OptionC c -> "option(" ^ toString c ^ ")"
5219
| PromiseC c -> "promise(" ^ toString c ^ ")"
@@ -63,29 +30,12 @@ let typeGetConverterNormalized ~config ~inline ~lookupId ~typeNameIsInterface
6330
let _, tNormalized = t |> visit ~visited in
6431
(IdentC, Array (tNormalized, mutable_))
6532
| Dict _ -> (IdentC, normalized_)
66-
| Function
67-
({argTypes; componentName; retType; typeVars; uncurried} as function_)
68-
->
33+
| Function ({argTypes; retType} as function_) ->
6934
let argConverted =
7035
argTypes |> List.map (argTypeToGroupedArgConverter ~visited)
7136
in
72-
let funArgConverters = argConverted |> List.map fst in
73-
let retConverter, retNormalized = retType |> visit ~visited in
74-
let isHook =
75-
match argTypes with
76-
| [{aType = Object (_, fields)}] ->
77-
retType |> EmitType.isTypeFunctionComponent ~fields
78-
| _ -> false
79-
in
80-
( FunctionC
81-
{
82-
funArgConverters;
83-
componentName;
84-
isHook;
85-
retConverter;
86-
typeVars;
87-
uncurried;
88-
},
37+
let _, retNormalized = retType |> visit ~visited in
38+
( IdentC,
8939
Function
9040
{
9141
function_ with
@@ -215,15 +165,6 @@ let typeGetNormalized ~config ~inline ~lookupId ~typeNameIsInterface type_ =
215165
let rec converterIsIdentity ~config ~toJS converter =
216166
match converter with
217167
| CircularC (_, c) -> c |> converterIsIdentity ~config ~toJS
218-
| FunctionC {funArgConverters; retConverter; uncurried} ->
219-
retConverter |> converterIsIdentity ~config ~toJS
220-
&& ((not toJS) || uncurried || funArgConverters |> List.length <= 1)
221-
&& funArgConverters
222-
|> List.for_all (fun groupedArgConverter ->
223-
match groupedArgConverter with
224-
| ArgConverter argConverter ->
225-
argConverter |> converterIsIdentity ~config ~toJS:(not toJS)
226-
| GroupConverter _ -> false)
227168
| IdentC -> true
228169
| OptionC c -> c |> converterIsIdentity ~config ~toJS
229170
| PromiseC c -> c |> converterIsIdentity ~config ~toJS
@@ -240,106 +181,6 @@ let rec apply ~(config : Config.t) ~converter ~indent ~nameGen ~toJS
240181
~comment:
241182
("WARNING: circular type " ^ s ^ ". Only shallow converter applied.")
242183
|> apply ~config ~converter:c ~indent ~nameGen ~toJS ~variantTables
243-
| FunctionC
244-
{
245-
funArgConverters;
246-
componentName;
247-
isHook;
248-
retConverter;
249-
typeVars;
250-
uncurried;
251-
} ->
252-
let resultName = EmitText.resultName ~nameGen in
253-
let indent1 = indent |> Indent.more in
254-
let indent2 = indent1 |> Indent.more in
255-
let mkReturn x =
256-
"const " ^ resultName ^ " = " ^ x ^ ";"
257-
^ Indent.break ~indent:indent1
258-
^ "return "
259-
^ (resultName
260-
|> apply ~config ~converter:retConverter ~indent:indent2 ~nameGen ~toJS
261-
~variantTables)
262-
in
263-
let convertArg i groupedArgConverter =
264-
match groupedArgConverter with
265-
| ArgConverter argConverter ->
266-
let varName = i + 1 |> EmitText.argi ~nameGen in
267-
let notToJS = not toJS in
268-
( [varName],
269-
[
270-
varName
271-
|> apply ~config ~converter:argConverter ~indent:indent2 ~nameGen
272-
~toJS:notToJS ~variantTables;
273-
] )
274-
| GroupConverter groupConverters ->
275-
let notToJS = not toJS in
276-
if toJS then
277-
let varName = i + 1 |> EmitText.argi ~nameGen in
278-
( [varName],
279-
groupConverters
280-
|> List.map (fun (label, optional, argConverter) ->
281-
varName
282-
|> EmitText.fieldAccess ~label
283-
|> apply ~config
284-
~converter:
285-
(match
286-
optional = Optional
287-
&& not
288-
(argConverter
289-
|> converterIsIdentity ~config ~toJS:notToJS)
290-
with
291-
| true -> OptionC argConverter
292-
| false -> argConverter)
293-
~indent:indent2 ~nameGen ~toJS:notToJS ~variantTables)
294-
)
295-
else
296-
let varNames =
297-
groupConverters
298-
|> List.map (fun (s, _optional, _argConverter) ->
299-
s |> EmitText.arg ~nameGen)
300-
in
301-
let varNamesArr = varNames |> Array.of_list in
302-
let fieldValues =
303-
groupConverters
304-
|> List.mapi (fun i (s, _optional, argConverter) ->
305-
s ^ ":"
306-
^ ((varNamesArr.(i) [@doesNotRaise])
307-
|> apply ~config ~converter:argConverter ~indent:indent2
308-
~nameGen ~toJS:notToJS ~variantTables))
309-
|> String.concat ", "
310-
in
311-
(varNames, ["{" ^ fieldValues ^ "}"])
312-
in
313-
let mkBody bodyArgs =
314-
let useCurry = (not uncurried) && toJS && List.length bodyArgs > 1 in
315-
config.emitImportCurry <- config.emitImportCurry || useCurry;
316-
let functionName =
317-
match isHook with
318-
| true -> "React.createElement"
319-
| false -> value
320-
in
321-
if isHook then config.emitImportReact <- true;
322-
let declareProps, args =
323-
match bodyArgs with
324-
| [props] when isHook ->
325-
let propsName = "$props" |> EmitText.name ~nameGen in
326-
( Indent.break ~indent:indent1
327-
^ "const " ^ propsName ^ " = " ^ props ^ ";",
328-
[value; propsName] )
329-
| _ -> ("", bodyArgs)
330-
in
331-
declareProps
332-
^ Indent.break ~indent:indent1
333-
^ (functionName |> EmitText.funCall ~args ~useCurry |> mkReturn)
334-
in
335-
let convertedArgs = funArgConverters |> List.mapi convertArg in
336-
let args = convertedArgs |> List.map fst |> List.concat in
337-
let funParams =
338-
args |> List.map (fun v -> v |> EmitType.ofTypeAny ~config)
339-
in
340-
let bodyArgs = convertedArgs |> List.map snd |> List.concat in
341-
EmitText.funDef ~bodyArgs ~functionName:componentName ~funParams ~indent
342-
~mkBody ~typeVars
343184
| IdentC -> value
344185
| OptionC c ->
345186
EmitText.parens

jscomp/gentype/EmitText.ml

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,18 @@ let name ~nameGen s =
1010
s
1111

1212
let parens xs = "(" ^ (xs |> String.concat ", ") ^ ")"
13-
let arg ~nameGen x = "Arg" ^ x |> name ~nameGen
14-
let argi ~nameGen i = "Arg" ^ (i |> string_of_int) |> name ~nameGen
15-
let array xs = "[" ^ (xs |> String.concat ", ") ^ "]"
1613
let comment x = "/* " ^ x ^ " */"
1714

18-
let curry ~args ~numArgs name =
19-
match numArgs with
20-
| 0 | 1 -> name ^ parens args
21-
| (2 | 3 | 4 | 5 | 6 | 7 | 8) as n ->
22-
"Curry._" ^ (n |> string_of_int) ^ parens ([name] @ args)
23-
| _ -> "Curry.app" ^ parens [name; args |> array]
24-
25-
let funCall ~args ~useCurry name =
26-
match useCurry with
27-
| true -> name |> curry ~args ~numArgs:(args |> List.length)
28-
| false -> name ^ parens args
2915

3016
let genericsString ~typeVars =
3117
match typeVars == [] with
3218
| true -> ""
3319
| false -> "<" ^ String.concat "," typeVars ^ ">"
3420

35-
let funDef ~bodyArgs ~functionName ~funParams ~indent ~mkBody ~typeVars =
36-
"function "
37-
^ (match functionName with
38-
| None -> ""
39-
| Some name -> name)
40-
^ genericsString ~typeVars ^ (funParams |> parens) ^ " {"
41-
^ (bodyArgs |> mkBody) ^ Indent.break ~indent ^ "}"
4221

4322
let newNameGen () = Hashtbl.create 1
4423
let quotes x = "\"" ^ x ^ "\""
4524

46-
let resultName ~nameGen = "result" |> name ~nameGen
47-
4825
let addComment ~comment x = "\n/* " ^ comment ^ " */\n " ^ x
4926
let arrayAccess ~index value = value ^ "[" ^ string_of_int index ^ "]"
5027
let fieldAccess ~label value = value ^ "." ^ label

jscomp/gentype/GenTypeCommon.ml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -97,22 +97,6 @@ and variant = {
9797

9898
and payload = {case: case; inlineRecord: bool; numArgs: int; t: type_}
9999

100-
let typeIsObject type_ =
101-
match type_ with
102-
| Array _ -> true
103-
| Dict _ -> true
104-
| Function _ -> false
105-
| GroupOfLabeledArgs _ -> false
106-
| Ident _ -> false
107-
| Null _ -> false
108-
| Nullable _ -> false
109-
| Object _ -> true
110-
| Option _ -> false
111-
| Promise _ -> true
112-
| Tuple _ -> true
113-
| TypeVar _ -> false
114-
| Variant _ -> false
115-
116100
type label = Nolabel | Label of string | OptLabel of string
117101

118102
type dep =

jscomp/gentype_tests/typescript-react-example/src/Docstrings.gen.tsx

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
/* eslint-disable import/first */
33

44

5-
// @ts-ignore: Implicit any on import
6-
import * as Curry__Es6Import from 'rescript/lib/es6/curry.js';
7-
const Curry: any = Curry__Es6Import;
8-
95
// @ts-ignore: Implicit any on import
106
import * as DocstringsBS__Es6Import from './Docstrings.bs';
117
const DocstringsBS: any = DocstringsBS__Es6Import;
@@ -21,15 +17,9 @@ export const signMessage: (message:string, key:number) => string = DocstringsBS.
2117

2218
export const one: (a:number) => number = DocstringsBS.one;
2319

24-
export const two: (a:number, b:number) => number = function (Arg1: any, Arg2: any) {
25-
const result = Curry._2(DocstringsBS.two, Arg1, Arg2);
26-
return result
27-
};
20+
export const two: (a:number, b:number) => number = DocstringsBS.two;
2821

29-
export const tree: (a:number, b:number, c:number) => number = function (Arg1: any, Arg2: any, Arg3: any) {
30-
const result = Curry._3(DocstringsBS.tree, Arg1, Arg2, Arg3);
31-
return result
32-
};
22+
export const tree: (a:number, b:number, c:number) => number = DocstringsBS.tree;
3323

3424
export const oneU: (a:number) => number = DocstringsBS.oneU;
3525

@@ -45,17 +35,11 @@ export const unnamed1: (param:number) => number = DocstringsBS.unnamed1;
4535

4636
export const unnamed1U: (param:number) => number = DocstringsBS.unnamed1U;
4737

48-
export const unnamed2: (param_0:number, param_1:number) => number = function (Arg1: any, Arg2: any) {
49-
const result = Curry._2(DocstringsBS.unnamed2, Arg1, Arg2);
50-
return result
51-
};
38+
export const unnamed2: (param_0:number, param_1:number) => number = DocstringsBS.unnamed2;
5239

5340
export const unnamed2U: (param_0:number, param_1:number) => number = DocstringsBS.unnamed2U;
5441

55-
export const grouped: (_1:{ readonly x: number; readonly y: number }, a:number, b:number, c:number, _5:{ readonly z: number }) => number = function (Arg1: any, Arg2: any, Arg3: any, Arg4: any, Arg5: any) {
56-
const result = Curry._6(DocstringsBS.grouped, Arg1.x, Arg1.y, Arg2, Arg3, Arg4, Arg5.z);
57-
return result
58-
};
42+
export const grouped: (_1:{ readonly x: number; readonly y: number }, a:number, b:number, c:number, _5:{ readonly z: number }) => number = DocstringsBS.grouped;
5943

6044
export const unitArgWithoutConversion: () => string = DocstringsBS.unitArgWithoutConversion;
6145

jscomp/gentype_tests/typescript-react-example/src/Hooks.gen.tsx

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
import * as React from 'react';
66

7-
// @ts-ignore: Implicit any on import
8-
import * as Curry__Es6Import from 'rescript/lib/es6/curry.js';
9-
const Curry: any = Curry__Es6Import;
10-
117
// @ts-ignore: Implicit any on import
128
import * as HooksBS__Es6Import from './Hooks.bs';
139
const HooksBS: any = HooksBS__Es6Import;
@@ -79,13 +75,7 @@ export const functionWithRenamedArgs: (_1:{
7975
readonly to: vehicle;
8076
readonly Type: vehicle;
8177
readonly cb: cb
82-
}) => string = function (Arg1: any) {
83-
const result = Curry._3(HooksBS.functionWithRenamedArgs, Arg1.to, Arg1.Type, function (Argto: any) {
84-
const result1 = Arg1.cb({to:Argto});
85-
return result1
86-
});
87-
return result
88-
};
78+
}) => string = HooksBS.functionWithRenamedArgs;
8979

9080
// tslint:disable-next-line:interface-over-type-literal
9181
export type WithRename_componentWithRenamedArgs_Props = {
@@ -100,10 +90,7 @@ export const WithRename_componentWithRenamedArgs: React.ComponentType<{
10090
readonly cb: cb
10191
}> = HooksBS.WithRename.componentWithRenamedArgs;
10292

103-
export const WithRef_makeWithRef: (_1:{ readonly vehicle: vehicle }, _2:(null | undefined | any)) => JSX.Element = function (Arg1: any, Arg2: any) {
104-
const result = Curry._2(HooksBS.WithRef.makeWithRef, Arg1, Arg2);
105-
return result
106-
};
93+
export const WithRef_makeWithRef: (_1:{ readonly vehicle: vehicle }, _2:(null | undefined | any)) => JSX.Element = HooksBS.WithRef.makeWithRef;
10794

10895
// tslint:disable-next-line:interface-over-type-literal
10996
export type testForwardRef_Props = { readonly vehicle: vehicle };
@@ -175,6 +162,8 @@ export const ForwardRef: { input: React.ComponentType<{ readonly r: r }> } = Hoo
175162

176163
export const Fun: { functionReturningReactElement: React.ComponentType<{ readonly name: string }> } = HooksBS.Fun
177164

165+
export const WithRef: { makeWithRef: (_1:{ readonly vehicle: vehicle }, _2:(null | undefined | any)) => JSX.Element } = HooksBS.WithRef
166+
178167
export const WithChildren: { aComponentWithChildren: React.ComponentType<{ readonly children: React.ReactNode; readonly vehicle: vehicle }> } = HooksBS.WithChildren
179168

180169
export const DD: { make: React.ComponentType<{ readonly array: Js_TypedArray2_Uint8Array_t; readonly name: string }> } = HooksBS.DD

jscomp/gentype_tests/typescript-react-example/src/ImportHooks.gen.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@ export const makeRenamed: unknown = makeRenamedTypeChecked as React.ComponentTyp
2626
export const fooTypeChecked: (_1:{ readonly person: person }) => string = fooNotChecked;
2727

2828
// Export 'foo' early to allow circular import from the '.bs.js' file.
29-
export const foo: unknown = function (Argperson: any) {
30-
const result = fooTypeChecked({person:Argperson});
31-
return result
32-
} as (_1:{ readonly person: person }) => string;
29+
export const foo: unknown = fooTypeChecked as (_1:{ readonly person: person }) => string;
3330

3431
// tslint:disable-next-line:interface-over-type-literal
3532
export type person = { readonly name: string; readonly age: number };

0 commit comments

Comments
 (0)