Skip to content

Commit e3256ba

Browse files
authored
Extend Gentype shims (#7842)
* extend gentype shims * changelog * format * remove a few unecessary * proper async iterator without any as well
1 parent edf2724 commit e3256ba

File tree

12 files changed

+736
-0
lines changed

12 files changed

+736
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
#### :rocket: New Feature
2020

21+
- Support mapping more standard library types automatically to TS types via `gentype`, without requiring shims. https://github.com/rescript-lang/rescript/pull/7842
22+
2123
#### :bug: Bug fix
2224

2325
- Show `Stdlib.TypedArray` completions for typed arrays. https://github.com/rescript-lang/rescript/pull/7827

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ test-syntax-roundtrip:
4242

4343
test-gentype:
4444
make -C tests/gentype_tests/typescript-react-example clean test
45+
make -C tests/gentype_tests/stdlib-no-shims clean test
4546

4647
test-rewatch:
4748
./rewatch/tests/suite-ci.sh
@@ -80,6 +81,7 @@ checkformat:
8081

8182
clean-gentype:
8283
make -C tests/gentype_tests/typescript-react-example clean
84+
make -C tests/gentype_tests/stdlib-no-shims clean
8385

8486
clean-rewatch:
8587
cargo clean --manifest-path rewatch/Cargo.toml && rm -f rewatch/rewatch

compiler/gentype/TranslateTypeExprFromTypes.ml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,48 @@ let translate_constr ~config ~params_translation ~(path : Path.t) ~type_env =
118118
}
119119
| (["Js"; "Re"; "t"] | ["RegExp"; "t"] | ["Stdlib"; "RegExp"; "t"]), [] ->
120120
{dependencies = []; type_ = regexp_t}
121+
| ["Stdlib"; "ArrayBuffer"; "t"], [] ->
122+
{dependencies = []; type_ = ident "ArrayBuffer"}
123+
| ["Stdlib"; "DataView"; "t"], [] ->
124+
{dependencies = []; type_ = ident "DataView"}
125+
| ["Stdlib"; "Int8Array"; "t"], [] ->
126+
{dependencies = []; type_ = ident "Int8Array"}
127+
| ["Stdlib"; "Uint8Array"; "t"], [] ->
128+
{dependencies = []; type_ = ident "Uint8Array"}
129+
| ["Stdlib"; "Uint8ClampedArray"; "t"], [] ->
130+
{dependencies = []; type_ = ident "Uint8ClampedArray"}
131+
| ["Stdlib"; "Int16Array"; "t"], [] ->
132+
{dependencies = []; type_ = ident "Int16Array"}
133+
| ["Stdlib"; "Uint16Array"; "t"], [] ->
134+
{dependencies = []; type_ = ident "Uint16Array"}
135+
| ["Stdlib"; "Int32Array"; "t"], [] ->
136+
{dependencies = []; type_ = ident "Int32Array"}
137+
| ["Stdlib"; "Uint32Array"; "t"], [] ->
138+
{dependencies = []; type_ = ident "Uint32Array"}
139+
| ["Stdlib"; "Float32Array"; "t"], [] ->
140+
{dependencies = []; type_ = ident "Float32Array"}
141+
| ["Stdlib"; "Float64Array"; "t"], [] ->
142+
{dependencies = []; type_ = ident "Float64Array"}
143+
| ["Stdlib"; "BigInt64Array"; "t"], [] ->
144+
{dependencies = []; type_ = ident "BigInt64Array"}
145+
| ["Stdlib"; "BigUint64Array"; "t"], [] ->
146+
{dependencies = []; type_ = ident "BigUint64Array"}
147+
| ["Stdlib"; "Symbol"; "t"], [] -> {dependencies = []; type_ = ident "symbol"}
148+
| ["Stdlib"; "Intl"; intl_module; "t"], [] ->
149+
{dependencies = []; type_ = ident ("Intl." ^ intl_module)}
150+
| (["Stdlib"; "Error"; "t"] | ["Stdlib"; "JsError"; "t"]), [] ->
151+
{dependencies = []; type_ = ident "Error"}
152+
| ["Stdlib"; "Iterator"; "t"], [param_translation] ->
153+
{
154+
dependencies = param_translation.dependencies;
155+
type_ = ident ~type_args:[param_translation.type_] "Iterator";
156+
}
157+
| ["Stdlib"; "AsyncIterator"; "t"], [param_translation] ->
158+
{
159+
dependencies = param_translation.dependencies;
160+
type_ = ident ~type_args:[param_translation.type_] "AsyncIterator";
161+
}
162+
| ["Stdlib"; "Ordering"; "t"], [] -> {dependencies = []; type_ = number_t}
121163
| ["unit"], [] -> {dependencies = []; type_ = unit_t}
122164
| (["array"] | ["Js"; ("Array" | "Array2"); "t"]), [param_translation] ->
123165
{param_translation with type_ = Array (param_translation.type_, Mutable)}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
SHELL = /bin/bash
2+
3+
test:
4+
yarn build
5+
yarn typecheck
6+
7+
clean:
8+
yarn clean
9+
10+
.DEFAULT_GOAL := test
11+
12+
.PHONY: clean test
13+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "@tests/gentype-stdlib-no-shims",
3+
"private": true,
4+
"scripts": {
5+
"build": "rescript legacy build",
6+
"clean": "rescript clean",
7+
"typecheck": "tsc"
8+
},
9+
"dependencies": {
10+
"rescript": "workspace:^"
11+
},
12+
"devDependencies": {
13+
"typescript": "5.8.2"
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"gentypeconfig": {
3+
"language": "typescript",
4+
"module": "esmodule",
5+
"importPath": "relative",
6+
"debug": { "all": false },
7+
"exportInterfaces": false
8+
},
9+
"name": "@tests/gentype-stdlib-no-shims",
10+
"sources": [
11+
{ "dir": "src", "subdirs": true }
12+
],
13+
"package-specs": {
14+
"module": "esmodule",
15+
"in-source": true
16+
},
17+
"suffix": ".res.js"
18+
}
19+
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* TypeScript file generated from StdlibNoShims.res by genType. */
2+
3+
/* eslint-disable */
4+
/* tslint:disable */
5+
6+
import * as StdlibNoShimsJS from './StdlibNoShims.res.js';
7+
8+
export const idInt: (x:number) => number = StdlibNoShimsJS.idInt as any;
9+
10+
export const idFloat: (x:number) => number = StdlibNoShimsJS.idFloat as any;
11+
12+
export const idBool: (x:boolean) => boolean = StdlibNoShimsJS.idBool as any;
13+
14+
export const idString: (x:string) => string = StdlibNoShimsJS.idString as any;
15+
16+
export const idBigInt: (x:bigint) => bigint = StdlibNoShimsJS.idBigInt as any;
17+
18+
export const idDate: (x:Date) => Date = StdlibNoShimsJS.idDate as any;
19+
20+
export const idRegExp: (x:RegExp) => RegExp = StdlibNoShimsJS.idRegExp as any;
21+
22+
export const idPromise: (x:Promise<string>) => Promise<string> = StdlibNoShimsJS.idPromise as any;
23+
24+
export const idDict: (x:{[id: string]: number}) => {[id: string]: number} = StdlibNoShimsJS.idDict as any;
25+
26+
export const idMap: (x:Map<string,number>) => Map<string,number> = StdlibNoShimsJS.idMap as any;
27+
28+
export const idWeakMap: (x:WeakMap<number[],number>) => WeakMap<number[],number> = StdlibNoShimsJS.idWeakMap as any;
29+
30+
export const idSet: (x:Set<string>) => Set<string> = StdlibNoShimsJS.idSet as any;
31+
32+
export const idWeakSet: (x:WeakSet<number[]>) => WeakSet<number[]> = StdlibNoShimsJS.idWeakSet as any;
33+
34+
export const idArray: (x:number[]) => number[] = StdlibNoShimsJS.idArray as any;
35+
36+
export const idUndefined: (x:(undefined | number)) => (undefined | number) = StdlibNoShimsJS.idUndefined as any;
37+
38+
export const idNull: (x:(null | number)) => (null | number) = StdlibNoShimsJS.idNull as any;
39+
40+
export const idNullable: (x:(null | undefined | number)) => (null | undefined | number) = StdlibNoShimsJS.idNullable as any;
41+
42+
export const idOption: (x:(undefined | string)) => (undefined | string) = StdlibNoShimsJS.idOption as any;
43+
44+
export const idJSON: (x:unknown) => unknown = StdlibNoShimsJS.idJSON as any;
45+
46+
export const idResult: (x:
47+
{ TAG: "Ok"; _0: number }
48+
| { TAG: "Error"; _0: string }) =>
49+
{ TAG: "Ok"; _0: number }
50+
| { TAG: "Error"; _0: string } = StdlibNoShimsJS.idResult as any;
51+
52+
export const idResultAlias: (x:
53+
{ TAG: "Ok"; _0: number }
54+
| { TAG: "Error"; _0: string }) =>
55+
{ TAG: "Ok"; _0: number }
56+
| { TAG: "Error"; _0: string } = StdlibNoShimsJS.idResultAlias as any;
57+
58+
export const idRef: (x:{ contents: number }) => { contents: number } = StdlibNoShimsJS.idRef as any;
59+
60+
export const returnsUnit: () => void = StdlibNoShimsJS.returnsUnit as any;
61+
62+
export const idTuple: (x:[number, string, number]) => [number, string, number] = StdlibNoShimsJS.idTuple as any;
63+
64+
export const idArrayBuffer: (x:ArrayBuffer) => ArrayBuffer = StdlibNoShimsJS.idArrayBuffer as any;
65+
66+
export const idDataView: (x:DataView) => DataView = StdlibNoShimsJS.idDataView as any;
67+
68+
export const idInt8Array: (x:Int8Array) => Int8Array = StdlibNoShimsJS.idInt8Array as any;
69+
70+
export const idUint8Array: (x:Uint8Array) => Uint8Array = StdlibNoShimsJS.idUint8Array as any;
71+
72+
export const idUint8ClampedArray: (x:Uint8ClampedArray) => Uint8ClampedArray = StdlibNoShimsJS.idUint8ClampedArray as any;
73+
74+
export const idInt16Array: (x:Int16Array) => Int16Array = StdlibNoShimsJS.idInt16Array as any;
75+
76+
export const idUint16Array: (x:Uint16Array) => Uint16Array = StdlibNoShimsJS.idUint16Array as any;
77+
78+
export const idInt32Array: (x:Int32Array) => Int32Array = StdlibNoShimsJS.idInt32Array as any;
79+
80+
export const idUint32Array: (x:Uint32Array) => Uint32Array = StdlibNoShimsJS.idUint32Array as any;
81+
82+
export const idFloat32Array: (x:Float32Array) => Float32Array = StdlibNoShimsJS.idFloat32Array as any;
83+
84+
export const idFloat64Array: (x:Float64Array) => Float64Array = StdlibNoShimsJS.idFloat64Array as any;
85+
86+
export const idBigInt64Array: (x:BigInt64Array) => BigInt64Array = StdlibNoShimsJS.idBigInt64Array as any;
87+
88+
export const idBigUint64Array: (x:BigUint64Array) => BigUint64Array = StdlibNoShimsJS.idBigUint64Array as any;
89+
90+
export const idSymbol: (x:symbol) => symbol = StdlibNoShimsJS.idSymbol as any;
91+
92+
export const idIterator: (x:Iterator<number>) => Iterator<number> = StdlibNoShimsJS.idIterator as any;
93+
94+
export const idAsyncIterator: (x:AsyncIterator<number>) => AsyncIterator<number> = StdlibNoShimsJS.idAsyncIterator as any;
95+
96+
export const idOrdering: (x:number) => number = StdlibNoShimsJS.idOrdering as any;
97+
98+
export const idIntlCollator: (x:Intl.Collator) => Intl.Collator = StdlibNoShimsJS.idIntlCollator as any;
99+
100+
export const idIntlDateTimeFormat: (x:Intl.DateTimeFormat) => Intl.DateTimeFormat = StdlibNoShimsJS.idIntlDateTimeFormat as any;
101+
102+
export const idIntlListFormat: (x:Intl.ListFormat) => Intl.ListFormat = StdlibNoShimsJS.idIntlListFormat as any;
103+
104+
export const idIntlLocale: (x:Intl.Locale) => Intl.Locale = StdlibNoShimsJS.idIntlLocale as any;
105+
106+
export const idIntlNumberFormat: (x:Intl.NumberFormat) => Intl.NumberFormat = StdlibNoShimsJS.idIntlNumberFormat as any;
107+
108+
export const idIntlPluralRules: (x:Intl.PluralRules) => Intl.PluralRules = StdlibNoShimsJS.idIntlPluralRules as any;
109+
110+
export const idIntlRelativeTimeFormat: (x:Intl.RelativeTimeFormat) => Intl.RelativeTimeFormat = StdlibNoShimsJS.idIntlRelativeTimeFormat as any;
111+
112+
export const idIntlSegmenter: (x:Intl.Segmenter) => Intl.Segmenter = StdlibNoShimsJS.idIntlSegmenter as any;
113+
114+
export const idIntlSegments: (x:Intl.Segments) => Intl.Segments = StdlibNoShimsJS.idIntlSegments as any;
115+
116+
export const idJsError: (x:Error) => Error = StdlibNoShimsJS.idJsError as any;
117+
118+
export const idObj: (x:{}) => {} = StdlibNoShimsJS.idObj as any;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Verify Stdlib.* types map without TS shims
2+
3+
@genType
4+
let idInt = (x: int) => x
5+
6+
@genType
7+
let idFloat = (x: float) => x
8+
9+
@genType
10+
let idBool = (x: bool) => x
11+
12+
@genType
13+
let idString = (x: String.t) => x
14+
15+
@genType
16+
let idBigInt = (x: bigint) => x
17+
18+
@genType
19+
let idDate = (x: Date.t) => x
20+
21+
@genType
22+
let idRegExp = (x: RegExp.t) => x
23+
24+
@genType
25+
let idPromise = (x: Promise.t<string>) => x
26+
27+
@genType
28+
let idDict = (x: Dict.t<int>) => x
29+
30+
@genType
31+
let idMap = (x: Map.t<string, int>) => x
32+
33+
@genType
34+
let idWeakMap = (x: WeakMap.t<array<int>, int>) => x
35+
36+
@genType
37+
let idSet = (x: Set.t<string>) => x
38+
39+
@genType
40+
let idWeakSet = (x: WeakSet.t<array<int>>) => x
41+
42+
@genType
43+
let idArray = (x: array<int>) => x
44+
45+
@genType
46+
let idUndefined = (x: undefined<int>) => x
47+
48+
@genType
49+
let idNull = (x: Null.t<int>) => x
50+
51+
@genType
52+
let idNullable = (x: Nullable.t<int>) => x
53+
54+
@genType
55+
let idOption = (x: option<string>) => x
56+
57+
@genType
58+
let idJSON = (x: JSON.t) => x
59+
60+
@genType
61+
let idResult = (x: Result.t<int, string>) => x
62+
63+
@genType
64+
let idResultAlias = (x: result<int, string>) => x
65+
66+
@genType
67+
let idRef = (x: ref<int>) => x
68+
69+
@genType
70+
let returnsUnit = (): unit => ()
71+
@genType
72+
let idTuple = (x: (int, string, float)) => x
73+
74+
// Typed arrays and related JS interop types
75+
@genType let idArrayBuffer = (x: ArrayBuffer.t) => x
76+
@genType let idDataView = (x: DataView.t) => x
77+
78+
@genType let idInt8Array = (x: Int8Array.t) => x
79+
@genType let idUint8Array = (x: Uint8Array.t) => x
80+
@genType let idUint8ClampedArray = (x: Uint8ClampedArray.t) => x
81+
@genType let idInt16Array = (x: Int16Array.t) => x
82+
@genType let idUint16Array = (x: Uint16Array.t) => x
83+
@genType let idInt32Array = (x: Int32Array.t) => x
84+
@genType let idUint32Array = (x: Uint32Array.t) => x
85+
@genType let idFloat32Array = (x: Float32Array.t) => x
86+
@genType let idFloat64Array = (x: Float64Array.t) => x
87+
@genType let idBigInt64Array = (x: BigInt64Array.t) => x
88+
@genType let idBigUint64Array = (x: BigUint64Array.t) => x
89+
90+
// Additional stdlib types
91+
@genType let idSymbol = (x: Symbol.t) => x
92+
93+
// More Stdlib exposed types (add more as generator support grows)
94+
@genType let idIterator = (x: Iterator.t<int>) => x
95+
@genType let idAsyncIterator = (x: AsyncIterator.t<int>) => x
96+
@genType let idOrdering = (x: Ordering.t) => x
97+
98+
// Intl* types
99+
@genType let idIntlCollator = (x: Intl.Collator.t) => x
100+
@genType let idIntlDateTimeFormat = (x: Intl.DateTimeFormat.t) => x
101+
@genType let idIntlListFormat = (x: Intl.ListFormat.t) => x
102+
@genType let idIntlLocale = (x: Intl.Locale.t) => x
103+
@genType let idIntlNumberFormat = (x: Intl.NumberFormat.t) => x
104+
@genType let idIntlPluralRules = (x: Intl.PluralRules.t) => x
105+
@genType let idIntlRelativeTimeFormat = (x: Intl.RelativeTimeFormat.t) => x
106+
@genType let idIntlSegmenter = (x: Intl.Segmenter.t) => x
107+
@genType let idIntlSegments = (x: Intl.Segments.t) => x
108+
109+
// Errors
110+
@genType let idJsError = (x: JsError.t) => x
111+
112+
// Dynamic object
113+
@genType let idObj = (x: {..}) => x
114+
115+
// dummy change to trigger rebuild

0 commit comments

Comments
 (0)