Skip to content

Commit d7cd94b

Browse files
cknittaspeddro
authored andcommitted
Import the default export for @scope starting with "default" (rescript-lang#8923)
Signed-off-by: Pedro Castro <aspeddro@gmail.com>
1 parent bcf6d69 commit d7cd94b

6 files changed

Lines changed: 63 additions & 2 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Import the default export, instead of the module namespace, for `@module` externals whose `@scope` starts with `"default"`.

‎compiler/core/lam_compile_external_call.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,9 @@ let translate_scoped_module_val
223223
E.external_var_field ?import_attributes ~external_name:bundle ~field:fn
224224
~default id
225225
| x :: rest ->
226-
(* TODO: what happens when scope contains "default" ?*)
227-
let default = false in
226+
(* A leading "default" scope is the module's default export: import
227+
it as such, like [external x: t = "default"] *)
228+
let default = x = "default" in
228229
let id =
229230
Lam_compile_env.add_js_module ?import_attributes module_bind_name bundle
230231
default ~dynamic_import:false

‎tests/commonjs_tests/src/scope_default_import.js‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// A leading "default" scope imports the module's default export
2+
3+
@module("mod1") @scope(("default", "x")) external fn: int => unit = "fn"
4+
@module("mod1") @scope("default") external value: int = "value"
5+
@module("mod1") @new @scope("default") external make: unit => {"a": int} = "Klass"
6+
@module("mod1") @scope("other") external other: int = "other"
7+
8+
@module("mod2") @scope("default") external defaultOfDefault: int = "default"
9+
10+
let f = () => fn(value + other + defaultOfDefault)
11+
let g = () => make()
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Mod1 from "mod1";
4+
import Mod1$1 from "mod1";
5+
import Mod2 from "mod2";
6+
7+
function f() {
8+
Mod1$1.x.fn((Mod1$1.value + Mod1.other.other | 0) + Mod2.default | 0);
9+
}
10+
11+
function g() {
12+
return new (Mod1$1.Klass)();
13+
}
14+
15+
export {
16+
f,
17+
g,
18+
}
19+
/* mod1 Not a pure module */
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// A leading "default" scope imports the module's default export
2+
3+
@module("mod1") @scope(("default", "x")) external fn: int => unit = "fn"
4+
@module("mod1") @scope("default") external value: int = "value"
5+
@module("mod1") @new @scope("default") external make: unit => {"a": int} = "Klass"
6+
@module("mod1") @scope("other") external other: int = "other"
7+
8+
@module("mod2") @scope("default") external defaultOfDefault: int = "default"
9+
10+
let f = () => fn(value + other + defaultOfDefault)
11+
let g = () => make()

0 commit comments

Comments
 (0)