Skip to content

Commit f00443c

Browse files
committedSep 9, 2022
Mak promise a built-in type.
Cleaner on hover and writing promise types explicitly.
1 parent 4d75a5b commit f00443c

11 files changed

+109
-45
lines changed
 

‎jscomp/gentype/TranslateTypeExprFromTypes.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
203203
| [ "Js"; "null_undefined" ] ),
204204
[ paramTranslation ] ) ->
205205
{ paramTranslation with type_ = Nullable paramTranslation.type_ }
206-
| [ "Js"; "Promise"; "t" ], [ paramTranslation ] ->
206+
| ([ "Js"; "Promise"; "t" ] | ["promise"]), [ paramTranslation ] ->
207207
{ paramTranslation with type_ = Promise paramTranslation.type_ }
208208
| ( [ "Js"; "Internal"; "fn" ],
209209
[ { dependencies = argsDependencies; type_ = Tuple ts }; ret ] ) ->

‎jscomp/main/builtin_cmi_datasets.ml

+3-3
Large diffs are not rendered by default.

‎jscomp/main/builtin_cmj_datasets.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(* 36afc4d4582ed9cc168039f5d3f28592 *)
1+
(* a32a8631aa0bfcd37a4d742e9ad8aada *)
22
let module_names : string array = Obj.magic (
33
"Js" (* 23 *),
44
"Arg" (* 217 *),
@@ -209,7 +209,7 @@ let module_data : string array = Obj.magic (
209209
(* Belt_Float *)"\132\149\166\190\000\000\000\022\000\000\000\007\000\000\000\021\000\000\000\020\160\144\176*fromString\144\160\160A@@@A",
210210
(* Belt_Range *)"\132\149\166\190\000\000\000\160\000\000\0004\000\000\000\166\000\000\000\156\160\b\000\000(\000\176$some\144\160\160C@@@\176%every\144\160\160C@@@\176%someU\144\160\160C@@@\176&everyU\144\160\160C@@@\176&someBy\144\160\160D@@@\176'everyBy\144\160\160D@@@\176'forEach\144\160\160C@@@\176'someByU\144\160\160D@@@\176(everyByU\144\160\160D@@@\176(forEachU\144\160\160C@@@A",
211211
(* Js_console *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
212-
(* Js_promise *)"\132\149\166\190\000\000\000\252\000\000\000J\000\000\000\243\000\000\000\232\160\160\176%catch\144\160\160B@@\144\148\192B\160\176\001\003\253$arg1@\160\176\001\003\254#obj@@\151\176\180%catch\160\160AA\160\160AA@\181%catch@@\160\144\004\014\160\151\176\b\000\000\004\016A\160\144\004\023@\176\1924others/js_promise.ml\000Q\001\011\018\001\011\018\192\004\002\000R\001\011T\001\011l@@\004\004\208B@@@@\176%then_\144\160\160B@@\144\148\192B\160\176\001\003\248$arg1@\160\176\001\003\249\004%@@\151\176\180$then\160\160AA\160\160AA@\181$then@@\160\144\004\r\160\151\176\b\000\000\004\016A\160\144\004\022@\176\192\004$\000N\001\n\186\001\n\186\192\004%\000O\001\n\248\001\011\016@@\004\003\208B@@@@A",
212+
(* Js_promise *)"\132\149\166\190\000\000\000\252\000\000\000J\000\000\000\243\000\000\000\232\160\160\176%catch\144\160\160B@@\144\148\192B\160\176\001\003\253$arg1@\160\176\001\003\254#obj@@\151\176\180%catch\160\160AA\160\160AA@\181%catch@@\160\144\004\014\160\151\176\b\000\000\004\016A\160\144\004\023@\176\1924others/js_promise.ml\000Q\001\011\241\001\011\241\192\004\002\000R\001\012?\001\012]@@\004\004\208B@@@@\176%then_\144\160\160B@@\144\148\192B\160\176\001\003\248$arg1@\160\176\001\003\249\004%@@\151\176\180$then\160\160AA\160\160AA@\181$then@@\160\144\004\r\160\151\176\b\000\000\004\016A\160\144\004\022@\176\192\004$\000N\001\011\135\001\011\135\192\004%\000O\001\011\209\001\011\239@@\004\003\208B@@@@A",
213213
(* Js_string2 *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
214214
(* Js_weakmap *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
215215
(* Js_weakset *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",

‎jscomp/ml/predef.ml

+12-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ and ident_floatarray = ident_create "floatarray"
4848

4949
and ident_unknown = ident_create "unknown"
5050

51+
and ident_promise = ident_create "promise"
52+
5153
type test =
5254
| For_sure_yes
5355
| For_sure_no
@@ -87,6 +89,8 @@ and path_unkonwn = Pident ident_unknown
8789
and path_extension_constructor = Pident ident_extension_constructor
8890
and path_floatarray = Pident ident_floatarray
8991

92+
and path_promise = Pident ident_promise
93+
9094
let type_int = newgenty (Tconstr(path_int, [], ref Mnil))
9195
and type_char = newgenty (Tconstr(path_char, [], ref Mnil))
9296
and type_bytes = newgenty (Tconstr(path_bytes, [], ref Mnil))
@@ -227,6 +231,12 @@ let common_initial_env add_type add_extension empty_env =
227231
type_params = [tvar];
228232
type_arity = 1;
229233
type_variance = [Variance.covariant]}
234+
and decl_promise =
235+
let tvar = newgenvar() in
236+
{decl_abstr with
237+
type_params = [tvar];
238+
type_arity = 1;
239+
type_variance = [Variance.covariant]}
230240
in
231241

232242
let add_extension id l =
@@ -268,7 +278,8 @@ let common_initial_env add_type add_extension empty_env =
268278
add_type ident_int decl_abstr_imm (
269279
add_type ident_extension_constructor decl_abstr (
270280
add_type ident_floatarray decl_abstr (
271-
empty_env)))))))))))))))))))))))
281+
add_type ident_promise decl_promise (
282+
empty_env))))))))))))))))))))))))
272283

