Skip to content

Commit e96f7ef

Browse files
aduh95targos
authored andcommittedNov 23, 2023
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 Backport-PR-URL: #50669 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
1 parent c1a8ae3 commit e96f7ef

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed
 

‎doc/api/vm.md

+26-19
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 `"assert"` 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
@@ -613,6 +613,14 @@ The identifier of the current module, as set in the constructor.
613613

614614
### `module.link(linker)`
615615

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

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

636643
* Returns: {vm.Module|Promise}
637644
* Returns: {Promise}
@@ -730,7 +737,7 @@ changes:
730737
- v17.0.0
731738
- v16.12.0
732739
pr-url: https://github.com/nodejs/node/pull/40249
733-
description: Added support for import assertions to the
740+
description: Added support for import attributes to the
734741
`importModuleDynamically` parameter.
735742
-->
736743

@@ -760,7 +767,7 @@ changes:
760767
`import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][].
761768
* `specifier` {string} specifier passed to `import()`
762769
* `module` {vm.Module}
763-
* `importAssertions` {Object} The `"assert"` value passed to the
770+
* `importAttributes` {Object} The `"assert"` value passed to the
764771
[`optionsExpression`][] optional parameter, or an empty object if no value
765772
was provided.
766773
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -974,7 +981,7 @@ changes:
974981
- v17.0.0
975982
- v16.12.0
976983
pr-url: https://github.com/nodejs/node/pull/40249
977-
description: Added support for import assertions to the
984+
description: Added support for import attributes to the
978985
`importModuleDynamically` parameter.
979986
- version: v15.9.0
980987
pr-url: https://github.com/nodejs/node/pull/35431
@@ -1018,7 +1025,7 @@ changes:
10181025
considered stable.
10191026
* `specifier` {string} specifier passed to `import()`
10201027
* `function` {Function}
1021-
* `importAssertions` {Object} The `"assert"` value passed to the
1028+
* `importAttributes` {Object} The `"assert"` value passed to the
10221029
[`optionsExpression`][] optional parameter, or an empty object if no value
10231030
was provided.
10241031
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1204,7 +1211,7 @@ changes:
12041211
- v17.0.0
12051212
- v16.12.0
12061213
pr-url: https://github.com/nodejs/node/pull/40249
1207-
description: Added support for import assertions to the
1214+
description: Added support for import attributes to the
12081215
`importModuleDynamically` parameter.
12091216
- version: v6.3.0
12101217
pr-url: https://github.com/nodejs/node/pull/6635
@@ -1242,7 +1249,7 @@ changes:
12421249
using it in a production environment.
12431250
* `specifier` {string} specifier passed to `import()`
12441251
* `script` {vm.Script}
1245-
* `importAssertions` {Object} The `"assert"` value passed to the
1252+
* `importAttributes` {Object} The `"assert"` value passed to the
12461253
[`optionsExpression`][] optional parameter, or an empty object if no value
12471254
was provided.
12481255
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1282,7 +1289,7 @@ changes:
12821289
- v17.0.0
12831290
- v16.12.0
12841291
pr-url: https://github.com/nodejs/node/pull/40249
1285-
description: Added support for import assertions to the
1292+
description: Added support for import attributes to the
12861293
`importModuleDynamically` parameter.
12871294
- version: v14.6.0
12881295
pr-url: https://github.com/nodejs/node/pull/34023
@@ -1341,7 +1348,7 @@ changes:
13411348
using it in a production environment.
13421349
* `specifier` {string} specifier passed to `import()`
13431350
* `script` {vm.Script}
1344-
* `importAssertions` {Object} The `"assert"` value passed to the
1351+
* `importAttributes` {Object} The `"assert"` value passed to the
13451352
[`optionsExpression`][] optional parameter, or an empty object if no value
13461353
was provided.
13471354
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
@@ -1385,7 +1392,7 @@ changes:
13851392
- v17.0.0
13861393
- v16.12.0
13871394
pr-url: https://github.com/nodejs/node/pull/40249
1388-
description: Added support for import assertions to the
1395+
description: Added support for import attributes to the
13891396
`importModuleDynamically` parameter.
13901397
- version: v6.3.0
13911398
pr-url: https://github.com/nodejs/node/pull/6635
@@ -1421,7 +1428,7 @@ changes:
14211428
using it in a production environment.
14221429
* `specifier` {string} specifier passed to `import()`
14231430
* `script` {vm.Script}
1424-
* `importAssertions` {Object} The `"assert"` value passed to the
1431+
* `importAttributes` {Object} The `"assert"` value passed to the
14251432
[`optionsExpression`][] optional parameter, or an empty object if no value
14261433
was provided.
14271434
* 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
@@ -302,8 +302,8 @@ class SourceTextModule extends Module {
302302
this[kLink] = async (linker) => {
303303
this.#statusOverride = 'linking';
304304

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

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

+2
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ async function asserts() {
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)
Please sign in to comment.