Skip to content

Commit 57efd52

Browse files
aduh95targos
authored andcommitted
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 ee65e44 commit 57efd52

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

doc/api/vm.md

+27-20
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
@@ -101,7 +101,7 @@ changes:
101101
using it in a production environment.
102102
* `specifier` {string} specifier passed to `import()`
103103
* `script` {vm.Script}
104-
* `importAssertions` {Object} The `"assert"` value passed to the
104+
* `importAttributes` {Object} The `"with"` value passed to the
105105
[`optionsExpression`][] optional parameter, or an empty object if no value
106106
was provided.
107107
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -615,6 +615,14 @@ The identifier of the current module, as set in the constructor.
615615

616616
### `module.link(linker)`
617617

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

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

638645
* Returns: {vm.Module|Promise}
639646
* Returns: {Promise}
@@ -732,7 +739,7 @@ changes:
732739
- v17.0.0
733740
- v16.12.0
734741
pr-url: https://github.com/nodejs/node/pull/40249
735-
description: Added support for import assertions to the
742+
description: Added support for import attributes to the
736743
`importModuleDynamically` parameter.
737744
-->
738745

@@ -762,7 +769,7 @@ changes:
762769
`import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][].
763770
* `specifier` {string} specifier passed to `import()`
764771
* `module` {vm.Module}
765-
* `importAssertions` {Object} The `"assert"` value passed to the
772+
* `importAttributes` {Object} The `"with"` value passed to the
766773
[`optionsExpression`][] optional parameter, or an empty object if no value
767774
was provided.
768775
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -977,7 +984,7 @@ changes:
977984
- v17.0.0
978985
- v16.12.0
979986
pr-url: https://github.com/nodejs/node/pull/40249
980-
description: Added support for import assertions to the
987+
description: Added support for import attributes to the
981988
`importModuleDynamically` parameter.
982989
- version: v15.9.0
983990
pr-url: https://github.com/nodejs/node/pull/35431
@@ -1021,7 +1028,7 @@ changes:
10211028
considered stable.
10221029
* `specifier` {string} specifier passed to `import()`
10231030
* `function` {Function}
1024-
* `importAssertions` {Object} The `"assert"` value passed to the
1031+
* `importAttributes` {Object} The `"with"` value passed to the
10251032
[`optionsExpression`][] optional parameter, or an empty object if no value
10261033
was provided.
10271034
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1207,7 +1214,7 @@ changes:
12071214
- v17.0.0
12081215
- v16.12.0
12091216
pr-url: https://github.com/nodejs/node/pull/40249
1210-
description: Added support for import assertions to the
1217+
description: Added support for import attributes to the
12111218
`importModuleDynamically` parameter.
12121219
- version: v6.3.0
12131220
pr-url: https://github.com/nodejs/node/pull/6635
@@ -1245,7 +1252,7 @@ changes:
12451252
using it in a production environment.
12461253
* `specifier` {string} specifier passed to `import()`
12471254
* `script` {vm.Script}
1248-
* `importAssertions` {Object} The `"assert"` value passed to the
1255+
* `importAttributes` {Object} The `"with"` value passed to the
12491256
[`optionsExpression`][] optional parameter, or an empty object if no value
12501257
was provided.
12511258
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1285,7 +1292,7 @@ changes:
12851292
- v17.0.0
12861293
- v16.12.0
12871294
pr-url: https://github.com/nodejs/node/pull/40249
1288-
description: Added support for import assertions to the
1295+
description: Added support for import attributes to the
12891296
`importModuleDynamically` parameter.
12901297
- version: v14.6.0
12911298
pr-url: https://github.com/nodejs/node/pull/34023
@@ -1344,7 +1351,7 @@ changes:
13441351
using it in a production environment.
13451352
* `specifier` {string} specifier passed to `import()`
13461353
* `script` {vm.Script}
1347-
* `importAssertions` {Object} The `"assert"` value passed to the
1354+
* `importAttributes` {Object} The `"with"` value passed to the
13481355
[`optionsExpression`][] optional parameter, or an empty object if no value
13491356
was provided.
13501357
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1388,7 +1395,7 @@ changes:
13881395
- v17.0.0
13891396
- v16.12.0
13901397
pr-url: https://github.com/nodejs/node/pull/40249
1391-
description: Added support for import assertions to the
1398+
description: Added support for import attributes to the
13921399
`importModuleDynamically` parameter.
13931400
- version: v6.3.0
13941401
pr-url: https://github.com/nodejs/node/pull/6635
@@ -1424,7 +1431,7 @@ changes:
14241431
using it in a production environment.
14251432
* `specifier` {string} specifier passed to `import()`
14261433
* `script` {vm.Script}
1427-
* `importAssertions` {Object} The `"assert"` value passed to the
1434+
* `importAttributes` {Object} The `"with"` value passed to the
14281435
[`optionsExpression`][] optional parameter, or an empty object if no value
14291436
was provided.
14301437
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is

lib/internal/vm/module.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ class SourceTextModule extends Module {
304304
this[kLink] = async (linker) => {
305305
this.#statusOverride = 'linking';
306306

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

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

+4-2
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)