Skip to content

Commit

Permalink
lib,doc: replace references to import assertions
Browse files Browse the repository at this point in the history
Use "import attributes" and "type attribute" instead.

PR-URL: #52998
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
  • Loading branch information
targos committed May 17, 2024
1 parent 075853e commit fac55e3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion doc/api/module.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ changes:
The `load` hook provides a way to define a custom method of determining how a
URL should be interpreted, retrieved, and parsed. It is also in charge of
validating the import assertion.
validating the import attributes.
The final value of `format` must be one of the following:
Expand Down
2 changes: 1 addition & 1 deletion doc/api/packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ There is the ECMAScript module loader:
`'./startup/index.js'`) must be fully specified.
* It does no extension searching. A file extension must be provided
when the specifier is a relative or absolute file URL.
* It can load JSON modules, but an import assertion is required.
* It can load JSON modules, but an import type attribute is required.
* It accepts only `.js`, `.mjs`, and `.cjs` extensions for JavaScript text
files.
* It can be used to load JavaScript CommonJS modules. Such modules
Expand Down
30 changes: 15 additions & 15 deletions lib/internal/modules/esm/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {
} = require('internal/errors').codes;

// The HTML spec has an implied default type of `'javascript'`.
const kImplicitAssertType = 'javascript';
const kImplicitTypeAttribute = 'javascript';

/**
* Define a map of module formats to import attributes types (the value of
Expand All @@ -25,11 +25,11 @@ const kImplicitAssertType = 'javascript';
*/
const formatTypeMap = {
'__proto__': null,
'builtin': kImplicitAssertType,
'commonjs': kImplicitAssertType,
'builtin': kImplicitTypeAttribute,
'commonjs': kImplicitTypeAttribute,
'json': 'json',
'module': kImplicitAssertType,
'wasm': kImplicitAssertType, // It's unclear whether the HTML spec will require an attribute type or not for Wasm; see https://github.com/WebAssembly/esm-integration/issues/42
'module': kImplicitTypeAttribute,
'wasm': kImplicitTypeAttribute, // It's unclear whether the HTML spec will require an type attribute or not for Wasm; see https://github.com/WebAssembly/esm-integration/issues/42
};

/**
Expand All @@ -38,9 +38,9 @@ const formatTypeMap = {
* `import './file.js' with { type: 'javascript' }` throws.
* @type {Array<string, string>}
*/
const supportedAssertionTypes = ArrayPrototypeFilter(
const supportedTypeAttributes = ArrayPrototypeFilter(
ObjectValues(formatTypeMap),
(type) => type !== kImplicitAssertType);
(type) => type !== kImplicitTypeAttribute);


