From c67207731f16a78f6cae90e49c53b10728241ecf Mon Sep 17 00:00:00 2001 From: Brian White Date: Fri, 13 Jan 2017 05:22:27 -0500 Subject: [PATCH] lib: simplify Module._resolveLookupPaths MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit consists of two changes: * Avoids returning request/id *just* for the debug() output * Returns `null` instead of an empty array for the list of paths PR-URL: https://github.com/nodejs/node/pull/10789 Reviewed-By: James M Snell Reviewed-By: Michaƫl Zasso Reviewed-By: Michael Dawson Reviewed-By: Matteo Collina Reviewed-By: Benjamin Gruenbaum --- lib/module.js | 29 ++++++++++---------- lib/repl.js | 2 +- test/parallel/test-module-relative-lookup.js | 7 ++++- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/lib/module.js b/lib/module.js index 77675fcd980421..115afc7553ad82 100644 --- a/lib/module.js +++ b/lib/module.js @@ -326,14 +326,14 @@ if (process.platform === 'win32') { // 'index.' character codes var indexChars = [ 105, 110, 100, 101, 120, 46 ]; var indexLen = indexChars.length; -Module._resolveLookupPaths = function(request, parent) { +Module._resolveLookupPaths = function(request, parent, newReturn) { if (NativeModule.nonInternalExists(request)) { - return [request, []]; + debug('looking for %j in []', request); + return (newReturn ? null : [request, []]); } - var reqLen = request.length; // Check for relative path - if (reqLen < 2 || + if (request.length < 2 || request.charCodeAt(0) !== 46/*.*/ || (request.charCodeAt(1) !== 46/*.*/ && request.charCodeAt(1) !== 47/*/*/)) { @@ -355,7 +355,8 @@ Module._resolveLookupPaths = function(request, parent) { } } - return [request, paths]; + debug('looking for %j in %j', request, paths); + return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]); } // with --eval, parent.id is not set and parent.filename is null @@ -363,7 +364,9 @@ Module._resolveLookupPaths = function(request, parent) { // make require('./path/to/foo') work - normally the path is taken // from realpath(__filename) but with eval there is no filename var mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths); - return [request, mainPaths]; + + debug('looking for %j in %j', request, mainPaths); + return (newReturn ? mainPaths : [request, mainPaths]); } // Is the parent an index module? @@ -413,7 +416,9 @@ Module._resolveLookupPaths = function(request, parent) { debug('RELATIVE: requested: %s set ID to: %s from %s', request, id, parent.id); - return [id, [path.dirname(parent.filename)]]; + var parentDir = [path.dirname(parent.filename)]; + debug('looking for %j in %j', id, parentDir); + return (newReturn ? parentDir : [id, parentDir]); }; @@ -472,16 +477,12 @@ Module._resolveFilename = function(request, parent, isMain) { return request; } - var resolvedModule = Module._resolveLookupPaths(request, parent); - var id = resolvedModule[0]; - var paths = resolvedModule[1]; + var paths = Module._resolveLookupPaths(request, parent, true); // look up the filename first, since that's the cache key. - debug('looking for %j in %j', id, paths); - var filename = Module._findPath(request, paths, isMain); if (!filename) { - var err = new Error("Cannot find module '" + request + "'"); + var err = new Error(`Cannot find module '${request}'`); err.code = 'MODULE_NOT_FOUND'; throw err; } @@ -564,7 +565,7 @@ Module.prototype._compile = function(content, filename) { if (!resolvedArgv) { // we enter the repl if we're not given a filename argument. if (process.argv[1]) { - resolvedArgv = Module._resolveFilename(process.argv[1], null); + resolvedArgv = Module._resolveFilename(process.argv[1], null, false); } else { resolvedArgv = 'repl'; } diff --git a/lib/repl.js b/lib/repl.js index 676fa105861d33..b00666267a646b 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -712,7 +712,7 @@ REPLServer.prototype.createContext = function() { } const module = new Module(''); - module.paths = Module._resolveLookupPaths('', parentModule)[1]; + module.paths = Module._resolveLookupPaths('', parentModule, true) || []; const require = internalModule.makeRequireFunction(module); context.module = module; diff --git a/test/parallel/test-module-relative-lookup.js b/test/parallel/test-module-relative-lookup.js index 2ecb412ce22782..6ebb8ee1881962 100644 --- a/test/parallel/test-module-relative-lookup.js +++ b/test/parallel/test-module-relative-lookup.js @@ -4,7 +4,12 @@ require('../common'); const assert = require('assert'); const _module = require('module'); // avoid collision with global.module const lookupResults = _module._resolveLookupPaths('./lodash'); -const paths = lookupResults[1]; +let paths = lookupResults[1]; assert.strictEqual(paths[0], '.', 'Current directory gets highest priority for local modules'); + +paths = _module._resolveLookupPaths('./lodash', null, true); + +assert.strictEqual(paths && paths[0], '.', + 'Current directory gets highest priority for local modules');