Skip to content

Commit

Permalink
extract module id from J.expression
Browse files Browse the repository at this point in the history
  • Loading branch information
mununki committed Oct 7, 2022
1 parent 326977a commit a18edd1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
24 changes: 21 additions & 3 deletions jscomp/core/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,29 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
match output_prefix with
| Some output_prefix ->
let output_dir = Filename.dirname output_prefix in
(* TODO: construct J.module_id from e *)
let id = Ident.create "Belt_List" in

(* TODO: pull this function out to top-level *)
let rec module_names_of_expression = function
| J.Var (J.Qualified ({ id = { name } }, _)) -> [ name ]
| J.Caml_block (exprs, _, _, _) ->
exprs
|> List.map (fun (e : J.expression) ->
module_names_of_expression e.expression_desc)
|> List.concat
| _ -> []
in

let module_name =
match module_names_of_expression e.expression_desc with
| [ module_name ] -> module_name
| _ -> assert false
(* TODO: graceful error message here *)
in

let path =
Js_name_of_module_id.string_of_module_id
{ id; kind = Js_op.Ml } ~output_dir
{ id = Ident.create module_name; kind = Js_op.Ml }
~output_dir
(* TODO: where is Js_package_info.module_system ? *)
Js_packages_info.NodeJS
in
Expand Down
4 changes: 2 additions & 2 deletions jscomp/test/Import.res
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ let each = Js.import(Belt.List.forEach)
let eachInt = (list: list<int>, f: int => unit) =>
Js.Promise.then_(each => list->each(f)->Js.Promise.resolve, each)

module type Belt = module type of Belt
module type BeltList = module type of Belt.List

let beltAsModule = Js.import(module(Belt: Belt))
let beltAsModule = Js.import(module(Belt.List: BeltList))
24 changes: 21 additions & 3 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -96699,11 +96699,29 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
match output_prefix with
| Some output_prefix ->
let output_dir = Filename.dirname output_prefix in
(* TODO: construct J.module_id from e *)
let id = Ident.create "Belt_List" in

(* TODO: pull this function out to top-level *)
let rec module_names_of_expression = function
| J.Var (J.Qualified ({ id = { name } }, _)) -> [ name ]
| J.Caml_block (exprs, _, _, _) ->
exprs
|> List.map (fun (e : J.expression) ->
module_names_of_expression e.expression_desc)
|> List.concat
| _ -> []
in

let module_name =
match module_names_of_expression e.expression_desc with
| [ module_name ] -> module_name
| _ -> assert false
(* TODO: graceful error message here *)
in

let path =
Js_name_of_module_id.string_of_module_id
{ id; kind = Js_op.Ml } ~output_dir
{ id = Ident.create module_name; kind = Js_op.Ml }
~output_dir
(* TODO: where is Js_package_info.module_system ? *)
Js_packages_info.NodeJS
in
Expand Down
24 changes: 21 additions & 3 deletions lib/4.06.1/unstable/js_playground_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -96699,11 +96699,29 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
match output_prefix with
| Some output_prefix ->
let output_dir = Filename.dirname output_prefix in
(* TODO: construct J.module_id from e *)
let id = Ident.create "Belt_List" in

(* TODO: pull this function out to top-level *)
let rec module_names_of_expression = function
| J.Var (J.Qualified ({ id = { name } }, _)) -> [ name ]
| J.Caml_block (exprs, _, _, _) ->
exprs
|> List.map (fun (e : J.expression) ->
module_names_of_expression e.expression_desc)
|> List.concat
| _ -> []
in

let module_name =
match module_names_of_expression e.expression_desc with
| [ module_name ] -> module_name
| _ -> assert false
(* TODO: graceful error message here *)
in

let path =
Js_name_of_module_id.string_of_module_id
{ id; kind = Js_op.Ml } ~output_dir
{ id = Ident.create module_name; kind = Js_op.Ml }
~output_dir
(* TODO: where is Js_package_info.module_system ? *)
Js_packages_info.NodeJS
in
Expand Down
24 changes: 21 additions & 3 deletions lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -265893,11 +265893,29 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
match output_prefix with
| Some output_prefix ->
let output_dir = Filename.dirname output_prefix in
(* TODO: construct J.module_id from e *)
let id = Ident.create "Belt_List" in

(* TODO: pull this function out to top-level *)
let rec module_names_of_expression = function
| J.Var (J.Qualified ({ id = { name } }, _)) -> [ name ]
| J.Caml_block (exprs, _, _, _) ->
exprs
|> List.map (fun (e : J.expression) ->
module_names_of_expression e.expression_desc)
|> List.concat
| _ -> []
in

let module_name =
match module_names_of_expression e.expression_desc with
| [ module_name ] -> module_name
| _ -> assert false
(* TODO: graceful error message here *)
in

let path =
Js_name_of_module_id.string_of_module_id
{ id; kind = Js_op.Ml } ~output_dir
{ id = Ident.create module_name; kind = Js_op.Ml }
~output_dir
(* TODO: where is Js_package_info.module_system ? *)
Js_packages_info.NodeJS
in
Expand Down

0 comments on commit a18edd1

Please sign in to comment.