Skip to content

Commit 2da66c1

Browse files
authored
vm: use import attributes instead of import assertions
The old import assertions proposal has been renamed to "import attributes" with the following major changes: 1. The keyword is now `with` instead of `assert`. 2. Unknown assertions cause an error rather than being ignored. This PR updates the documentation to encourage folks to use the new syntax, and add aliases to preserve backward compatibility. PR-URL: #50141 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]>
1 parent 82cc1d1 commit 2da66c1

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

doc/api/vm.md

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ changes:
6262
- v17.0.0
6363
- v16.12.0
6464
pr-url: https://github.com/nodejs/node/pull/40249
65-
description: Added support for import assertions to the
65+
description: Added support for import attributes to the
6666
`importModuleDynamically` parameter.
6767
- version: v10.6.0
6868
pr-url: https://github.com/nodejs/node/pull/20300
@@ -103,7 +103,7 @@ changes:
103103
[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][].
104104
* `specifier` {string} specifier passed to `import()`
105105
* `script` {vm.Script}
106-
* `importAssertions` {Object} The `"assert"` value passed to the
106+
* `importAttributes` {Object} The `"with"` value passed to the
107107
[`optionsExpression`][] optional parameter, or an empty object if no value
108108
was provided.
109109
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -617,6 +617,14 @@ The identifier of the current module, as set in the constructor.
617617

618618
### `module.link(linker)`
619619

620+
<!-- YAML
621+
changes:
622+
- version: REPLACEME
623+
pr-url: https://github.com/nodejs/node/pull/50141
624+
description: The option `extra.assert` is renamed to `extra.attributes`. The
625+
former name is still provided for backward compatibility.
626+
-->
627+
620628
* `linker` {Function}
621629
* `specifier` {string} The specifier of the requested module:
622630
```mjs
@@ -627,15 +635,14 @@ The identifier of the current module, as set in the constructor.
627635
* `referencingModule` {vm.Module} The `Module` object `link()` is called on.
628636

629637
* `extra` {Object}
630-
* `assert` {Object} The data from the assertion:
631-
<!-- eslint-skip -->
632-
```js
633-
import foo from 'foo' assert { name: 'value' };
634-
// ^^^^^^^^^^^^^^^^^ the assertion
638+
* `attributes` {Object} The data from the attribute:
639+
```mjs
640+
import foo from 'foo' with { name: 'value' };
641+
// ^^^^^^^^^^^^^^^^^ the attribute
635642
```
636-
Per ECMA-262, hosts are expected to ignore assertions that they do not
637-
support, as opposed to, for example, triggering an error if an
638-
unsupported assertion is present.
643+
Per ECMA-262, hosts are expected to trigger an error if an
644+
unsupported attribute is present.
645+
* `assert` {Object} Alias for `extra.attributes`.
639646

640647
* Returns: {vm.Module|Promise}
641648
* Returns: {Promise}
@@ -734,7 +741,7 @@ changes:
734741
- v17.0.0
735742
- v16.12.0
736743
pr-url: https://github.com/nodejs/node/pull/40249
737-
description: Added support for import assertions to the
744+
description: Added support for import attributes to the
738745
`importModuleDynamically` parameter.
739746
-->
740747

