Skip to content

Commit dc1553d

Browse files
authored
🤖 Merge PR DefinitelyTyped#72691 fix(node/vm): correct SourceTextModuleOptions["importModuleDynamically"] by @YieldRay
1 parent 4d5af71 commit dc1553d

File tree

6 files changed

+113
-5
lines changed

6 files changed

+113
-5
lines changed

‎types/node/test/vm.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,33 @@ import {
144144
this.setExport("default", obj);
145145
});
146146
});
147+
148+
{
149+
const script = new Script("import(\"foo.json\", { with: { type: \"json\" } })", {
150+
async importModuleDynamically(specifier, referrer, importAttributes) {
151+
specifier; // $ExpectType string
152+
referrer; // $ExpectType Script
153+
importAttributes; // $ExpectType ImportAttributes
154+
const m = new SyntheticModule(["bar"], () => {});
155+
await m.link(() => {
156+
throw new Error("unreachable");
157+
});
158+
m.setExport("bar", { hello: "world" });
159+
return m;
160+
},
161+
});
162+
163+
const module = new SourceTextModule("import(\"foo.json\", { with: { type: \"json\" } })", {
164+
async importModuleDynamically(specifier, referrer, importAttributes) {
165+
specifier; // $ExpectType string
166+
referrer; // $ExpectType SourceTextModule
167+
importAttributes; // $ExpectType ImportAttributes
168+
const m = new SyntheticModule(["bar"], () => {});
169+
await m.link(() => {
170+
throw new Error("unreachable");
171+
});
172+
m.setExport("bar", { hello: "world" });
173+
return m;
174+
},
175+
});
176+
}

‎types/node/v18/test/vm.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,33 @@ import {
152152
this.setExport("default", obj);
153153
});
154154
});
155+
156+
{
157+
const script = new Script("import(\"foo.json\", { with: { type: \"json\" } })", {
158+
async importModuleDynamically(specifier, referrer, importAttributes) {
159+
specifier; // $ExpectType string
160+
referrer; // $ExpectType Script
161+
importAttributes; // $ExpectType ImportAttributes
162+
const m = new SyntheticModule(["bar"], () => {});
163+
await m.link(() => {
164+
throw new Error("unreachable");
165+
});
166+
m.setExport("bar", { hello: "world" });
167+
return m;
168+
},
169+
});
170+
171+
const module = new SourceTextModule("import(\"foo.json\", { with: { type: \"json\" } })", {
172+
async importModuleDynamically(specifier, referrer, importAttributes) {
173+
specifier; // $ExpectType string
174+
referrer; // $ExpectType SourceTextModule
175+
importAttributes; // $ExpectType ImportAttributes
176+
const m = new SyntheticModule(["bar"], () => {});
177+
await m.link(() => {
178+
throw new Error("unreachable");
179+
});
180+
m.setExport("bar", { hello: "world" });
181+
return m;
182+
},
183+
});
184+
}

‎types/node/v18/vm.d.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ declare module "vm" {
6868
* If this option is not specified, calls to `import()` will reject with `ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`.
6969
*/
7070
importModuleDynamically?:
71-
| ((specifier: string, script: Script, importAttributes: ImportAttributes) => Module)
71+
| ((specifier: string, script: Script, importAttributes: ImportAttributes) => Module | Promise<Module>)
7272
| undefined;
7373
}
7474
interface RunningScriptOptions extends BaseOptions {
@@ -620,7 +620,13 @@ declare module "vm" {
620620
* Called during evaluation of this module to initialize the `import.meta`.
621621
*/
622622
initializeImportMeta?: ((meta: ImportMeta, module: SourceTextModule) => void) | undefined;
623-
importModuleDynamically?: ScriptOptions["importModuleDynamically"] | undefined;
623+
importModuleDynamically?:
624+
| ((
625+
specifier: string,
626+
referrer: SourceTextModule,
627+
importAttributes: ImportAttributes,
628+
) => Module | Promise<Module>)
629+
| undefined;
624630
}
625631
class SourceTextModule extends Module {
626632
/**

‎types/node/v20/test/vm.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,33 @@ import {
152152
this.setExport("default", obj);
153153
});
154154
});
155+
156+
{
157+
const script = new Script("import(\"foo.json\", { with: { type: \"json\" } })", {
158+
async importModuleDynamically(specifier, referrer, importAttributes) {
159+
specifier; // $ExpectType string
160+
referrer; // $ExpectType Script
161+
importAttributes; // $ExpectType ImportAttributes
162+
const m = new SyntheticModule(["bar"], () => {});
163+
await m.link(() => {
164+
throw new Error("unreachable");
165+
});
166+
m.setExport("bar", { hello: "world" });
167+
return m;
168+
},
169+
});
170+
171+
const module = new SourceTextModule("import(\"foo.json\", { with: { type: \"json\" } })", {
172+
async importModuleDynamically(specifier, referrer, importAttributes) {
173+
specifier; // $ExpectType string
174+
referrer; // $ExpectType SourceTextModule
175+
importAttributes; // $ExpectType ImportAttributes
176+
const m = new SyntheticModule(["bar"], () => {});
177+
await m.link(() => {
178+
throw new Error("unreachable");
179+
});
180+
m.setExport("bar", { hello: "world" });
181+
return m;
182+
},
183+
});
184+
}

‎types/node/v20/vm.d.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ declare module "vm" {
6969
* [Support of dynamic `import()` in compilation APIs](https://nodejs.org/docs/latest-v20.x/api/vm.html#support-of-dynamic-import-in-compilation-apis).
7070
*/
7171
importModuleDynamically?:
72-
| ((specifier: string, script: Script, importAttributes: ImportAttributes) => Module)
72+
| ((specifier: string, script: Script, importAttributes: ImportAttributes) => Module | Promise<Module>)
7373
| typeof constants.USE_MAIN_CONTEXT_DEFAULT_LOADER
7474
| undefined;
7575
}
@@ -815,7 +815,13 @@ declare module "vm" {
815815
* Called during evaluation of this module to initialize the `import.meta`.
816816
*/
817817
initializeImportMeta?: ((meta: ImportMeta, module: SourceTextModule) => void) | undefined;
818-
importModuleDynamically?: ScriptOptions["importModuleDynamically"] | undefined;
818+
importModuleDynamically?:
819+
| ((
820+
specifier: string,
821+
referrer: SourceTextModule,
822+
importAttributes: ImportAttributes,
823+
) => Module | Promise<Module>)
824+
| undefined;
819825
}
820826
/**
821827
* This feature is only available with the `--experimental-vm-modules` command

‎types/node/vm.d.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,13 @@ declare module "vm" {
856856
* Called during evaluation of this module to initialize the `import.meta`.
857857
*/
858858
initializeImportMeta?: ((meta: ImportMeta, module: SourceTextModule) => void) | undefined;
859-
importModuleDynamically?: ScriptOptions["importModuleDynamically"] | undefined;
859+
importModuleDynamically?:
860+
| ((
861+
specifier: string,
862+
referrer: SourceTextModule,
863+
importAttributes: ImportAttributes,
864+
) => Module | Promise<Module>)
865+
| undefined;
860866
}
861867
/**
862868
* This feature is only available with the `--experimental-vm-modules` command

0 commit comments

Comments
 (0)