/**
Expand All @@ -50,7 +50,7 @@ const supportedAssertionTypes = ArrayPrototypeFilter(
* @param {Record<string, string>} importAttributes Validations for the
* module import.
* @returns {true}
* @throws {TypeError} If the format and assertion type are incompatible.
* @throws {TypeError} If the format and type attribute are incompatible.
*/
function validateAttributes(url, format,
importAttributes = { __proto__: null }) {
Expand All @@ -68,16 +68,16 @@ function validateAttributes(url, format,
// formats in the future.
return true;

case kImplicitAssertType:
// This format doesn't allow an import assertion type, so the property
case kImplicitTypeAttribute:
// This format doesn't allow an import type attribute, so the property
// must not be set on the import attributes object.
if (!ObjectPrototypeHasOwnProperty(importAttributes, 'type')) {
return true;
}
return handleInvalidType(url, importAttributes.type);

case importAttributes.type:
// The asserted type is the valid type for this format.
// The type attribute is the valid type for this format.
return true;

default:
Expand All @@ -92,16 +92,16 @@ function validateAttributes(url, format,
}

/**
* Throw the correct error depending on what's wrong with the type assertion.
* Throw the correct error depending on what's wrong with the type attribute.
* @param {string} url The resolved URL for the module to be imported
* @param {string} type The value of the import assertion `type` property
* @param {string} type The value of the import attributes' `type` property
*/
function handleInvalidType(url, type) {
// `type` might have not been a string.
validateString(type, 'type');

// `type` might not have been one of the types we understand.
if (!ArrayPrototypeIncludes(supportedAssertionTypes, type)) {
if (!ArrayPrototypeIncludes(supportedTypeAttributes, type)) {
throw new ERR_IMPORT_ATTRIBUTE_UNSUPPORTED('type', type);
}

Expand All @@ -111,6 +111,6 @@ function handleInvalidType(url, type) {


module.exports = {
kImplicitAssertType,
kImplicitTypeAttribute,
validateAttributes,
};
6 changes: 3 additions & 3 deletions lib/internal/modules/esm/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const {
compileSourceTextModule,
getDefaultConditions,
} = require('internal/modules/esm/utils');
const { kImplicitAssertType } = require('internal/modules/esm/assert');
const { kImplicitTypeAttribute } = require('internal/modules/esm/assert');
const { canParse } = internalBinding('url');
const { ModuleWrap, kEvaluating, kEvaluated } = internalBinding('module_wrap');
const {
Expand Down Expand Up @@ -276,7 +276,7 @@ class ModuleLoader {
*/
importSyncForRequire(mod, filename, source, isMain, parent) {
const url = pathToFileURL(filename).href;
let job = this.loadCache.get(url, kImplicitAssertType);
let job = this.loadCache.get(url, kImplicitTypeAttribute);
// This module job is already created:
// 1. If it was loaded by `require()` before, at this point the instantiation
// is already completed and we can check the whether it is in a cycle
Expand Down Expand Up @@ -307,7 +307,7 @@ class ModuleLoader {

const { ModuleJobSync } = require('internal/modules/esm/module_job');
job = new ModuleJobSync(this, url, kEmptyObject, wrap, isMain, inspectBrk);
this.loadCache.set(url, kImplicitAssertType, job);
this.loadCache.set(url, kImplicitTypeAttribute, job);
mod[kRequiredModuleSymbol] = job.module;
return job.runSync().namespace;
}
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/modules/esm/module_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
ObjectKeys,
SafeMap,
} = primordials;
const { kImplicitAssertType } = require('internal/modules/esm/assert');
const { kImplicitTypeAttribute } = require('internal/modules/esm/assert');
let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
debug = fn;
});
Expand Down Expand Up @@ -88,12 +88,12 @@ class ResolveCache extends SafeMap {
*/
class LoadCache extends SafeMap {
constructor(i) { super(i); } // eslint-disable-line no-useless-constructor
get(url, type = kImplicitAssertType) {
get(url, type = kImplicitTypeAttribute) {
validateString(url, 'url');
validateString(type, 'type');
return super.get(url)?.[type];
}
set(url, type = kImplicitAssertType, job) {
set(url, type = kImplicitTypeAttribute, job) {
validateString(url, 'url');
validateString(type, 'type');

Expand All @@ -103,18 +103,18 @@ class LoadCache extends SafeMap {
throw new ERR_INVALID_ARG_TYPE('job', 'ModuleJob', job);
}
debug(`Storing ${url} (${
type === kImplicitAssertType ? 'implicit type' : type
type === kImplicitTypeAttribute ? 'implicit type' : type
}) in ModuleLoadMap`);
const cachedJobsForUrl = super.get(url) ?? { __proto__: null };
cachedJobsForUrl[type] = job;
return super.set(url, cachedJobsForUrl);
}
has(url, type = kImplicitAssertType) {
has(url, type = kImplicitTypeAttribute) {
validateString(url, 'url');
validateString(type, 'type');
return super.get(url)?.[type] !== undefined;
}
delete(url, type = kImplicitAssertType) {
delete(url, type = kImplicitTypeAttribute) {
const cached = super.get(url);
if (cached) {
cached[type] = undefined;
Expand Down

0 comments on commit fac55e3

Please sign in to comment.