Skip to content

Commit 98f9a4e

Browse files
committed
add more instanceof backed type
1 parent a505761 commit 98f9a4e

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

jscomp/ml/ast_untagged_variants.ml

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
module Instance = struct
2-
type t = Array | Promise
3-
let to_string = function Array -> "Array" | Promise -> "Promise"
2+
type t = Array | Promise | Date | RegExp
3+
let to_string = function
4+
Array -> "Array"
5+
| Promise -> "Promise"
6+
| Date -> "Date"
7+
| RegExp -> "RegExp"
48
end
59

610
type untaggedError =
@@ -126,9 +130,20 @@ let type_is_builtin_object (t : Types.type_expr) =
126130
match t.desc with
127131
| Tconstr (path, _, _) ->
128132
let name = Path.name path in
129-
name = "Js.Dict.t" || name = "Js_dict.t" || name = "Js.Re.t" || name = "RescriptCore.Re.t"
133+
name = "Js.Dict.t" || name = "Js_dict.t"
130134
| _ -> false
131135

136+
let type_to_instanceof_backed_obj (t : Types.type_expr) =
137+
match t.desc with
138+
| Tconstr (path, _, _) -> (
139+
match Path.name path with
140+
| "Js.Date.t" -> Some(Instance.Date)
141+
| "Js.Re.t" | "RescriptCore.Re.t" ->
142+
(* TODO: Get rid of explicit Core by digging through aliases *)
143+
Some(RegExp)
144+
| _ -> None)
145+
| _ -> None
146+
132147
let get_block_type ~env (cstr : Types.constructor_declaration) :
133148
block_type option =
134149
match (process_untagged cstr.cd_attributes, cstr.cd_args) with
@@ -158,6 +173,11 @@ let get_block_type ~env (cstr : Types.constructor_declaration) :
158173
| true, Cstr_tuple [({desc = Tconstr _} as t)] when type_is_builtin_object t
159174
->
160175
Some ObjectType
176+
| true, Cstr_tuple [({desc = Tconstr _} as t)] when type_to_instanceof_backed_obj t |> Option.is_some
177+
->
178+
(match type_to_instanceof_backed_obj t with
179+
| None -> None
180+
| Some instanceType -> Some (InstanceType instanceType))
161181
| true, Cstr_tuple [ty] -> (
162182
let default = Some UnknownType in
163183
match !extract_concrete_typedecl env ty with

jscomp/test/UntaggedVariants.js

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

jscomp/test/UntaggedVariants.res

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,3 +383,28 @@ module Arr = {
383383
| TestInt => Js.log(12)
384384
}
385385
}
386+
387+
module AllInstanceofTypes = {
388+
type record = {userName: string}
389+
390+
@unboxed
391+
type t =
392+
| String(string)
393+
| Array(array<string>)
394+
| Promise(promise<string>)
395+
| Object(record)
396+
| Date(Js.Date.t)
397+
| RegExp(Js.Re.t)
398+
| BigInt(Js.Bigint.t)
399+
400+
let classifyAll = async (t: t) =>
401+
switch t {
402+
| String(s) => Js.log(s)
403+
| Promise(p) => Js.log(await p)
404+
| Object({userName}) => Js.log(userName)
405+
| Date(date) => Js.log(date->Js.Date.toString)
406+
| RegExp(re) => Js.log(re->Js.Re.test_("test"))
407+
| BigInt(i) => Js.log(Js.String.make(i))
408+
| Array(arr) => Js.log(arr->Belt.Array.joinWith("-"))
409+
}
410+
}

0 commit comments

Comments
 (0)