Skip to content

Commit

Permalink
esm: add a runtime warning when using import assertions
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
aduh95 authored and danielleadams committed Apr 11, 2023
1 parent 512f28f commit 96a39d1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
13 changes: 13 additions & 0 deletions lib/internal/modules/esm/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
ArrayPrototypeFilter,
ArrayPrototypeIncludes,
ObjectCreate,
ObjectKeys,
ObjectValues,
ObjectPrototypeHasOwnProperty,
} = primordials;
Expand All @@ -18,6 +19,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 @@ -56,6 +59,16 @@ function validateAssertions(url, format,
importAssertions = ObjectCreate(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 ' +
'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

0 comments on commit 96a39d1

Please sign in to comment.