Skip to content
This repository was archived by the owner on Nov 13, 2023. It is now read-only.

Commit cf3519e

Browse files
committed
Add support for exporting uncurried function types.
Fixes #103.
1 parent ce5b5eb commit cf3519e

File tree

8 files changed

+165
-1
lines changed

8 files changed

+165
-1
lines changed

Changes.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# master
22
- [Fix issue with access to the .bs.js file when exporting nested component](https://github.com/cristianoc/genType/issues/104).
33
- Fix: [Emit enum conversion tables early, to avoid the case where they’re used before being defined](https://github.com/cristianoc/genType/issues/102).
4+
- Add support for exporting uncurried function types.
45

56
# 1.5.0
67
- Hygiene: avoid variable capture for generated variable names (function arguments, return value, array items).

examples/flow-react-example/src/Uncurried.bs.js

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @flow strict
3+
* @generated
4+
* @nolint
5+
*/
6+
7+
// $FlowExpectedError: Reason checked type sufficiently
8+
import * as UncurriedBS from './Uncurried.bs';
9+
10+
export const uncurried0: () => string = UncurriedBS.uncurried0;
11+
12+
export const uncurried1: (number) => string = UncurriedBS.uncurried1;
13+
14+
export const uncurried2: (number, string) => string = UncurriedBS.uncurried2;
15+
16+
export const uncurried3: (number, string, number) => string = UncurriedBS.uncurried3;
17+
18+
export const curried3: (number, string, number) => string = UncurriedBS.curried3;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[@genType]
2+
let uncurried0 = (.) => "";
3+
4+
[@genType]
5+
let uncurried1 = (. x) => x |> string_of_int;
6+
7+
[@genType]
8+
let uncurried2 = (. x, y) => (x |> string_of_int) ++ y;
9+
10+
[@genType]
11+
let uncurried3 =
12+
(. x, y, z) => (x |> string_of_int) ++ y ++ (z |> string_of_int);
13+
14+
[@genType]
15+
let curried3 = (x, y, z) => (x |> string_of_int) ++ y ++ (z |> string_of_int);

examples/typescript-react-example/src/Uncurried.bs.js

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* TypeScript file generated by genType. */
2+
3+
// tslint:disable-next-line:no-var-requires
4+
const UncurriedBS = require('./Uncurried.bs');
5+
6+
export const uncurried0: () => string = UncurriedBS.uncurried0;
7+
8+
export const uncurried1: (_1:number) => string = UncurriedBS.uncurried1;
9+
10+
export const uncurried2: (_1:number, _2:string) => string = UncurriedBS.uncurried2;
11+
12+
export const uncurried3: (_1:number, _2:string, _3:number) => string = UncurriedBS.uncurried3;
13+
14+
export const curried3: (_1:number, _2:string, _3:number) => string = UncurriedBS.curried3;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[@genType]
2+
let uncurried0 = (.) => "";
3+
4+
[@genType]
5+
let uncurried1 = (. x) => x |> string_of_int;
6+
7+
[@genType]
8+
let uncurried2 = (. x, y) => (x |> string_of_int) ++ y;
9+
10+
[@genType]
11+
let uncurried3 =
12+
(. x, y, z) => (x |> string_of_int) ++ y ++ (z |> string_of_int);
13+
14+
[@genType]
15+
let curried3 = (x, y, z) => (x |> string_of_int) ++ y ++ (z |> string_of_int);

src/TranslateTypeExprFromTypes.re

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,33 @@ let translateConstr =
197197
...paramTranslation,
198198
typ: Nullable(paramTranslation.typ),
199199
}
200-
200+
| (
201+
Pdot(Pdot(Pident({name: "Js", _}), "Internal", _), "fn", _),
202+
[{dependencies: argsDependencies, typ: Tuple(ts)}, ret],
203+
) => {
204+
dependencies: argsDependencies @ ret.dependencies,
205+
typ: Function({typeVars: [], argTypes: ts, retType: ret.typ}),
206+
}
207+
| (
208+
Pdot(Pdot(Pident({name: "Js", _}), "Internal", _), "fn", _),
209+
[
210+
{
211+
dependencies: argsDependencies,
212+
typ: Enum({cases: [{label: "Arity_0", _}]}),
213+
},
214+
ret,
215+
],
216+
) => {
217+
dependencies: argsDependencies @ ret.dependencies,
218+
typ: Function({typeVars: [], argTypes: [], retType: ret.typ}),
219+
}
220+
| (
221+
Pdot(Pdot(Pident({name: "Js", _}), "Internal", _), "fn", _),
222+
[{dependencies: argsDependencies, typ: singleT}, ret],
223+
) => {
224+
dependencies: argsDependencies @ ret.dependencies,
225+
typ: Function({typeVars: [], argTypes: [singleT], retType: ret.typ}),
226+
}
201227
| (Pdot(Pident({name: "Js", _}), "t", _), _) =>
202228
let dependencies =
203229
fieldsTranslations
@@ -493,6 +519,17 @@ and translateTypeExprFromTypes_ =
493519
| None => {dependencies: [], typ: mixedOrUnknown(~config)}
494520
};
495521

522+
/* Handle bucklescript's "Arity_" encoding in first argument of Js.Internal.fn(_,_) for uncurried functions.
523+
Return the argument tuple. */
524+
| Tvariant({row_fields: [(_, Rpresent(Some(t)))], _}) =>
525+
t
526+
|> translateTypeExprFromTypes_(
527+
~config,
528+
~typeVarsGen,
529+
~noFunctionReturnDependencies,
530+
~typeEnv,
531+
)
532+
496533
| Tfield(_)
497534
| Tnil
498535
| Tobject(_)

0 commit comments

Comments
 (0)