Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v20.x backport] esm: mark import attributes and JSON module as stable #55961

Open
wants to merge 4 commits into
base: v20.x-staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

# Reset this number to 0 on major V8 upgrades.
# Increment by one for each non-official patch applied to deps/v8.
'v8_embedder_string': '-node.23',
'v8_embedder_string': '-node.25',

##### V8 defaults for Node.js #####

Expand Down
3 changes: 3 additions & 0 deletions deps/v8/src/common/message-template.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ namespace internal {
T(IllegalInvocation, "Illegal invocation") \
T(ImmutablePrototypeSet, \
"Immutable prototype object '%' cannot have their prototype set") \
T(ImportAssertDeprecated, \
"'assert' is deprecated in import statements and support will be removed " \
"in %; use 'with' instead") \
T(ImportAssertionDuplicateKey, "Import assertion has duplicate key '%'") \
T(ImportCallNotNewExpression, "Cannot use new with import") \
T(ImportOutsideModule, "Cannot use import statement outside a module") \
Expand Down
14 changes: 14 additions & 0 deletions deps/v8/src/execution/isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5193,6 +5193,20 @@ MaybeHandle<FixedArray> Isolate::GetImportAssertionsFromArgument(
// an error.
return MaybeHandle<FixedArray>();
}

if (V8_UNLIKELY(!import_assertions_object->IsUndefined())) {
MessageLocation* location = nullptr;
MessageLocation computed_location;
if (ComputeLocation(&computed_location)) {
location = &computed_location;
}
Handle<JSMessageObject> message = MessageHandler::MakeMessageObject(
this, MessageTemplate::kImportAssertDeprecated, location,
factory()->NewStringFromAsciiChecked("a future version"),
Handle<FixedArray>::null());
message->set_error_level(v8::Isolate::kMessageWarning);
MessageHandler::ReportMessage(this, location, message);
}
}

