Skip to content

Commit 96a39d1

Browse files
aduh95danielleadams
authored andcommitted
esm: add a runtime warning when using import assertions
PR-URL: #46901 Refs: #46830 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Mohammed Keyvanzadeh <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 512f28f commit 96a39d1

6 files changed

+50
-5
lines changed

lib/internal/modules/esm/assert.js

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

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

62+
if (!alreadyWarned && ObjectKeys(importAssertions).length !== 0) {
63+
alreadyWarned = true;
64+
process.emitWarning(
65+
'Import assertions are not a stable feature of the JavaScript language, ' +
66+
'avoid relying on their current behavior and syntax as those might change ' +
67+
'in a future version of Node.js.',
68+
'ExperimentalWarning',
69+
);
70+
}
71+
5972
switch (validType) {
6073
case undefined:
6174
// 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)