Permalink
Browse files

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: #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>
  • Loading branch information...
mscdex committed Jan 13, 2017
1 parent e32425b commit c67207731f16a78f6cae90e49c53b10728241ecf
Showing with 22 additions and 16 deletions.
  1. +15 −14 lib/module.js
  2. +1 −1 lib/repl.js
  3. +6 −1 test/parallel/test-module-relative-lookup.js
View
@@ -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,15 +355,18 @@ 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
if (!parent || !parent.id || !parent.filename) {
// 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';
}
View
@@ -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;
@@ -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');

0 comments on commit c672077

Please sign in to comment.