Skip to content

Commit

Permalink
await for dynamic import without async context
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Dec 20, 2022
1 parent 1f9c581 commit d1ec709
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
10 changes: 8 additions & 2 deletions jscomp/frontend/bs_builtin_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,15 @@ let expr_mapper ~async_context ~in_function_def (self : mapper)
match Ast_attributes.has_await_payload e.pexp_attributes with
| None -> result
| Some _ ->
(* if !async_context = false then
(if !async_context = false then
let isJsImport (e : Parsetree.expression) =
match e with
| { pexp_desc = Pexp_apply ({ pexp_desc = Pexp_ident { txt = Ldot ( Lident "Js", "import") } }, _) } -> true
| _ -> false
in
if not (isJsImport e) then
Location.raise_errorf ~loc:e.pexp_loc
"Await on expression not in an async context"; *)
"Await on expression not in an async context");
Ast_await.create_await_expression result

let typ_mapper (self : mapper) (typ : Parsetree.core_type) =
Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/Import.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ eachIntAsync({
console.log("async", n);
}));

var beltAsModule = import("../../lib/js/belt_List.js");
var beltAsModule = await import("../../lib/js/belt_List.js");

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

Expand Down
2 changes: 1 addition & 1 deletion jscomp/test/Import.res
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let _ = list{1, 2, 3}->eachIntLazy(n => Js.log2("lazy", n))
let _ = list{1, 2, 3}->eachIntAsync(n => Js.log2("async", n))

module type BeltList = module type of Belt.List
let beltAsModule = Js.import(module(Belt.List: BeltList))
let beltAsModule = await Js.import(module(Belt.List: BeltList))

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

0 comments on commit d1ec709

Please sign in to comment.