Browse Source

lib: simplify Module._resolveLookupPaths

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 <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
v6
Brian White 8 years ago
parent
commit
c67207731f
No known key found for this signature in database GPG Key ID: 606D7358F94DA209
  1. 29
      lib/module.js
  2. 2
      lib/repl.js
  3. 7
      test/parallel/test-module-relative-lookup.js

29
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';
}

2
lib/repl.js

@ -712,7 +712,7 @@ REPLServer.prototype.createContext = function() {
}
const module = new Module('<repl>');
module.paths = Module._resolveLookupPaths('<repl>', parentModule)[1];
module.paths = Module._resolveLookupPaths('<repl>', parentModule, true) || [];
const require = internalModule.makeRequireFunction(module);
context.module = module;

7
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');

Loading…
Cancel
Save