Skip to content

Commit cef82cf

Browse files
committed
refactor tests etc a bit
1 parent 2b84d15 commit cef82cf

12 files changed

+232
-88
lines changed

compiler/gentype/EmitJs.ml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,6 @@ let emit_translation_as_string ~config ~file_name
635635
|> List.map (fun (type_declaration : CodeItem.type_declaration) ->
636636
type_declaration.export_from_type_declaration)
637637
in
638-
let emitters = Emitters.initial in
639638
let type_name_is_interface ~env =
640639
type_name_is_interface ~export_type_map
641640
~export_type_map_from_other_files:env.export_type_map_from_other_files
@@ -644,7 +643,7 @@ let emit_translation_as_string ~config ~file_name
644643
try export_type_map |> StringMap.find s
645644
with Not_found -> env.export_type_map_from_other_files |> StringMap.find s
646645
in
647-
let emitters = emitters
646+
let emitters = Emitters.initial
648647
and module_items_emitter = ExportModule.create_module_items_emitter ()
649648
and env = initial_env in
650649
let env, emitters =

compiler/gentype/EmitType.ml

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,18 +118,40 @@ let rec render_type ~(config : Config.t) ?(indent = None)
118118
~type_name_is_interface
119119
| Ident {builtin; name; type_args} ->
120120
let name = name |> sanitize_type_name in
121-
(match
122-
(not builtin) && config.export_interfaces
123-
&& name |> type_name_is_interface
124-
with
125-
| true -> name |> interface_name ~config
126-
| false -> name)
127-
^ EmitText.generics_string
128-
~type_vars:
129-
(type_args
130-
|> List.map
131-
(render_type ~config ~indent ~type_name_is_interface ~in_fun_type)
132-
)
121+
let rendered_name =
122+
match
123+
(not builtin) && config.export_interfaces
124+
&& name |> type_name_is_interface
125+
with
126+
| true -> name |> interface_name ~config
127+
| false -> name
128+
in
129+
if name = "$RescriptTypeSatisfiesTypeScriptType" then
130+
match type_args with
131+
| [t1; t2] ->
132+
let render_arg t =
133+
" "
134+
^ (t
135+
|> render_type ~config ~indent:(Some " ") ~type_name_is_interface
136+
~in_fun_type)
137+
in
138+
rendered_name ^ "<\n" ^ render_arg t1 ^ ",\n" ^ render_arg t2 ^ "\n>"
139+
| _ ->
140+
rendered_name
141+
^ EmitText.generics_string
142+
~type_vars:
143+
(type_args
144+
|> List.map
145+
(render_type ~config ~indent ~type_name_is_interface
146+
~in_fun_type))
147+
else
148+
rendered_name
149+
^ EmitText.generics_string
150+
~type_vars:
151+
(type_args
152+
|> List.map
153+
(render_type ~config ~indent ~type_name_is_interface
154+
~in_fun_type))
133155
| Null type_ ->
134156
"(null | "
135157
^ (type_ |> render_type ~config ~indent ~type_name_is_interface ~in_fun_type)
@@ -426,8 +448,10 @@ let emit_import_react ~emitters =
426448

427449
let emit_satisfies_helper ~emitters =
428450
let alias =
429-
"export type $RescriptTypeSatisfiesTypeScriptType<RescriptType, \
430-
TypeScriptType extends RescriptType> = TypeScriptType;"
451+
"export type $RescriptTypeSatisfiesTypeScriptType<\n\
452+
RescriptType,\n\
453+
TypeScriptType extends RescriptType\n\
454+
> = TypeScriptType;"
431455
in
432456
Emitters.export_early ~emitters alias
433457

