Skip to content

Commit 6c4f477

Browse files
JakobJingleheimermarco-ippolito
authored andcommitted
module: tidy code and comments
PR-URL: #52437 Backport-PR-URL: #53500 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Feng Yu <[email protected]>
1 parent 3a2d8bf commit 6c4f477

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

doc/api/errors.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2533,7 +2533,7 @@ This is not allowed because ES Modules cannot be evaluated while they are
25332533
already being evaluated.
25342534

25352535
To avoid the cycle, the `require()` call involved in a cycle should not happen
2536-
at the top-level of either a ES Module (via `createRequire()`) or a CommonJS
2536+
at the top-level of either an ES Module (via `createRequire()`) or a CommonJS
25372537
module, and should be done lazily in an inner function.
25382538

25392539
<a id="ERR_REQUIRE_ASYNC_MODULE"></a>

lib/internal/modules/cjs/loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1063,7 +1063,7 @@ Module._load = function(request, parent, isMain) {
10631063
}
10641064
// If it's cached by the ESM loader as a way to indirectly pass
10651065
// the module in to avoid creating it twice, the loading request
1066-
// come from imported CJS. In that case use the kModuleCircularVisited
1066+
// came from imported CJS. In that case use the kModuleCircularVisited
10671067
// to determine if it's loading or not.
10681068
if (cachedModule[kModuleCircularVisited]) {
10691069
return getExportsForCircularRequire(cachedModule);

lib/internal/modules/esm/loader.js

+20-8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ const {
4242
} = require('internal/modules/helpers');
4343
let defaultResolve, defaultLoad, defaultLoadSync, importMetaInitializer;
4444

45+
/**
46+
* @typedef {import('url').URL} URL
47+
*/
48+
4549
/**
4650
* Lazy loads the module_map module and returns a new instance of ResolveCache.
4751
* @returns {import('./module_map.js').ResolveCache}
@@ -77,6 +81,10 @@ function getTranslators() {
7781
*/
7882
let hooksProxy;
7983

84+
/**
85+
* @typedef {import('../cjs/loader.js').Module} CJSModule
86+
*/
87+
8088
/**
8189
* @typedef {Record<string, any>} ModuleExports
8290
*/
@@ -257,11 +265,11 @@ class ModuleLoader {
257265
/**
258266
* This constructs (creates, instantiates and evaluates) a module graph that
259267
* is require()'d.
260-
* @param {import('../cjs/loader.js').Module} mod CJS module wrapper of the ESM.
268+
* @param {CJSModule} mod CJS module wrapper of the ESM.
261269
* @param {string} filename Resolved filename of the module being require()'d
262270
* @param {string} source Source code. TODO(joyeecheung): pass the raw buffer.
263271
* @param {string} isMain Whether this module is a main module.
264-
* @param {import('../cjs/loader.js').Module|undefined} parent Parent module, if any.
272+
* @param {CJSModule|undefined} parent Parent module, if any.
265273
* @returns {{ModuleWrap}}
266274
*/
267275
importSyncForRequire(mod, filename, source, isMain, parent) {
@@ -343,7 +351,7 @@ class ModuleLoader {
343351
}
344352
throw new ERR_REQUIRE_CYCLE_MODULE(message);
345353
}
346-
// Othersie the module could be imported before but the evaluation may be already
354+
// Otherwise the module could be imported before but the evaluation may be already
347355
// completed (e.g. the require call is lazy) so it's okay. We will return the
348356
// module now and check asynchronicity of the entire graph later, after the
349357
// graph is instantiated.
@@ -352,8 +360,12 @@ class ModuleLoader {
352360

353361
defaultLoadSync ??= require('internal/modules/esm/load').defaultLoadSync;
354362
const loadResult = defaultLoadSync(url, { format, importAttributes });
355-
const { responseURL, source } = loadResult;
356-
const { format: finalFormat } = loadResult;
363+
const {
364+
format: finalFormat,
365+
responseURL,
366+
source,
367+
} = loadResult;
368+
357369
this.validateLoadResult(url, finalFormat);
358370
if (finalFormat === 'wasm') {
359371
assert.fail('WASM is currently unsupported by require(esm)');
@@ -725,11 +737,11 @@ function getOrInitializeCascadedLoader() {
725737

726738
/**
727739
* Register a single loader programmatically.
728-
* @param {string|import('url').URL} specifier
729-
* @param {string|import('url').URL} [parentURL] Base to use when resolving `specifier`; optional if
740+
* @param {string|URL} specifier
741+
* @param {string|URL} [parentURL] Base to use when resolving `specifier`; optional if
730742
* `specifier` is absolute. Same as `options.parentUrl`, just inline
731743
* @param {object} [options] Additional options to apply, described below.
732-
* @param {string|import('url').URL} [options.parentURL] Base to use when resolving `specifier`
744+
* @param {string|URL} [options.parentURL] Base to use when resolving `specifier`
733745
* @param {any} [options.data] Arbitrary data passed to the loader's `initialize` hook
734746
* @param {any[]} [options.transferList] Objects in `data` that are changing ownership
735747
* @returns {void} We want to reserve the return value for potential future extension of the API.

lib/internal/modules/esm/translators.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const { dirname, extname, isAbsolute } = require('path');
4141
const {
4242
loadBuiltinModule,
4343
stripBOM,
44+
urlToFilename,
4445
} = require('internal/modules/helpers');
4546
const {
4647
kIsCachedByESMLoader,
@@ -243,7 +244,7 @@ function loadCJSModule(module, source, url, filename) {
243244
}
244245
}
245246
const { url: resolvedURL } = cascadedLoader.resolveSync(specifier, url, kEmptyObject);
246-
return StringPrototypeStartsWith(resolvedURL, 'file://') ? fileURLToPath(resolvedURL) : resolvedURL;
247+
return urlToFilename(resolvedURL);
247248
});
248249
setOwnProperty(requireFn, 'main', process.mainModule);
249250

@@ -265,7 +266,7 @@ const cjsCache = new SafeMap();
265266
function createCJSModuleWrap(url, source, isMain, loadCJS = loadCJSModule) {
266267
debug(`Translating CJSModule ${url}`);
267268

268-
const filename = StringPrototypeStartsWith(url, 'file://') ? fileURLToPath(url) : url;
269+
const filename = urlToFilename(url);
269270
// In case the source was not provided by the `load` step, we need fetch it now.
270271
source = stringify(source ?? getSource(new URL(url)).source);
271272

0 commit comments

Comments
 (0)