Skip to content

Commit 996c83c

Browse files
committed
Add Core standard library support for genType
1 parent 095abc8 commit 996c83c

16 files changed

+106
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ subset of the arguments, and return a curried type with the remaining ones https
2626
- Add support for toplevel `await` https://github.com/rescript-lang/rescript-compiler/pull/5940
2727
- Support optional named arguments without a final unit in uncurried functions https://github.com/rescript-lang/rescript-compiler/pull/5907
2828
- Add experimental suppport for directives. An annotation such as `@@directive("use client;")` emits `use client;` verbatim before imports https://github.com/rescript-lang/rescript-compiler/pull/5998
29+
- Add `Core` standard library support for `genType`
2930

3031
#### :boom: Breaking Change
3132

jscomp/gentype/Converter.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ let typeGetConverterNormalized ~config ~inline ~lookupId ~typeNameIsInterface
246246
in
247247
(TupleC innerConversions, Tuple normalizedList)
248248
| TypeVar _ -> (IdentC, normalized_)
249+
| Undefined t ->
250+
let tConverter, tNormalized = t |> visit ~visited in
251+
(OptionC tConverter, Undefined tNormalized)
249252
| Variant variant ->
250253
let allowUnboxed = not variant.polymorphic in
251254
let withPayloads, normalized, unboxed =

jscomp/gentype/EmitJs.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ let propagateAnnotationToSubTypes ~codeItems (typeMap : CodeItem.exportTypeMap)
626626
retType |> visit
627627
| GroupOfLabeledArgs fields | Object (_, fields) | Record fields ->
628628
fields |> List.iter (fun {type_} -> type_ |> visit)
629-
| Option t | Null t | Nullable t | Promise t -> t |> visit
629+
| Option t | Null t | Nullable t | Promise t | Undefined t -> t |> visit
630630
| Tuple innerTypes -> innerTypes |> List.iter visit
631631
| TypeVar _ -> ()
632632
| Variant {inherits; payloads} ->

jscomp/gentype/EmitType.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ let rec renderType ~(config : Config.t) ?(indent = None) ~typeNameIsInterface
157157
|> String.concat ", ")
158158
^ "]"
159159
| TypeVar s -> s
160+
| Undefined type_ ->
161+
"(undefined | "
162+
^ (type_ |> renderType ~config ~indent ~typeNameIsInterface ~inFunType)
163+
^ ")"
160164
| Variant {inherits; noPayloads; payloads; polymorphic; unboxed} ->
161165
let inheritsRendered =
162166
inherits

jscomp/gentype/GenTypeCommon.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type type_ =
7070
| Record of fields
7171
| Tuple of type_ list
7272
| TypeVar of string
73+
| Undefined of type_
7374
| Variant of variant
7475

7576
and fields = field list
@@ -119,6 +120,7 @@ let typeIsObject type_ =
119120
| Record _ -> true
120121
| Tuple _ -> true
121122
| TypeVar _ -> false
123+
| Undefined _ -> false
122124
| Variant _ -> false
123125

124126
type label = Nolabel | Label of string | OptLabel of string

jscomp/gentype/TranslateTypeExprFromTypes.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,14 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
201201
{dependencies = []; type_ = EmitType.typeReactElement}
202202
| (["FB"; "option"] | ["option"]), [paramTranslation] ->
203203
{paramTranslation with type_ = Option paramTranslation.type_}
204-
| (["Js"; "Null"; "t"] | ["Js"; "null"]), [paramTranslation] ->
204+
| ( (["Js"; "Undefined"; "t"] | ["Undefined"; "t"] | ["Js"; "undefined"]),
205+
[paramTranslation] ) ->
206+
{paramTranslation with type_ = Undefined paramTranslation.type_}
207+
| (["Js"; "Null"; "t"] | ["Null"; "t"] | ["Js"; "null"]), [paramTranslation]
208+
->
205209
{paramTranslation with type_ = Null paramTranslation.type_}
206210
| ( ( ["Js"; "Nullable"; "t"]
211+
| ["Nullable"; "t"]
207212
| ["Js"; "nullable"]
208213
| ["Js"; "Null_undefined"; "t"]
209214
| ["Js"; "null_undefined"] ),

jscomp/gentype/TypeVars.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ let rec substitute ~f type0 =
6767
match f s with
6868
| None -> type0
6969
| Some type1 -> type1)
70+
| Undefined type_ -> Undefined (type_ |> substitute ~f)
7071
| Variant variant ->
7172
Variant
7273
{
@@ -94,7 +95,7 @@ let rec free_ type0 : StringSet.t =
9495
|> List.fold_left
9596
(fun s typeArg -> StringSet.union s (typeArg |> free_))
9697
StringSet.empty
97-
| Null type_ | Nullable type_ -> type_ |> free_
98+
| Null type_ | Nullable type_ | Undefined type_ -> type_ |> free_
9899
| Option type_ | Promise type_ -> type_ |> free_
99100
| Tuple innerTypes ->
100101
innerTypes

jscomp/gentype_tests/typescript-react-example/src/Core.bs.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.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* TypeScript file generated from Core.res by genType. */
2+
/* eslint-disable import/first */
3+
4+
5+
// @ts-ignore: Implicit any on import
6+
import * as CoreBS__Es6Import from './Core.bs';
7+
const CoreBS: any = CoreBS__Es6Import;
8+
9+
export const null0: (x:(null | number)) => (null | number) = CoreBS.null0;
10+
11+
export const null1: (x:(null | number)) => (null | number) = CoreBS.null1;
12+
13+
export const nullable0: (x:(null | undefined | number)) => (null | undefined | number) = CoreBS.nullable0;
14+
15+
export const nullable1: (x:(null | undefined | number)) => (null | undefined | number) = CoreBS.nullable1;
16+
17+
export const undefined0: (x:(undefined | number)) => (undefined | number) = function (Arg1: any) {
18+
const result = CoreBS.undefined0((Arg1 == null ? undefined : Arg1));
19+
return result
20+
};
21+
22+
export const undefined1: (x:(undefined | number)) => (undefined | number) = function (Arg1: any) {
23+
const result = CoreBS.undefined1((Arg1 == null ? undefined : Arg1));
24+
return result
25+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@genType
2+
let null0 = (x: Js.null<int>) => x
3+
4+
@genType
5+
let null1 = (x: Null.t<int>) => x
6+
7+
@genType
8+
let nullable0 = (x: Js.nullable<int>) => x
9+
10+
@genType
11+
let nullable1 = (x: Nullable.t<int>) => x
12+
13+
@genType
14+
let undefined0 = (x: Js.undefined<int>) => x
15+
16+
@genType
17+
let undefined1 = (x: Undefined.t<int>) => x

jscomp/gentype_tests/typescript-react-example/src/Null.bs.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type t<'a> = Js.null<'a>

jscomp/gentype_tests/typescript-react-example/src/Nullable.bs.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type t<'a> = Js.nullable<'a>

jscomp/gentype_tests/typescript-react-example/src/Undefined.bs.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type t<'a> = Js.undefined<'a>

0 commit comments

Comments
 (0)