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

esm: add a runtime warning when using import assertions #46901

Merged
Merged
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
13 changes: 13 additions & 0 deletions lib/internal/modules/esm/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ObjectKeys,
ObjectValues,
ObjectPrototypeHasOwnProperty,
} = primordials;
Expand All @@ -17,6 +18,8 @@ const {
// The HTML spec has an implied default type of `'javascript'`.
const kImplicitAssertType = 'javascript';

let alreadyWarned = false;

/**
* Define a map of module formats to import assertion types (the value of
* `type` in `assert { type: 'json' }`).
Expand Down Expand Up @@ -55,6 +58,16 @@ function validateAssertions(url, format,
importAssertions = { __proto__: null }) {
const validType = formatTypeMap[format];

if (!alreadyWarned && ObjectKeys(importAssertions).length !== 0) {
alreadyWarned = true;
process.emitWarning(
'Import assertions are not a stable feature of the JavaScript language, ' +
'avoid relying on their current behavior and syntax as those might change ' +
Comment on lines +64 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comma splice.

Suggested change
'Import assertions are not a stable feature of the JavaScript language, ' +
'avoid relying on their current behavior and syntax as those might change ' +
'Import assertions are not a stable feature of the JavaScript language. ' +
'Avoid relying on their current behavior and syntax as those might change ' +

'in a future version of Node.js.',
'ExperimentalWarning',
);
}

switch (validType) {
case undefined:
// Ignore assertions for module formats we don't recognize, to allow new
Expand Down
7 changes: 7 additions & 0 deletions test/es-module/test-esm-import-assertion-errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const { rejects } = require('assert');
const jsModuleDataUrl = 'data:text/javascript,export{}';
const jsonModuleDataUrl = 'data:application/json,""';

common.expectWarning(
'ExperimentalWarning',
'Import assertions are not a stable feature of the JavaScript language, ' +
'avoid relying on their current behavior and syntax as those might change ' +
'in a future version of Node.js.'
);

async function test() {
await rejects(
import('data:text/css,', { assert: { type: 'css' } }),
Expand Down
10 changes: 9 additions & 1 deletion test/es-module/test-esm-import-assertion-errors.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import '../common/index.mjs';
import { expectWarning } from '../common/index.mjs';
import { rejects } from 'assert';

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

expectWarning(
'ExperimentalWarning',
'Import assertions are not a stable feature of the JavaScript language, ' +
'avoid relying on their current behavior and syntax as those might change ' +
'in a future version of Node.js.'
);


await rejects(
// This rejects because of the unsupported MIME type, not because of the
// unsupported assertion.
Expand Down
10 changes: 9 additions & 1 deletion test/es-module/test-esm-import-assertion-validation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
// Flags: --expose-internals
'use strict';
require('../common');
const common = require('../common');

const assert = require('assert');

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

common.expectWarning(
'ExperimentalWarning',
'Import assertions are not a stable feature of the JavaScript language, ' +
'avoid relying on their current behavior and syntax as those might change ' +
'in a future version of Node.js.'
);


const url = 'test://';

assert.ok(validateAssertions(url, 'builtin', {}));
Expand Down
10 changes: 10 additions & 0 deletions test/es-module/test-esm-import-assertion-warning.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expectWarning } from '../common/index.mjs';

expectWarning(
'ExperimentalWarning',
'Import assertions are not a stable feature of the JavaScript language, ' +
'avoid relying on their current behavior and syntax as those might change ' +
'in a future version of Node.js.'
);

await import('data:text/javascript,', { assert: { someUnsupportedKey: 'value' } });
5 changes: 2 additions & 3 deletions test/es-module/test-esm-json.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { describe, it } from 'node:test';

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


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

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