Skip to content

Commit d1ec709

Browse files
committed
await for dynamic import without async context
1 parent 1f9c581 commit d1ec709

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

jscomp/frontend/bs_builtin_ppx.ml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,15 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
227227
match Ast_attributes.has_await_payload e.pexp_attributes with
228228
| None -> result
229229
| Some _ ->
230-
(* if !async_context = false then
230+
(if !async_context = false then
231+
let isJsImport (e : Parsetree.expression) =
232+
match e with
233+
| { pexp_desc = Pexp_apply ({ pexp_desc = Pexp_ident { txt = Ldot ( Lident "Js", "import") } }, _) } -> true
234+
| _ -> false
235+
in
236+
if not (isJsImport e) then
231237
Location.raise_errorf ~loc:e.pexp_loc
232-
"Await on expression not in an async context"; *)
238+
"Await on expression not in an async context");
233239
Ast_await.create_await_expression result
234240

235241
let typ_mapper (self : mapper) (typ : Parsetree.core_type) =

jscomp/test/Import.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ eachIntAsync({
4444
console.log("async", n);
4545
}));
4646

47-
var beltAsModule = import("../../lib/js/belt_List.js");
47+
var beltAsModule = await import("../../lib/js/belt_List.js");
4848

4949
var M = await import("../../lib/js/belt_List.js");
5050

jscomp/test/Import.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let _ = list{1, 2, 3}->eachIntLazy(n => Js.log2("lazy", n))
99
let _ = list{1, 2, 3}->eachIntAsync(n => Js.log2("async", n))
1010

1111
module type BeltList = module type of Belt.List
12-
let beltAsModule = Js.import(module(Belt.List: BeltList))
12+
let beltAsModule = await Js.import(module(Belt.List: BeltList))
1313

1414
// module type BeltList0 = module type of Belt.List
1515
// module M = unpack(@res.await Js.import(module(Belt.List: BeltList0)))

0 commit comments

Comments
 (0)