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 <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Mohammed Keyvanzadeh <[email protected]>
Reviewed-By: Jacob Smith <[email protected]>
  • Loading branch information
aduh95 authored and targos committed Mar 14, 2023
1 parent e5b8896 commit 2c621d6
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 @@ -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 ' +
'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 2c621d6

Please sign in to comment.