Skip to content

Commit

Permalink
vm: use import attributes instead of import assertions
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
aduh95 authored and targos committed Nov 23, 2023
1 parent c1a8ae3 commit e96f7ef
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
45 changes: 26 additions & 19 deletions doc/api/vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v10.6.0
pr-url: https://github.com/nodejs/node/pull/20300
Expand Down Expand Up @@ -101,7 +101,7 @@ changes:
using it in a production environment.
* `specifier` {string} specifier passed to `import()`
* `script` {vm.Script}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"assert"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -613,6 +613,14 @@ The identifier of the current module, as set in the constructor.

### `module.link(linker)`

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/50141
description: The option `extra.assert` is renamed to `extra.attributes`. The
former name is still provided for backward compatibility.
-->

* `linker` {Function}
* `specifier` {string} The specifier of the requested module:
```mjs
Expand All @@ -623,15 +631,14 @@ The identifier of the current module, as set in the constructor.
* `referencingModule` {vm.Module} The `Module` object `link()` is called on.

* `extra` {Object}
* `assert` {Object} The data from the assertion:
<!-- eslint-skip -->
```js
* `attributes` {Object} The data from the attribute:
```mjs
import foo from 'foo' assert { name: 'value' };
// ^^^^^^^^^^^^^^^^^ the assertion
// ^^^^^^^^^^^^^^^^^ the attribute
```
Per ECMA-262, hosts are expected to ignore assertions that they do not
support, as opposed to, for example, triggering an error if an
unsupported assertion is present.
Per ECMA-262, hosts are expected to trigger an error if an
unsupported attribute is present.
* `assert` {Object} Alias for `extra.attributes`.

* Returns: {vm.Module|Promise}
* Returns: {Promise}
Expand Down Expand Up @@ -730,7 +737,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
-->

Expand Down Expand Up @@ -760,7 +767,7 @@ changes:
`import()` will reject with [`ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING`][].
* `specifier` {string} specifier passed to `import()`
* `module` {vm.Module}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"assert"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -974,7 +981,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v15.9.0
pr-url: https://github.com/nodejs/node/pull/35431
Expand Down Expand Up @@ -1018,7 +1025,7 @@ changes:
considered stable.
* `specifier` {string} specifier passed to `import()`
* `function` {Function}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"assert"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -1204,7 +1211,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v6.3.0
pr-url: https://github.com/nodejs/node/pull/6635
Expand Down Expand Up @@ -1242,7 +1249,7 @@ changes:
using it in a production environment.
* `specifier` {string} specifier passed to `import()`
* `script` {vm.Script}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"assert"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -1282,7 +1289,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v14.6.0
pr-url: https://github.com/nodejs/node/pull/34023
Expand Down Expand Up @@ -1341,7 +1348,7 @@ changes:
using it in a production environment.
* `specifier` {string} specifier passed to `import()`
* `script` {vm.Script}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"assert"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down Expand Up @@ -1385,7 +1392,7 @@ changes:
- v17.0.0
- v16.12.0
pr-url: https://github.com/nodejs/node/pull/40249
description: Added support for import assertions to the
description: Added support for import attributes to the
`importModuleDynamically` parameter.
- version: v6.3.0
pr-url: https://github.com/nodejs/node/pull/6635
Expand Down Expand Up @@ -1421,7 +1428,7 @@ changes:
using it in a production environment.
* `specifier` {string} specifier passed to `import()`
* `script` {vm.Script}
* `importAssertions` {Object} The `"assert"` value passed to the
* `importAttributes` {Object} The `"assert"` value passed to the
[`optionsExpression`][] optional parameter, or an empty object if no value
was provided.
* Returns: {Module Namespace Object|vm.Module} Returning a `vm.Module` is
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/vm/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ class SourceTextModule extends Module {
this[kLink] = async (linker) => {
this.#statusOverride = 'linking';

const promises = this[kWrap].link(async (identifier, assert) => {
const module = await linker(identifier, this, { assert });
const promises = this[kWrap].link(async (identifier, attributes) => {
const module = await linker(identifier, this, { attributes, assert: attributes });
if (module[kWrap] === undefined) {
throw new ERR_VM_MODULE_NOT_MODULE();
}
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-vm-module-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,9 @@ async function asserts() {
await m.link((s, r, p) => {
assert.strictEqual(s, 'foo');
assert.strictEqual(r.identifier, 'm');
assert.strictEqual(p.attributes.n1, 'v1');
assert.strictEqual(p.assert.n1, 'v1');
assert.strictEqual(p.attributes.n2, 'v2');
assert.strictEqual(p.assert.n2, 'v2');
return new SourceTextModule('');
});
Expand Down

0 comments on commit e96f7ef

Please sign in to comment.