compiler/gentype/TranslateTypeDeclarations.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
101101
type_name
102102
|> create_export_type_from_type_declaration
103103
~annotation:annotation_for_export ~loc ~name_as ~opaque
104-
~type_:(apply_satisfies_wrapper translation.type_)
105-
~type_env ~doc_string ~type_vars
104+
~type_:translation.type_ ~type_env ~doc_string ~type_vars
106105
in
107106
let import_types =
108107
translation.dependencies
@@ -212,7 +211,8 @@ let traslate_declaration_kind ~config ~loc ~output_file_relative ~resolver
212211
|> TranslateTypeExprFromTypes.translate_type_expr_from_types ~config
213212
~type_env
214213
in
215-
translation |> handle_general_declaration |> return_type_declaration
214+
{translation with type_ = apply_satisfies_wrapper translation.type_}
215+
|> handle_general_declaration |> return_type_declaration
216216
| GeneralDeclaration (Some core_type), None ->
217217
let translation =
218218
core_type |> TranslateCoreType.translate_core_type ~config ~type_env

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)}

tests/gentype_tests/genimport-single/Makefile

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,11 @@
11
SHELL = /bin/bash
22

3-
test:
3+
build:
44
yarn workspaces focus @tests/gentype-genimport-single
55
yarn build
6-
# Expect direct type aliasing to imported TS type
7-
grep -Fq 'export type numberT = Type$$TypeScript;' src/GenTypeImportExpectedErrors.gen.tsx
8-
grep -Fq 'export type tupleT = Type$$TypeScript;' src/GenTypeImportExpectedErrors.gen.tsx
9-
grep -Fq 'export type arrayT = Type$$TypeScript;' src/GenTypeImportExpectedErrors.gen.tsx
10-
grep -Fq 'export type promiseT = Type$$TypeScript;' src/GenTypeImportExpectedErrors.gen.tsx
11-
grep -Fq 'export type nestedArrayT = Type$$TypeScript;' src/GenTypeImportExpectedErrors.gen.tsx
12-
grep -Fq 'export type stringT = Type$$TypeScript;' src/GenTypeImportExpectedErrors.gen.tsx
13-
# Now TypeScript typecheck and ensure no type errors are emitted
14-
yarn typecheck > ts-errors.txt 2>&1 || true
15-
if [ -s ts-errors.txt ]; then \
16-
echo 'Unexpected TypeScript errors:' ; \
17-
cat ts-errors.txt ; \
18-
exit 1 ; \
19-
else \
20-
echo 'TypeScript typecheck clean' ; \
21-
fi
6+
7+
test: build
8+
./test.sh
229

2310
clean:
2411
yarn workspaces focus @tests/gentype-genimport-single

tests/gentype_tests/genimport-single/src/AriaComponents.gen.tsx

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,18 @@
33
/* eslint-disable */
44
/* tslint:disable */
55

6-
export type $RescriptTypeSatisfiesTypeScriptType<RescriptType, TypeScriptType extends RescriptType> = TypeScriptType;
6+
export type $RescriptTypeSatisfiesTypeScriptType<
7+
RescriptType,
8+
TypeScriptType extends RescriptType
9+
> = TypeScriptType;
710

8-
export type groupRenderProps = $RescriptTypeSatisfiesTypeScriptType<{
9-
readonly isHovered: boolean;
10-
readonly isFocusWithin: boolean;
11-
readonly isFocusVisible: boolean;
12-
readonly isDisabled: boolean;
13-
readonly isInvalid: boolean
14-
},import("react-aria-components").GroupRenderProps>;
11+
export type groupRenderProps = $RescriptTypeSatisfiesTypeScriptType<
12+
{
13+
readonly isHovered: boolean;
14+
readonly isFocusWithin: boolean;
15+
readonly isFocusVisible: boolean;
16+
readonly isDisabled: boolean;
17+
readonly isInvalid: boolean
18+
},
19+
import("react-aria-components").GroupRenderProps
20+
>;

