Skip to content

Commit f655b26

Browse files
committed
fix #274: provide type safe file local variables
1 parent f9e0e47 commit f655b26

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

jscomp/others/bs_node.mli

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,10 @@ type node_module = <
4343
children : node_module array ;
4444
paths : string array;
4545
> Js.t
46+
47+
type node_require = <
48+
main : node_module Js.undefined;
49+
50+
resolve : string -> string [@bs]
51+
(* @raise exception *)
52+
> Js.t

jscomp/syntax/ppx_entry.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,15 @@ let rec unsafe_mapper : Ast_mapper.mapper =
310310
(Ast_payload.raw_string_payload loc "module"))
311311
(Typ.constr ~loc
312312
{ txt = Ldot (Lident "Bs_node", "node_module") ;
313-
loc} [] )
313+
loc} [] )
314+
| Some {txt = Lident "__require"}
315+
->
316+
Exp.constraint_ ~loc
317+
(Ast_util.handle_raw loc
318+
(Ast_payload.raw_string_payload loc "require"))
319+
(Typ.constr ~loc
320+
{ txt = Ldot (Lident "Bs_node", "node_require") ;
321+
loc} [] )
314322
| Some _ | None -> Location.raise_errorf ~loc "Ilegal payload"
315323
end
316324

jscomp/test/.depend

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ test_react_case.cmj :
690690
test_react_case.cmx :
691691
test_regex.cmj :
692692
test_regex.cmx :
693+
test_require.cmj : ../runtime/js.cmj
694+
test_require.cmx : ../runtime/js.cmx
693695
test_runtime_encoding.cmj : ../stdlib/array.cmi
694696
test_runtime_encoding.cmx : ../stdlib/array.cmx
695697
test_scope.cmj :
@@ -1448,6 +1450,8 @@ test_react_case.cmo :
14481450
test_react_case.cmj :
14491451
test_regex.cmo :
14501452
test_regex.cmj :
1453+
test_require.cmo : ../runtime/js.cmo
1454+
test_require.cmj : ../runtime/js.cmj
14511455
test_runtime_encoding.cmo : ../stdlib/array.cmi
14521456
test_runtime_encoding.cmj : ../stdlib/array.cmj
14531457
test_scope.cmo :

jscomp/test/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ OTHERS := a test_ari test_export2 test_internalOO test_obj_simple_ffi test_scope
5959
optional_ffi_test poly_variant_test \
6060
bs_rest_test infer_type_test fs_test module_as_function\
6161
test_case_set test_mutliple string_bound_get_test inline_string_test\
62-
ppx_this_obj_test unsafe_obj_external gpr_627_test jsoo_485_test jsoo_400_test
62+
ppx_this_obj_test unsafe_obj_external gpr_627_test jsoo_485_test jsoo_400_test \
63+
test_require
6364

6465

6566
SOURCE_LIST := js_dyn $(OTHERS)

jscomp/test/test_require.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.9.3 , PLEASE EDIT WITH CARE
2+
'use strict';
3+
4+
5+
var u = (require);
6+
7+
console.log(u.resolve("./test_require.js"));
8+
9+
if ((require).main === (module)) {
10+
console.log("is main");
11+
}
12+
else {
13+
console.log("not main");
14+
}
15+
16+
exports.u = u;
17+
/* u Not a pure module */

jscomp/test/test_require.ml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
3+
let u = [%bs.node __require]
4+
5+
let () =
6+
Js.log @@ u#@resolve "./test_require.js"
7+
let () =
8+
if [%bs.node __require]##main ==
9+
Js.Undefined.return
10+
[%bs.node __module] then
11+
Js.log "is main"
12+
else
13+
Js.log "not main"

0 commit comments

Comments
 (0)