273284
let build_initial_env add_type add_exception empty_env =
274285
let common = common_initial_env add_type add_exception empty_env in

‎jscomp/ml/predef.mli

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ val path_int64: Path.t
5252
val path_lazy_t: Path.t
5353
val path_extension_constructor: Path.t
5454
val path_floatarray: Path.t
55+
val path_promise: Path.t
5556

5657
val path_match_failure: Path.t
5758
val path_assert_failure : Path.t

‎jscomp/ml/typecore.ml

+1
Original file line numberDiff line numberDiff line change
@@ -1868,6 +1868,7 @@ and type_expect ?in_function ?recarg env sexp ty_expected =
18681868
let rec extractPromise t =
18691869
match t.desc with
18701870
| Tconstr (Pdot (Pdot (Pident {name = "Js"}, "Promise", _), "t", _), [t1], _)
1871+
| Tconstr (Pident {name = "promise"}, [t1], _)
18711872
->
18721873
Some t1
18731874
| Tlink t1 | Tsubst t1 | Tpoly (t1, []) -> extractPromise t1

‎jscomp/others/js_promise.ml

+18-18
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
[@@@warning "-103"]
3333

34-
type +'a t
34+
type +'a t = 'a promise
3535
type error
3636

3737
(*
@@ -43,52 +43,52 @@ type error
4343
external make :
4444
((resolve:(('a -> unit)[@bs]) -> reject:((exn -> unit)[@bs]) -> unit)
4545
[@bs.uncurry]) ->
46-
'a t = "Promise"
46+
'a promise = "Promise"
4747
[@@bs.new]
4848

4949
(* `make (fun resolve reject -> .. )` *)
50-
external resolve : 'a -> 'a t = "resolve" [@@bs.val] [@@bs.scope "Promise"]
51-
external reject : exn -> 'a t = "reject" [@@bs.val] [@@bs.scope "Promise"]
50+
external resolve : 'a -> 'a promise = "resolve" [@@bs.val] [@@bs.scope "Promise"]
51+
external reject : exn -> 'a promise = "reject" [@@bs.val] [@@bs.scope "Promise"]
5252

53-
external all : 'a t array -> 'a array t = "all"
53+
external all : 'a promise array -> 'a array promise = "all"
5454
[@@bs.val] [@@bs.scope "Promise"]
5555

56-
external all2 : 'a0 t * 'a1 t -> ('a0 * 'a1) t = "all"
56+
external all2 : 'a0 promise * 'a1 promise -> ('a0 * 'a1) promise = "all"
5757
[@@bs.val] [@@bs.scope "Promise"]
5858

59-
external all3 : 'a0 t * 'a1 t * 'a2 t -> ('a0 * 'a1 * 'a2) t = "all"
59+
external all3 : 'a0 promise * 'a1 promise * 'a2 promise -> ('a0 * 'a1 * 'a2) promise = "all"
6060
[@@bs.val] [@@bs.scope "Promise"]
6161

62-
external all4 : 'a0 t * 'a1 t * 'a2 t * 'a3 t -> ('a0 * 'a1 * 'a2 * 'a3) t
62+
external all4 : 'a0 promise * 'a1 promise * 'a2 promise * 'a3 promise -> ('a0 * 'a1 * 'a2 * 'a3) promise
6363
= "all"
6464
[@@bs.val] [@@bs.scope "Promise"]
6565

6666
external all5 :
67-
'a0 t * 'a1 t * 'a2 t * 'a3 t * 'a4 t -> ('a0 * 'a1 * 'a2 * 'a3 * 'a4) t
67+
'a0 promise * 'a1 promise * 'a2 promise * 'a3 promise * 'a4 promise -> ('a0 * 'a1 * 'a2 * 'a3 * 'a4) promise
6868
= "all"
6969
[@@bs.val] [@@bs.scope "Promise"]
7070