tests/gentype_tests/genimport-single/src/GenTypeImportExpectedErrors.gen.tsx

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* TypeScript file generated from GenTypeSatisfiesExpectedErrors.res by genType. */
2+
3+
/* eslint-disable */
4+
/* tslint:disable */
5+
6+
export type $RescriptTypeSatisfiesTypeScriptType<
7+
RescriptType,
8+
TypeScriptType extends RescriptType
9+
> = TypeScriptType;
10+
11+
import * as GenTypeSatisfiesExpectedErrorsJS from './GenTypeSatisfiesExpectedErrors.res.js';
12+
13+
export type numberT = $RescriptTypeSatisfiesTypeScriptType<
14+
number,
15+
import("external-module").Type
16+
>;
17+
18+
export type tupleT = $RescriptTypeSatisfiesTypeScriptType<
19+
[number, string],
20+
import("external-module").Type
21+
>;
22+
23+
export type arrayT = $RescriptTypeSatisfiesTypeScriptType<
24+
number[],
25+
import("external-module").Type
26+
>;
27+
28+
export type promiseT = $RescriptTypeSatisfiesTypeScriptType<
29+
Promise<number>,
30+
import("external-module").Type
31+
>;
32+
33+
export type nestedArrayT = $RescriptTypeSatisfiesTypeScriptType<
34+
Array<number[]>,
35+
import("external-module").Type
36+
>;
37+
38+
export type stringT = $RescriptTypeSatisfiesTypeScriptType<
39+
string,
40+
import("external-module").Type
41+
>;
42+
43+
export const useNumber: (x:numberT) => numberT = GenTypeSatisfiesExpectedErrorsJS.useNumber as any;
44+
45+
export const useTuple: (x:tupleT) => tupleT = GenTypeSatisfiesExpectedErrorsJS.useTuple as any;
46+
47+
export const useArray: (x:arrayT) => arrayT = GenTypeSatisfiesExpectedErrorsJS.useArray as any;
48+
49+
export const usePromise: (x:promiseT) => promiseT = GenTypeSatisfiesExpectedErrorsJS.usePromise as any;
50+
51+
export const useNestedArray: (x:nestedArrayT) => nestedArrayT = GenTypeSatisfiesExpectedErrorsJS.useNestedArray as any;
52+
53+
export const useString: (x:stringT) => stringT = GenTypeSatisfiesExpectedErrorsJS.useString as any;
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
/* This module intentionally contains cases that should cause TypeScript errors
22
when the external imported Type mismatches the ReScript manifest type. */
33

4-
@genType.import(("external-module", "Type"))
4+
@gentype.satisfies(("external-module", "Type"))
55
type numberT = int
66

7-
@genType
7+
@gentype
88
let useNumber = (x: numberT) => x
99

10-
@genType.import(("external-module", "Type"))
10+
@gentype.satisfies(("external-module", "Type"))
1111
type tupleT = (int, string)
1212

13-
@genType
13+
@gentype
1414
let useTuple = (x: tupleT) => x
1515

16-
@genType.import(("external-module", "Type"))
16+
@gentype.satisfies(("external-module", "Type"))
1717
type arrayT = array<int>
1818

19-
@genType
19+
@gentype
2020
let useArray = (x: arrayT) => x
2121

22-
@genType.import(("external-module", "Type"))
22+
@gentype.satisfies(("external-module", "Type"))
2323
type promiseT = Js.Promise.t<int>
2424

25-
@genType
25+
@gentype
2626
let usePromise = (x: promiseT) => x
2727

28-
@genType.import(("external-module", "Type"))
28+
@gentype.satisfies(("external-module", "Type"))
2929
type nestedArrayT = array<array<int>>
3030

31-
@genType
31+
@gentype
3232
let useNestedArray = (x: nestedArrayT) => x
3333

3434
/* Positive case: string matches external Type=string */
35-
@genType.import(("external-module", "Type"))
35+
@gentype.satisfies(("external-module", "Type"))
3636
type stringT = string
3737

38-
@genType
38+
@gentype
3939
let useString = (x: stringT) => x

tests/gentype_tests/genimport-single/src/GenTypeSatisfiesExpectedErrors.res.js

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)