Skip to content

Commit 89be95f

Browse files
committed
deprecate %exteranl in favor of %global and %define
1 parent 1d46abe commit 89be95f

14 files changed

+112
-80
lines changed

Diff for: jscomp/frontend/ast_exp_extension.ml

+11-6
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,19 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
6161
Exp.constraint_ ~loc
6262
(Ast_exp_handle_external.handle_raw ~kind:Raw_re loc payload)
6363
(Ast_comb.to_js_re_type loc)
64-
| "external" -> (
64+
| "global" | "external" -> (
65+
if txt = "external" then
66+
Location.deprecated loc
67+
"%external is deprecated, use %global or %define instead.";
68+
match Ast_payload.as_ident payload with
69+
| Some {txt = Lident x} -> Ast_exp_handle_external.handle_global loc x
70+
| None | Some _ ->
71+
Location.raise_errorf ~loc "external expects a single identifier")
72+
| "define" -> (
6573
match Ast_payload.as_ident payload with
6674
| Some {txt = Lident x} ->
67-
Ast_exp_handle_external.handle_external loc x
68-
(* do we need support [%external gg.xx ]
69-
70-
{[ Js.Undefined.to_opt (if Js.typeof x == "undefined" then x else Js.Undefined.empty ) ]}
71-
*)
75+
Ast_exp_handle_external.handle_define loc x
76+
(* TODO: we need to support %define(gg.xx) *)
7277
| None | Some _ ->
7378
Location.raise_errorf ~loc "external expects a single identifier")
7479
| "time" -> (

Diff for: jscomp/frontend/ast_exp_handle_external.ml

+20-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,25 @@
2424

2525
open Ast_helper
2626

27+
(**
28+
{[ Js.undefinedToOption (Js.globalThis["x"]) ]}
29+
*)
30+
let handle_global loc (x : string) : Parsetree.expression =
31+
let global_reference : Parsetree.expression =
32+
{
33+
pexp_loc = loc;
34+
pexp_attributes = [];
35+
pexp_desc =
36+
Ast_util.js_property loc
37+
(Exp.ident {loc; txt = Ldot (Lident "Js", "globalThis")})
38+
x;
39+
}
40+
in
41+
let undefined_typeof =
42+
Exp.ident {loc; txt = Ldot (Lident "Js", "undefinedToOption")}
43+
in
44+
Ast_compatible.app1 ~loc undefined_typeof global_reference
45+
2746
(*
2847
{[
2948
Js.undefinedToOption
@@ -32,7 +51,7 @@ open Ast_helper
3251
3352
]}
3453
*)
35-
let handle_external loc (x : string) : Parsetree.expression =
54+
let handle_define loc (x : string) : Parsetree.expression =
3655
let raw_exp : Ast_exp.t =
3756
let str_exp =
3857
Ast_compatible.const_exp_string ~loc x ~delimiter:Ext_string.empty

Diff for: jscomp/frontend/ast_exp_handle_external.mli

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
val handle_external : Location.t -> string -> Parsetree.expression
25+
val handle_global : Location.t -> string -> Parsetree.expression
26+
27+
val handle_define : Location.t -> string -> Parsetree.expression
2628

2729
val handle_debugger : Location.t -> Ast_payload.t -> Parsetree.expression_desc
2830

Diff for: jscomp/others/js.res

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ Prefer `Js.Array2` over `Js.Array`, `Js.String2` over `Js.String`, etc. The latt
7777
/** JS object type */
7878
type t<'a> = {..} as 'a
7979

80+
/** JS global object reference */
81+
@val external globalThis: t<'a> = "globalThis"
82+
8083
module Internal = {
8184
external opaqueFullApply: 'a => 'a = "%uncurried_apply"
8285

Diff for: jscomp/runtime/js.res

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ Prefer `Js.Array2` over `Js.Array`, `Js.String2` over `Js.String`, etc. The latt
7777
/** JS object type */
7878
type t<'a> = {..} as 'a
7979

80+
/** JS global object reference */
81+
@val external globalThis: t<'a> = "globalThis"
82+
8083
module Internal = {
8184
external opaqueFullApply: 'a => 'a = "%uncurried_apply"
8285

Diff for: jscomp/test/reasonReactRouter.js

+9-9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: jscomp/test/reasonReactRouter.res

+7-7
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ let arrayToList = a => {
6060
/* actually you know what, not gonna provide search for now. It's a mess.
6161
We'll let users roll their own solution/data structure for now */
6262
let path = () =>
63-
switch %external(window) {
63+
switch %global(window) {
6464
| None => list{}
6565
| Some(window: Dom.window) =>
6666
switch window |> location |> pathname {
@@ -78,7 +78,7 @@ let path = () =>
7878
}
7979
}
8080
let hash = () =>
81-
switch %external(window) {
81+
switch %global(window) {
8282
| None => ""
8383
| Some(window: Dom.window) =>
8484
switch window |> location |> hash {
@@ -91,7 +91,7 @@ let hash = () =>
9191
}
9292
}
9393
let search = () =>
94-
switch %external(window) {
94+
switch %global(window) {
9595
| None => ""
9696
| Some(window: Dom.window) =>
9797
switch window |> location |> search {
@@ -103,15 +103,15 @@ let search = () =>
103103
}
104104
}
105105
let push = path =>
106-
switch (%external(history), %external(window)) {
106+
switch (%global(history), %global(window)) {
107107
| (None, _)
108108
| (_, None) => ()
109109
| (Some(history: Dom.history), Some(window: Dom.window)) =>
110110
pushState(history, ~href=path)
111111
dispatchEvent(window, safeMakeEvent("popstate"))
112112
}
113113
let replace = path =>
114-
switch (%external(history), %external(window)) {
114+
switch (%global(history), %global(window)) {
115115
| (None, _)
116116
| (_, None) => ()
117117
| (Some(history: Dom.history), Some(window: Dom.window)) =>
@@ -143,15 +143,15 @@ let url = () => {path: path(), hash: hash(), search: search()}
143143
/* alias exposed publicly */
144144
let dangerouslyGetInitialUrl = url
145145
let watchUrl = callback =>
146-
switch %external(window) {
146+
switch %global(window) {
147147
| None => () => ()
148148
| Some(window: Dom.window) =>
149149
let watcherID = () => callback(url())
150150
addEventListener(window, "popstate", watcherID)
151151
watcherID
152152
}
153153
let unwatchUrl = watcherID =>
154-
switch %external(window) {
154+
switch %global(window) {
155155
| None => ()
156156
| Some(window: Dom.window) => removeEventListener(window, "popstate", watcherID)
157157
}

Diff for: jscomp/test/undef_regression2_test.res

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ let ok = (loc, x) => {
1212
list{(loc ++ (" id " ++ string_of_int(test_id.contents)), _ => Mt.Ok(x)), ...suites.contents}
1313
}
1414

15-
let a = switch %external(___undefined_value) {
15+
let a = switch %define(___undefined_value) {
1616
| None => 1
1717
| Some(_) => 2
1818
}
1919

2020
let test = () =>
21-
switch %external(__DEV__) {
21+
switch %define(__DEV__) {
2222
| Some(_) => Js.log("dev mode")
2323
| None => Js.log("producton mode")
2424
}
2525

2626
let test2 = () =>
27-
switch %external(__filename) {
27+
switch %define(__filename) {
2828
| Some(f) => Js.log(f)
2929
| None => Js.log("non node environment")
3030
}
3131

3232
let test3 = () =>
33-
if %external(__DEV__) == None {
33+
if %define(__DEV__) == None {
3434
Js.log("production mode")
3535
}
3636

Diff for: lib/es6/belt_internalBuckets.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@
33
import * as Belt_Array from "./belt_Array.js";
44
import * as Caml_option from "./caml_option.js";
55

6-
function copyBucket(c) {
7-
if (c === undefined) {
8-
return c;
9-
}
10-
let head = {
11-
key: c.key,
12-
value: c.value,
13-
next: undefined
14-
};
15-
copyAuxCont(c.next, head);
16-
return head;
17-
}
18-
196
function copyAuxCont(_c, _prec) {
207
while(true) {
218
let prec = _prec;
@@ -35,6 +22,19 @@ function copyAuxCont(_c, _prec) {
3522
};
3623
}
3724

25+
function copyBucket(c) {
26+
if (c === undefined) {
27+
return c;
28+
}
29+
let head = {
30+
key: c.key,
31+
value: c.value,
32+
next: undefined
33+
};
34+
copyAuxCont(c.next, head);
35+
return head;
36+
}
37+
3838
function copyBuckets(buckets) {
3939
let len = buckets.length;
4040
let newBuckets = new Array(len);

Diff for: lib/es6/belt_internalSetBuckets.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33
import * as Belt_Array from "./belt_Array.js";
44

5+
function copyBucket(c) {
6+
if (c === undefined) {
7+
return c;
8+
}
9+
let head = {
10+
key: c.key,
11+
next: undefined
12+
};
13+
copyAuxCont(c.next, head);
14+
return head;
15+
}
16+
517
function copyAuxCont(_c, _prec) {
618
while(true) {
719
let prec = _prec;
@@ -20,18 +32,6 @@ function copyAuxCont(_c, _prec) {
2032
};
2133
}
2234

23-
function copyBucket(c) {
24-
if (c === undefined) {
25-
return c;
26-
}
27-
let head = {
28-
key: c.key,
29-
next: undefined
30-
};
31-
copyAuxCont(c.next, head);
32-
return head;
33-
}
34-
3535
function copyBuckets(buckets) {
3636
let len = buckets.length;
3737
let newBuckets = new Array(len);

Diff for: lib/es6/sys.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ let unix = Caml_sys.os_type() === "Unix";
1919
let win32 = Caml_sys.os_type() === "Win32";
2020

2121
function getenv_opt(s) {
22-
let x = typeof process === "undefined" ? undefined : process;
22+
let x = globalThis.process;
2323
if (x !== undefined) {
2424
return x.env[s];
2525
}

Diff for: lib/js/belt_internalBuckets.js

+13-13
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@
33
let Belt_Array = require("./belt_Array.js");
44
let Caml_option = require("./caml_option.js");
55

6-
function copyBucket(c) {
7-
if (c === undefined) {
8-
return c;
9-
}
10-
let head = {
11-
key: c.key,
12-
value: c.value,
13-
next: undefined
14-
};
15-
copyAuxCont(c.next, head);
16-
return head;
17-
}
18-
196
function copyAuxCont(_c, _prec) {
207
while(true) {
218
let prec = _prec;
@@ -35,6 +22,19 @@ function copyAuxCont(_c, _prec) {
3522
};
3623
}
3724

25+
function copyBucket(c) {
26+
if (c === undefined) {
27+
return c;
28+
}
29+
let head = {
30+
key: c.key,
31+
value: c.value,
32+
next: undefined
33+
};
34+
copyAuxCont(c.next, head);
35+
return head;
36+
}
37+
3838
function copyBuckets(buckets) {
3939
let len = buckets.length;
4040
let newBuckets = new Array(len);

0 commit comments

Comments
 (0)