Skip to content

Commit 9903df5

Browse files
committed
module: use options bag for stat and tryFile probe flags
Address review feedback: pass the resolution-probe flag as a named option instead of a positional boolean, so callsites read tryFile(basePath + exts[i], { isMain, isSpeculativeProbe: true }); _stat(curPath, { isSpeculativeProbe: true }); `isMain` moves into the bag as well rather than staying positional, and `stat` gets the same treatment since that is where the flag is consumed. Both default to `kEmptyObject`, matching the existing idiom in this file. No behaviour change: the negative-caching scope is identical and the existing tests pass unmodified. Signed-off-by: Maxime David <maxday@amazon.com>
1 parent 7d9cb3b commit 9903df5

1 file changed

Lines changed: 13 additions & 11 deletions

File tree

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,13 @@ function wrapModuleLoad(request, parent, isMain, options) {
281281
* is missing on a failed `require()` and then created can still be picked up by
282282
* a later `require()` in the same tree.
283283
* @param {string} filename Absolute path to the file
284-
* @param {boolean} [isSpeculativeProbe] Whether this is a resolution guess
285-
* rather than a path the user named, which makes the negative result
284+
* @param {object} [options]
285+
* @param {boolean} [options.isSpeculativeProbe] Whether this is a resolution
286+
* guess rather than a path the user named, which makes the negative result
286287
* cacheable and lets a previously cached negative be reused.
287288
* @returns {number}
288289
*/
289-
function stat(filename, isSpeculativeProbe = false) {
290+
function stat(filename, { isSpeculativeProbe = false } = kEmptyObject) {
290291
// Guard against internal bugs where a non-string filename is passed in by mistake.
291292
assert(typeof filename === 'string');
292293

@@ -573,7 +574,7 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
573574
}
574575

575576
const filename = path.resolve(requestPath, pkg);
576-
let actual = tryFile(filename, isMain) ||
577+
let actual = tryFile(filename, { isMain }) ||
577578
tryExtensions(filename, exts, isMain) ||
578579
tryExtensions(path.resolve(filename, 'index'), exts, isMain);
579580
if (actual === false) {
@@ -605,13 +606,14 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
605606
* Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false or
606607
* `--preserve-symlinks-main` and `isMain` is true , keep symlinks intact, otherwise resolve to the absolute realpath.
607608
* @param {string} requestPath The path to the file to load.
608-
* @param {boolean} isMain Whether the file is the main module.
609-
* @param {boolean} [isSpeculativeProbe] Whether `requestPath` is a resolution
610-
* guess rather than a path the user named. See {@link stat}.
609+
* @param {object} [options]
610+
* @param {boolean} [options.isMain] Whether the file is the main module.
611+
* @param {boolean} [options.isSpeculativeProbe] Whether `requestPath` is a
612+
* resolution guess rather than a path the user named. See {@link stat}.
611613
* @returns {string|undefined}
612614
*/
613-
function tryFile(requestPath, isMain, isSpeculativeProbe = false) {
614-
const rc = _stat(requestPath, isSpeculativeProbe);
615+
function tryFile(requestPath, { isMain = false, isSpeculativeProbe = false } = kEmptyObject) {
616+
const rc = _stat(requestPath, { isSpeculativeProbe });
615617
if (rc !== 0) { return; }
616618
if (getOptionValue(isMain ? '--preserve-symlinks-main' : '--preserve-symlinks')) {
617619
return path.resolve(requestPath);
@@ -630,7 +632,7 @@ function tryFile(requestPath, isMain, isSpeculativeProbe = false) {
630632
*/
631633
function tryExtensions(basePath, exts, isMain) {
632634
for (let i = 0; i < exts.length; i++) {
633-
const filename = tryFile(basePath + exts[i], isMain, true);
635+
const filename = tryFile(basePath + exts[i], { isMain, isSpeculativeProbe: true });
634636

635637
if (filename) {
636638
return filename;
@@ -844,7 +846,7 @@ Module._findPath = function(request, paths, isMain, conditions = getCjsCondition
844846
// A candidate lookup directory (typically a `node_modules` ancestor) is a
845847
// speculative probe: most of the chain does not exist, and every bare
846848
// specifier in the tree re-walks the same missing ancestors.
847-
if (insidePath && curPath && _stat(curPath, true) < 1) {
849+
if (insidePath && curPath && _stat(curPath, { isSpeculativeProbe: true }) < 1) {
848850
continue;
849851
}
850852

0 commit comments

Comments
 (0)