@@ -767,7 +774,7 @@ changes:
767774
[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][].
768775
* `specifier` {string} specifier passed to `import()`
769776
* `module` {vm.Module}
770-
* `importAssertions` {Object} The `"assert"` value passed to the
777+
* `importAttributes` {Object} The `"with"` value passed to the
771778
[`optionsExpression`][] optional parameter, or an empty object if no value
772779
was provided.
773780
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -982,7 +989,7 @@ changes:
982989
- v17.0.0
983990
- v16.12.0
984991
pr-url: https://github.com/nodejs/node/pull/40249
985-
description: Added support for import assertions to the
992+
description: Added support for import attributes to the
986993
`importModuleDynamically` parameter.
987994
- version: v15.9.0
988995
pr-url: https://github.com/nodejs/node/pull/35431
@@ -1028,7 +1035,7 @@ changes:
10281035
[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][].
10291036
* `specifier` {string} specifier passed to `import()`
10301037
* `function` {Function}
1031-
* `importAssertions` {Object} The `"assert"` value passed to the
1038+
* `importAttributes` {Object} The `"with"` value passed to the
10321039
[`optionsExpression`][] optional parameter, or an empty object if no value
10331040
was provided.
10341041
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1214,7 +1221,7 @@ changes:
12141221
- v17.0.0
12151222
- v16.12.0
12161223
pr-url: https://github.com/nodejs/node/pull/40249
1217-
description: Added support for import assertions to the
1224+
description: Added support for import attributes to the
12181225
`importModuleDynamically` parameter.
12191226
- version: v6.3.0
12201227
pr-url: https://github.com/nodejs/node/pull/6635
@@ -1254,7 +1261,7 @@ changes:
12541261
[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][].
12551262
* `specifier` {string} specifier passed to `import()`
12561263
* `script` {vm.Script}
1257-
* `importAssertions` {Object} The `"assert"` value passed to the
1264+
* `importAttributes` {Object} The `"with"` value passed to the
12581265
[`optionsExpression`][] optional parameter, or an empty object if no value
12591266
was provided.
12601267
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1294,7 +1301,7 @@ changes:
12941301
- v17.0.0
12951302
- v16.12.0
12961303
pr-url: https://github.com/nodejs/node/pull/40249
1297-
description: Added support for import assertions to the
1304+
description: Added support for import attributes to the
12981305
`importModuleDynamically` parameter.
12991306
- version: v14.6.0
13001307
pr-url: https://github.com/nodejs/node/pull/34023
@@ -1355,7 +1362,7 @@ changes:
13551362
[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][].
13561363
* `specifier` {string} specifier passed to `import()`
13571364
* `script` {vm.Script}
1358-
* `importAssertions` {Object} The `"assert"` value passed to the
1365+
* `importAttributes` {Object} The `"with"` value passed to the
13591366
[`optionsExpression`][] optional parameter, or an empty object if no value
13601367
was provided.
13611368
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1399,7 +1406,7 @@ changes:
13991406
- v17.0.0
14001407
- v16.12.0
14011408
pr-url: https://github.com/nodejs/node/pull/40249
1402-
description: Added support for import assertions to the
1409+
description: Added support for import attributes to the
14031410
`importModuleDynamically` parameter.
14041411
- version: v6.3.0
14051412
pr-url: https://github.com/nodejs/node/pull/6635
@@ -1437,7 +1444,7 @@ changes:
14371444
[`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING_FLAG`][].
14381445
* `specifier` {string} specifier passed to `import()`
14391446
* `script` {vm.Script}
1440-
* `importAssertions` {Object} The `"assert"` value passed to the
1447+
* `importAttributes` {Object} The `"with"` value passed to the
14411448
[`optionsExpression`][] optional parameter, or an empty object if no value
14421449
was provided.
14431450
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is

lib/internal/vm/module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ class SourceTextModule extends Module {
305305
this[kLink] = async (linker) => {
306306
this.#statusOverride = 'linking';
307307

308-
const promises = this[kWrap].link(async (identifier, assert) => {
309-
const module = await linker(identifier, this, { assert });
308+
const promises = this[kWrap].link(async (identifier, attributes) => {
309+
const module = await linker(identifier, this, { attributes, assert: attributes });
310310
if (module[kWrap] === undefined) {
311311
throw new ERR_VM_MODULE_NOT_MODULE();
312312
}

test/parallel/test-vm-module-link.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
// Flags: --experimental-vm-modules
3+
// Flags: --experimental-vm-modules --harmony-import-attributes
44

55
const common = require('../common');
66

@@ -126,12 +126,14 @@ async function circular2() {
126126

127127
async function asserts() {
128128
const m = new SourceTextModule(`
129-
import "foo" assert { n1: 'v1', n2: 'v2' };
129+
import "foo" with { n1: 'v1', n2: 'v2' };
130130
`, { identifier: 'm' });
131131
await m.link((s, r, p) => {
132132
assert.strictEqual(s, 'foo');
133133
assert.strictEqual(r.identifier, 'm');
134+
assert.strictEqual(p.attributes.n1, 'v1');
134135
assert.strictEqual(p.assert.n1, 'v1');
136+
assert.strictEqual(p.attributes.n2, 'v2');
135137
assert.strictEqual(p.assert.n2, 'v2');
136138
return new SourceTextModule('');
137139
});

0 commit comments

Comments
 (0)