Skip to content

Commit 2c621d6

Browse files
aduh95targos
authored andcommitted
esm: add a runtime warning when using import assertions
PR-URL: #46901 Refs: #46830 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent e5b8896 commit 2c621d6

6 files changed

+50
-5
lines changed

lib/internal/modules/esm/assert.js

+13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const {
44
ArrayPrototypeFilter,
55
ArrayPrototypeIncludes,
6+
ObjectKeys,
67
ObjectValues,
78
ObjectPrototypeHasOwnProperty,
89
} = primordials;
@@ -17,6 +18,8 @@ const {
1718
// The HTML spec has an implied default type of `'javascript'`.
1819
const kImplicitAssertType = 'javascript';
1920

21+
let alreadyWarned = false;
22+
2023
/**
2124
* Define a map of module formats to import assertion types (the value of
2225
* `type` in `assert { type: 'json' }`).
@@ -55,6 +58,16 @@ function validateAssertions(url, format,
5558
importAssertions = { __proto__: null }) {
5659
const validType = formatTypeMap[format];
5760

61+
if (!alreadyWarned && ObjectKeys(importAssertions).length !== 0) {
62+
alreadyWarned = true;
63+
process.emitWarning(
64+
'Import assertions are not a stable feature of the JavaScript language, ' +
65+
'avoid relying on their current behavior and syntax as those might change ' +
66+
'in a future version of Node.js.',
67+
'ExperimentalWarning',
68+
);
69+
}
70+
5871
switch (validType) {
5972
case undefined:
6073
// Ignore assertions for module formats we don't recognize, to allow new

test/es-module/test-esm-import-assertion-errors.js

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ const { rejects } = require('assert');
55
const jsModuleDataUrl = 'data:text/javascript,export{}';
66
const jsonModuleDataUrl = 'data:application/json,""';
77

8+
common.expectWarning(
9+
'ExperimentalWarning',
10+
'Import assertions are not a stable feature of the JavaScript language, ' +
11+
'avoid relying on their current behavior and syntax as those might change ' +
12+
'in a future version of Node.js.'
13+
);
14+
815
async function test() {
916
await rejects(
1017
import('data:text/css,', { assert: { type: 'css' } }),

test/es-module/test-esm-import-assertion-errors.mjs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
1-
import '../common/index.mjs';
1+
import { expectWarning } from '../common/index.mjs';
22
import { rejects } from 'assert';
33

44
const jsModuleDataUrl = 'data:text/javascript,export{}';
55
const jsonModuleDataUrl = 'data:application/json,""';
66

7+
expectWarning(
8+
'ExperimentalWarning',
9+
'Import assertions are not a stable feature of the JavaScript language, ' +
10+
'avoid relying on their current behavior and syntax as those might change ' +
11+
'in a future version of Node.js.'
12+
);
13+
14+
715
await rejects(
816
// This rejects because of the unsupported MIME type, not because of the
917
// unsupported assertion.

test/es-module/test-esm-import-assertion-validation.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
// Flags: --expose-internals
22
'use strict';
3-
require('../common');
3+
const common = require('../common');
44

55
const assert = require('assert');
66

77
const { validateAssertions } = require('internal/modules/esm/assert');
88

9+
common.expectWarning(
10+
'ExperimentalWarning',
11+
'Import assertions are not a stable feature of the JavaScript language, ' +
12+
'avoid relying on their current behavior and syntax as those might change ' +
13+
'in a future version of Node.js.'
14+
);
15+
16+
917
const url = 'test://';
1018

1119
assert.ok(validateAssertions(url, 'builtin', {}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { expectWarning } from '../common/index.mjs';
2+
3+
expectWarning(
4+
'ExperimentalWarning',
5+
'Import assertions are not a stable feature of the JavaScript language, ' +
6+
'avoid relying on their current behavior and syntax as those might change ' +
7+
'in a future version of Node.js.'
8+
);
9+
10+
await import('data:text/javascript,', { assert: { someUnsupportedKey: 'value' } });

test/es-module/test-esm-json.mjs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { describe, it } from 'node:test';
66

77
import secret from '../fixtures/experimental.json' assert { type: 'json' };
88

9-
109
describe('ESM: importing JSON', () => {
1110
it('should load JSON', () => {
1211
assert.strictEqual(secret.ofLife, 42);
@@ -17,8 +16,8 @@ describe('ESM: importing JSON', () => {
1716
fixtures.path('/es-modules/json-modules.mjs'),
1817
]);
1918

20-
assert.match(stderr, /ExperimentalWarning/);
21-
assert.match(stderr, /JSON modules/);
19+
assert.match(stderr, /ExperimentalWarning: Importing JSON modules/);
20+
assert.match(stderr, /ExperimentalWarning: Import assertions/);
2221
assert.strictEqual(code, 0);
2322
assert.strictEqual(signal, null);
2423
});

0 commit comments

Comments
 (0)