Skip to content

Commit 3b47ef9

Browse files
authored
Deprecate %external extension (#6906)
* deprecate `%external` in favor of `%global` and `%define` * just deprecate `%external`
1 parent 44c0c27 commit 3b47ef9

8 files changed

+47
-182
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@
8787
- Improve unused attribute warning message. https://github.com/rescript-lang/rescript-compiler/pull/6787
8888
- Remove internal option `use-stdlib` from build schema. https://github.com/rescript-lang/rescript-compiler/pull/6778
8989
- Fix `Js.Types.JSBigInt` payload to use native `bigint` type. https://github.com/rescript-lang/rescript-compiler/pull/6911
90+
- Deprecate `%external` extension, which has never been officially introduced. https://github.com/rescript-lang/rescript-compiler/pull/6906
9091

9192
# 11.1.2
9293

jscomp/frontend/ast_exp_extension.ml

+3-6
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,10 @@ let handle_extension e (self : Bs_ast_mapper.mapper)
6262
(Ast_exp_handle_external.handle_raw ~kind:Raw_re loc payload)
6363
(Ast_comb.to_js_re_type loc)
6464
| "external" -> (
65+
Location.deprecated loc
66+
"%external is deprecated, use %raw or regular FFI syntax instead.";
6567
match Ast_payload.as_ident payload with
66-
| 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-
*)
68+
| Some {txt = Lident x} -> Ast_exp_handle_external.handle_external loc x
7269
| None | Some _ ->
7370
Location.raise_errorf ~loc "external expects a single identifier")
7471
| "time" -> (

jscomp/frontend/ast_exp_handle_external.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@
2424

2525
open Ast_helper
2626

27-
(*
27+
(**
2828
{[
2929
Js.undefinedToOption
3030
(if Js.typeof x = "undefined" then undefined
3131
else x )
3232
3333
]}
34+
35+
@deprecated
3436
*)
3537
let handle_external loc (x : string) : Parsetree.expression =
3638
let raw_exp : Ast_exp.t =

jscomp/test/build.ninja

+1-2
Large diffs are not rendered by default.

jscomp/test/reasonReactRouter.js

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

jscomp/test/reasonReactRouter.res

+20-14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ external createEventNonIEBrowsers: string => Dom.event = "createEvent"
3333
@send
3434
external initEventNonIEBrowsers: (Dom.event, string, bool, bool) => unit = "initEvent"
3535

36+
@val @scope("globalThis")
37+
external window: option<Dom.window> = "window"
38+
39+
@val @scope("globalThis")
40+
external history: option<Dom.history> = "history"
41+
3642
let safeMakeEvent = eventName =>
3743
if Js.typeof(event) == "function" {
3844
makeEventIE11Compatible(eventName)
@@ -60,9 +66,9 @@ let arrayToList = a => {
6066
/* actually you know what, not gonna provide search for now. It's a mess.
6167
We'll let users roll their own solution/data structure for now */
6268
let path = () =>
63-
switch %external(window) {
69+
switch window {
6470
| None => list{}
65-
| Some(window: Dom.window) =>
71+
| Some(window) =>
6672
switch window |> location |> pathname {
6773
| ""
6874
| "/" => list{}
@@ -78,9 +84,9 @@ let path = () =>
7884
}
7985
}
8086
let hash = () =>
81-
switch %external(window) {
87+
switch window {
8288
| None => ""
83-
| Some(window: Dom.window) =>
89+
| Some(window) =>
8490
switch window |> location |> hash {
8591
| ""
8692
| "#" => ""
@@ -91,9 +97,9 @@ let hash = () =>
9197
}
9298
}
9399
let search = () =>
94-
switch %external(window) {
100+
switch window {
95101
| None => ""
96-
| Some(window: Dom.window) =>
102+
| Some(window) =>
97103
switch window |> location |> search {
98104
| ""
99105
| "?" => ""
@@ -103,18 +109,18 @@ let search = () =>
103109
}
104110
}
105111
let push = path =>
106-
switch (%external(history), %external(window)) {
112+
switch (history, window) {
107113
| (None, _)
108114
| (_, None) => ()
109-
| (Some(history: Dom.history), Some(window: Dom.window)) =>
115+
| (Some(history), Some(window)) =>
110116
pushState(history, ~href=path)
111117
dispatchEvent(window, safeMakeEvent("popstate"))
112118
}
113119
let replace = path =>
114-
switch (%external(history), %external(window)) {
120+
switch (history, window) {
115121
| (None, _)
116122
| (_, None) => ()
117-
| (Some(history: Dom.history), Some(window: Dom.window)) =>
123+
| (Some(history), Some(window)) =>
118124
replaceState(history, ~href=path)
119125
dispatchEvent(window, safeMakeEvent("popstate"))
120126
}
@@ -143,17 +149,17 @@ let url = () => {path: path(), hash: hash(), search: search()}
143149
/* alias exposed publicly */
144150
let dangerouslyGetInitialUrl = url
145151
let watchUrl = callback =>
146-
switch %external(window) {
152+
switch window {
147153
| None => () => ()
148-
| Some(window: Dom.window) =>
154+
| Some(window) =>
149155
let watcherID = () => callback(url())
150156
addEventListener(window, "popstate", watcherID)
151157
watcherID
152158
}
153159
let unwatchUrl = watcherID =>
154-
switch %external(window) {
160+
switch window {
155161
| None => ()
156-
| Some(window: Dom.window) => removeEventListener(window, "popstate", watcherID)
162+
| Some(window) => removeEventListener(window, "popstate", watcherID)
157163
}
158164

159165
let useUrl = (~serverUrl=?, ()) => {

jscomp/test/undef_regression2_test.js

-97
This file was deleted.

jscomp/test/undef_regression2_test.res

-44
This file was deleted.

0 commit comments

Comments
 (0)