Skip to content

Commit a18edd1

Browse files
committed
extract module id from J.expression
1 parent 326977a commit a18edd1

File tree

5 files changed

+86
-14
lines changed

5 files changed

+86
-14
lines changed

jscomp/core/lam_compile_primitive.ml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,29 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
8787
match output_prefix with
8888
| Some output_prefix ->
8989
let output_dir = Filename.dirname output_prefix in
90-
(* TODO: construct J.module_id from e *)
91-
let id = Ident.create "Belt_List" in
90+
91+
(* TODO: pull this function out to top-level *)
92+
let rec module_names_of_expression = function
93+
| J.Var (J.Qualified ({ id = { name } }, _)) -> [ name ]
94+
| J.Caml_block (exprs, _, _, _) ->
95+
exprs
96+
|> List.map (fun (e : J.expression) ->
97+
module_names_of_expression e.expression_desc)
98+
|> List.concat
99+
| _ -> []
100+
in
101+
102+
let module_name =
103+
match module_names_of_expression e.expression_desc with
104+
| [ module_name ] -> module_name
105+
| _ -> assert false
106+
(* TODO: graceful error message here *)
107+
in
108+
92109
let path =
93110
Js_name_of_module_id.string_of_module_id
94-
{ id; kind = Js_op.Ml } ~output_dir
111+
{ id = Ident.create module_name; kind = Js_op.Ml }
112+
~output_dir
95113
(* TODO: where is Js_package_info.module_system ? *)
96114
Js_packages_info.NodeJS
97115
in

jscomp/test/Import.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ let each = Js.import(Belt.List.forEach)
33
let eachInt = (list: list<int>, f: int => unit) =>
44
Js.Promise.then_(each => list->each(f)->Js.Promise.resolve, each)
55

6-
module type Belt = module type of Belt
6+
module type BeltList = module type of Belt.List
77

8-
let beltAsModule = Js.import(module(Belt: Belt))
8+
let beltAsModule = Js.import(module(Belt.List: BeltList))

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96699,11 +96699,29 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
9669996699
match output_prefix with
9670096700
| Some output_prefix ->
9670196701
let output_dir = Filename.dirname output_prefix in
96702-
(* TODO: construct J.module_id from e *)
96703-
let id = Ident.create "Belt_List" in
96702+
96703+
(* TODO: pull this function out to top-level *)
96704+
let rec module_names_of_expression = function
96705+
| J.Var (J.Qualified ({ id = { name } }, _)) -> [ name ]
96706+
| J.Caml_block (exprs, _, _, _) ->
96707+
exprs
96708+
|> List.map (fun (e : J.expression) ->
96709+
module_names_of_expression e.expression_desc)
96710+
|> List.concat
96711+
| _ -> []
96712+
in
96713+
96714+
let module_name =
96715+
match module_names_of_expression e.expression_desc with
96716+
| [ module_name ] -> module_name
96717+
| _ -> assert false
96718+
(* TODO: graceful error message here *)
96719+
in
96720+
9670496721
let path =
9670596722
Js_name_of_module_id.string_of_module_id
96706-
{ id; kind = Js_op.Ml } ~output_dir
96723+
{ id = Ident.create module_name; kind = Js_op.Ml }
96724+
~output_dir
9670796725
(* TODO: where is Js_package_info.module_system ? *)
9670896726
Js_packages_info.NodeJS
9670996727
in

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96699,11 +96699,29 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
9669996699
match output_prefix with
9670096700
| Some output_prefix ->
9670196701
let output_dir = Filename.dirname output_prefix in
96702-
(* TODO: construct J.module_id from e *)
96703-
let id = Ident.create "Belt_List" in
96702+
96703+
(* TODO: pull this function out to top-level *)
96704+
let rec module_names_of_expression = function
96705+
| J.Var (J.Qualified ({ id = { name } }, _)) -> [ name ]
96706+
| J.Caml_block (exprs, _, _, _) ->
96707+
exprs
96708+
|> List.map (fun (e : J.expression) ->
96709+
module_names_of_expression e.expression_desc)
96710+
|> List.concat
96711+
| _ -> []
96712+
in
96713+
96714+
let module_name =
96715+
match module_names_of_expression e.expression_desc with
96716+
| [ module_name ] -> module_name
96717+
| _ -> assert false
96718+
(* TODO: graceful error message here *)
96719+
in
96720+
9670496721
let path =
9670596722
Js_name_of_module_id.string_of_module_id
96706-
{ id; kind = Js_op.Ml } ~output_dir
96723+
{ id = Ident.create module_name; kind = Js_op.Ml }
96724+
~output_dir
9670796725
(* TODO: where is Js_package_info.module_system ? *)
9670896726
Js_packages_info.NodeJS
9670996727
in

lib/4.06.1/whole_compiler.ml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265893,11 +265893,29 @@ let translate ?output_prefix loc (cxt : Lam_compile_context.t)
265893265893
match output_prefix with
265894265894
| Some output_prefix ->
265895265895
let output_dir = Filename.dirname output_prefix in
265896-
(* TODO: construct J.module_id from e *)
265897-
let id = Ident.create "Belt_List" in
265896+
265897+
(* TODO: pull this function out to top-level *)
265898+
let rec module_names_of_expression = function
265899+
| J.Var (J.Qualified ({ id = { name } }, _)) -> [ name ]
265900+
| J.Caml_block (exprs, _, _, _) ->
265901+
exprs
265902+
|> List.map (fun (e : J.expression) ->
265903+
module_names_of_expression e.expression_desc)
265904+
|> List.concat
265905+
| _ -> []
265906+
in
265907+
265908+
let module_name =
265909+
match module_names_of_expression e.expression_desc with
265910+
| [ module_name ] -> module_name
265911+
| _ -> assert false
265912+
(* TODO: graceful error message here *)
265913+
in
265914+
265898265915
let path =
265899265916
Js_name_of_module_id.string_of_module_id
265900-
{ id; kind = Js_op.Ml } ~output_dir
265917+
{ id = Ident.create module_name; kind = Js_op.Ml }
265918+
~output_dir
265901265919
(* TODO: where is Js_package_info.module_system ? *)
265902265920
Js_packages_info.NodeJS
265903265921
in

0 commit comments

Comments
 (0)