7171
external all6 :
72-
'a0 t * 'a1 t * 'a2 t * 'a3 t * 'a4 t * 'a5 t ->
73-
('a0 * 'a1 * 'a2 * 'a3 * 'a4 * 'a5) t = "all"
72+
'a0 promise * 'a1 promise * 'a2 promise * 'a3 promise * 'a4 promise * 'a5 promise ->
73+
('a0 * 'a1 * 'a2 * 'a3 * 'a4 * 'a5) promise = "all"
7474
[@@bs.val] [@@bs.scope "Promise"]
7575

76-
external race : 'a t array -> 'a t = "race" [@@bs.val] [@@bs.scope "Promise"]
76+
external race : 'a promise array -> 'a promise = "race" [@@bs.val] [@@bs.scope "Promise"]
7777

78-
external then_ : (('a -> 'b t)[@bs.uncurry]) -> 'b t = "then"
79-
[@@bs.send.pipe: 'a t]
78+
external then_ : (('a -> 'b promise)[@bs.uncurry]) -> 'b promise = "then"
79+
[@@bs.send.pipe: 'a promise]
8080

81-
external catch : ((error -> 'a t)[@bs.uncurry]) -> 'a t = "catch"
82-
[@@bs.send.pipe: 'a t]
81+
external catch : ((error -> 'a promise)[@bs.uncurry]) -> 'a promise = "catch"
82+
[@@bs.send.pipe: 'a promise]
8383
(* ` p|> catch handler`
8484
Note in JS the returned promise type is actually runtime dependent,
8585
if promise is rejected, it will pick the `handler` otherwise the original promise,
8686
to make it strict we enforce reject handler
8787
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch
8888
*)
8989

90-
external unsafe_async : 'a -> 'a t = "%identity"
91-
external unsafe_await : 'a t -> 'a = "?await"
90+
external unsafe_async : 'a -> 'a promise = "%identity"
91+
external unsafe_await : 'a promise -> 'a = "?await"
9292

9393

9494
(*

‎lib/4.06.1/unstable/all_ounit_tests.ml

+13-1
Original file line numberDiff line numberDiff line change
@@ -43611,6 +43611,7 @@ val path_int64: Path.t
4361143611
val path_lazy_t: Path.t
4361243612
val path_extension_constructor: Path.t
4361343613
val path_floatarray: Path.t
43614+
val path_promise: Path.t
4361443615

4361543616
val path_match_failure: Path.t
4361643617
val path_assert_failure : Path.t
@@ -43697,6 +43698,8 @@ and ident_floatarray = ident_create "floatarray"
4369743698

4369843699
and ident_unknown = ident_create "unknown"
4369943700

43701+
and ident_promise = ident_create "promise"
43702+
4370043703
type test =
4370143704
| For_sure_yes
4370243705
| For_sure_no
@@ -43736,6 +43739,8 @@ and path_unkonwn = Pident ident_unknown
4373643739
and path_extension_constructor = Pident ident_extension_constructor
4373743740
and path_floatarray = Pident ident_floatarray
4373843741

43742+
and path_promise = Pident ident_promise
43743+
4373943744
let type_int = newgenty (Tconstr(path_int, [], ref Mnil))
4374043745
and type_char = newgenty (Tconstr(path_char, [], ref Mnil))
4374143746
and type_bytes = newgenty (Tconstr(path_bytes, [], ref Mnil))
@@ -43876,6 +43881,12 @@ let common_initial_env add_type add_extension empty_env =
4387643881
type_params = [tvar];
4387743882
type_arity = 1;
4387843883
type_variance = [Variance.covariant]}
43884+
and decl_promise =
43885+
let tvar = newgenvar() in
43886+
{decl_abstr with
43887+
type_params = [tvar];
43888+
type_arity = 1;
43889+
type_variance = [Variance.covariant]}
4387943890
in
4388043891

4388143892
let add_extension id l =
@@ -43917,7 +43928,8 @@ let common_initial_env add_type add_extension empty_env =
4391743928
add_type ident_int decl_abstr_imm (
4391843929
add_type ident_extension_constructor decl_abstr (
4391943930
add_type ident_floatarray decl_abstr (
43920-
empty_env)))))))))))))))))))))))
43931+
add_type ident_promise decl_promise (
43932+
empty_env))))))))))))))))))))))))
4392143933

4392243934
let build_initial_env add_type add_exception empty_env =
4392343935
let common = common_initial_env add_type add_exception empty_env in

‎lib/4.06.1/unstable/js_compiler.ml

+19-6
Large diffs are not rendered by default.

‎lib/4.06.1/unstable/js_playground_compiler.ml

+19-6
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)