Skip to content

Commit 4d39a40

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 13c9744 commit 4d39a40

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

@@ -576,7 +577,7 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
576577
}
577578

578579
const filename = path.resolve(requestPath, pkg);
579-
let actual = tryFile(filename, isMain) ||
580+
let actual = tryFile(filename, { isMain }) ||
580581
tryExtensions(filename, exts, isMain) ||
581582
tryExtensions(path.resolve(filename, 'index'), exts, isMain);
582583
if (actual === false) {
@@ -608,13 +609,14 @@ function tryPackage(requestPath, exts, isMain, originalPath) {
608609
* Check if the file exists and is not a directory if using `--preserve-symlinks` and `isMain` is false or
609610
* `--preserve-symlinks-main` and `isMain` is true , keep symlinks intact, otherwise resolve to the absolute realpath.
610611
* @param {string} requestPath The path to the file to load.
611-
* @param {boolean} isMain Whether the file is the main module.
612-
* @param {boolean} [isSpeculativeProbe] Whether `requestPath` is a resolution
613-
* guess rather than a path the user named. See {@link stat}.
612+
* @param {object} [options]
613+
* @param {boolean} [options.isMain] Whether the file is the main module.
614+
* @param {boolean} [options.isSpeculativeProbe] Whether `requestPath` is a
615+
* resolution guess rather than a path the user named. See {@link stat}.
614616
* @returns {string|undefined}
615617
*/
616-
function tryFile(requestPath, isMain, isSpeculativeProbe = false) {
617-
const rc = _stat(requestPath, isSpeculativeProbe);
618+
function tryFile(requestPath, { isMain = false, isSpeculativeProbe = false } = kEmptyObject) {
619+
const rc = _stat(requestPath, { isSpeculativeProbe });
618620
if (rc !== 0) { return; }
619621
if (getOptionValue(isMain ? '--preserve-symlinks-main' : '--preserve-symlinks')) {
620622
return path.resolve(requestPath);
@@ -633,7 +635,7 @@ function tryFile(requestPath, isMain, isSpeculativeProbe = false) {
633635
*/
634636
function tryExtensions(basePath, exts, isMain) {
635637
for (let i = 0; i < exts.length; i++) {
636-
const filename = tryFile(basePath + exts[i], isMain, true);
638+
const filename = tryFile(basePath + exts[i], { isMain, isSpeculativeProbe: true });
637639

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

0 commit comments

Comments
 (0)