// If there is no 'with' or 'assert' option in the options bag, it's not an
Expand Down
3 changes: 3 additions & 0 deletions deps/v8/src/parsing/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1365,6 +1365,9 @@ ImportAssertions* Parser::ParseImportAssertClause() {
!scanner()->HasLineTerminatorBeforeNext() &&
CheckContextualKeyword(ast_value_factory()->assert_string())) {
// 'assert' keyword consumed
info_->pending_error_handler()->ReportWarningAt(
position(), end_position(), MessageTemplate::kImportAssertDeprecated,
"a future version");
} else {
return import_assertions;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*%(basename)s:9: 'assert' is deprecated in import statements and support will be removed in a future version; use 'with' instead
undefined:0: Error: Invalid module type was asserted
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*%(basename)s:9: 'assert' is deprecated in import statements and support will be removed in a future version; use 'with' instead
undefined:1: SyntaxError: Unexpected token '/', "// Copyrig"... is not valid JSON
// Copyright 2021 the V8 project authors. All rights reserved.
^
SyntaxError: Unexpected token '/', "// Copyrig"... is not valid JSON
SyntaxError: Unexpected token '/', "// Copyrig"... is not valid JSON
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ bytecodes: [
/* 48 E> */ B(DefineKeyedOwnProperty), R(this), R(0), U8(0), U8(0),
/* 53 S> */ B(LdaImmutableCurrentContextSlot), U8(3),
/* 58 E> */ B(GetKeyedProperty), R(this), U8(2),
B(Wide), B(LdaSmi), I16(311),
B(Wide), B(LdaSmi), I16(312),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
Expand Down Expand Up @@ -115,7 +115,7 @@ bytecodes: [
/* 41 E> */ B(DefineKeyedOwnProperty), R(this), R(0), U8(0), U8(0),
/* 46 S> */ B(LdaImmutableCurrentContextSlot), U8(3),
/* 51 E> */ B(GetKeyedProperty), R(this), U8(2),
B(Wide), B(LdaSmi), I16(310),
B(Wide), B(LdaSmi), I16(311),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
Expand Down Expand Up @@ -149,7 +149,7 @@ bytecodes: [
B(Star2),
B(LdaImmutableCurrentContextSlot), U8(3),
/* 58 E> */ B(GetKeyedProperty), R(this), U8(2),
B(Wide), B(LdaSmi), I16(311),
B(Wide), B(LdaSmi), I16(312),
B(Star3),
B(LdaConstant), U8(0),
B(Star4),
Expand Down Expand Up @@ -181,7 +181,7 @@ bytecodes: [
/* 41 E> */ B(DefineKeyedOwnProperty), R(this), R(0), U8(0), U8(0),
/* 46 S> */ B(LdaImmutableCurrentContextSlot), U8(3),
/* 51 E> */ B(GetKeyedProperty), R(this), U8(2),
B(Wide), B(LdaSmi), I16(310),
B(Wide), B(LdaSmi), I16(311),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ bytecodes: [
B(Star2),
B(LdaImmutableCurrentContextSlot), U8(3),
/* 54 E> */ B(GetKeyedProperty), R(this), U8(2),
B(Wide), B(LdaSmi), I16(309),
B(Wide), B(LdaSmi), I16(310),
B(Star3),
B(LdaConstant), U8(0),
B(Star4),
Expand Down Expand Up @@ -91,7 +91,7 @@ bytecodes: [
/* 44 E> */ B(DefineKeyedOwnProperty), R(this), R(0), U8(0), U8(0),
/* 49 S> */ B(LdaImmutableCurrentContextSlot), U8(3),
/* 54 E> */ B(GetKeyedProperty), R(this), U8(2),
B(Wide), B(LdaSmi), I16(309),
B(Wide), B(LdaSmi), I16(311),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(1),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
Expand Down Expand Up @@ -61,13 +61,13 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
/* 61 E> */ B(CallRuntime), U16(Runtime::kNewTypeError), R(2), U8(2),
B(Throw),
B(Wide), B(LdaSmi), I16(309),
B(Wide), B(LdaSmi), I16(310),
B(Star2),
B(LdaConstant), U8(1),
B(Star3),
Expand Down Expand Up @@ -99,13 +99,13 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star1),
B(LdaConstant), U8(0),
B(Star2),
/* 61 E> */ B(CallRuntime), U16(Runtime::kNewTypeError), R(1), U8(2),
B(Throw),
B(Wide), B(LdaSmi), I16(309),
B(Wide), B(LdaSmi), I16(310),
B(Star1),
B(LdaConstant), U8(1),
B(Star2),
Expand Down Expand Up @@ -145,7 +145,7 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
Expand All @@ -167,7 +167,7 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star3),
B(LdaConstant), U8(0),
B(Star4),
Expand All @@ -182,7 +182,7 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
Expand Down Expand Up @@ -216,13 +216,13 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star1),
B(LdaConstant), U8(0),
B(Star2),
/* 65 E> */ B(CallRuntime), U16(Runtime::kNewTypeError), R(1), U8(2),
B(Throw),
B(Wide), B(LdaSmi), I16(311),
B(Wide), B(LdaSmi), I16(312),
B(Star1),
B(LdaConstant), U8(1),
B(Star2),
Expand Down Expand Up @@ -253,13 +253,13 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star1),
B(LdaConstant), U8(0),
B(Star2),
/* 58 E> */ B(CallRuntime), U16(Runtime::kNewTypeError), R(1), U8(2),
B(Throw),
B(Wide), B(LdaSmi), I16(310),
B(Wide), B(LdaSmi), I16(311),
B(Star1),
B(LdaConstant), U8(1),
B(Star2),
Expand Down Expand Up @@ -292,13 +292,13 @@ bytecodes: [
B(TestReferenceEqual), R(this),
B(Mov), R(this), R(0),
B(JumpIfTrue), U8(16),
B(Wide), B(LdaSmi), I16(303),
B(Wide), B(LdaSmi), I16(304),
B(Star2),
B(LdaConstant), U8(0),
B(Star3),
/* 65 E> */ B(CallRuntime), U16(Runtime::kNewTypeError), R(2), U8(2),
B(Throw),
B(Wide), B(LdaSmi), I16(311),
B(Wide), B(LdaSmi), I16(312),
B(Star2),
B(LdaConstant), U8(1),
B(Star3),
Expand Down Expand Up @@ -327,7 +327,7 @@ bytecode array length: 19
bytecodes: [
/* 46 S> */ B(LdaImmutableCurrentContextSlot), U8(3),
/* 51 E> */ B(GetKeyedProperty), R(this), U8(0),
B(Wide), B(LdaSmi), I16(310),
B(Wide), B(LdaSmi), I16(311),
B(Star1),
B(LdaConstant), U8(0),
B(Star2),
Expand Down
27 changes: 17 additions & 10 deletions doc/api/esm.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<!-- YAML
added: v8.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/55333

Check warning on line 11 in doc/api/esm.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: Import attributes are no longer experimental.
- version: v20.10.0
pr-url: https://github.com/nodejs/node/pull/50140
description: Add experimental support for import attributes.
Expand Down Expand Up @@ -262,13 +265,9 @@
description: Switch from Import Assertions to Import Attributes.
-->

> Stability: 1.1 - Active development

> This feature was previously named "Import assertions", and using the `assert`
> keyword instead of `with`. Any uses in code of the prior `assert` keyword
> should be updated to use `with` instead.
> Stability: 2 - Stable

The [Import Attributes proposal][] adds an inline syntax for module import
[Import attributes][Import Attributes MDN] are an inline syntax for module import
statements to pass on more information alongside the module specifier.

```js
Expand All @@ -278,13 +277,14 @@
await import('./bar.json', { with: { type: 'json' } });
```

Node.js supports the following `type` values, for which the attribute is
mandatory:
Node.js only supports the `type` attribute, for which it supports the following values:

| Attribute `type` | Needed for |
| ---------------- | ---------------- |
| `'json'` | [JSON modules][] |

The `type: 'json'` attribute is mandatory when importing JSON modules.

## Built-in modules

[Built-in modules][] provide named exports of their public API. A
Expand Down Expand Up @@ -591,7 +591,14 @@

## JSON modules

> Stability: 1 - Experimental
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/55333

Check warning on line 597 in doc/api/esm.md

View workflow job for this annotation

GitHub Actions / lint-pr-url

pr-url doesn't match the URL of the current PR.
description: JSON modules are no longer experimental.
-->

> Stability: 2 - Stable

JSON files can be referenced by `import`:

Expand Down Expand Up @@ -1129,7 +1136,7 @@
[Dynamic `import()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
[ES Module Integration Proposal for WebAssembly]: https://github.com/webassembly/esm-integration
[Import Attributes]: #import-attributes
[Import Attributes proposal]: https://github.com/tc39/proposal-import-attributes
[Import Attributes MDN]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import/with
[JSON modules]: #json-modules
[Loading ECMAScript modules using `require()`]: modules.md#loading-ecmascript-modules-using-require
[Module customization hooks]: module.md#customization-hooks
Expand Down
1 change: 0 additions & 1 deletion lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ translators.set('builtin', function builtinStrategy(url) {
// Strategy for loading a JSON file
const isWindows = process.platform === 'win32';
translators.set('json', function jsonStrategy(url, source) {
emitExperimentalWarning('Importing JSON modules');
assertBufferSource(source, true, 'load');
debug(`Loading JSONModule ${url}`);
const pathname = StringPrototypeStartsWith(url, 'file:') ?
Expand Down
4 changes: 2 additions & 2 deletions test/es-module/test-esm-json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ describe('ESM: importing JSON', () => {
assert.strictEqual(secret.ofLife, 42);
});

it('should print an experimental warning', async () => {
it('should not print an experimental warning', async () => {
const { code, signal, stderr } = await spawnPromisified(execPath, [
fixtures.path('/es-modules/json-modules.mjs'),
]);

assert.match(stderr, /ExperimentalWarning: Importing JSON modules/);
assert.strictEqual(stderr, '');
assert.strictEqual(code, 0);
assert.strictEqual(signal, null);
});
Expand Down
Loading