module: remove dead code

This removes a lot of code that has no functionality anymore. All
Node.js internal code calls `_resolveLookupPaths` with two arguments.

The code that validates `index.js` is not required at all as we check
for these files anyway, so it's just redundant code that should be
removed.

PR-URL: https://github.com/nodejs/node/pull/26983
Refs: https://github.com/nodejs/node/pull/25362
Reviewed-By: Jan Krems <jan.krems@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Ruben Bridgewater 2019-03-29 14:46:53 +01:00
parent a3f30a48c2
commit d11c4beb4b
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
4 changed files with 15 additions and 75 deletions

View File

@ -23,7 +23,7 @@ function makeRequireFunction(mod) {
function paths(request) { function paths(request) {
validateString(request, 'request'); validateString(request, 'request');
return Module._resolveLookupPaths(request, mod, true); return Module._resolveLookupPaths(request, mod);
} }
resolve.paths = paths; resolve.paths = paths;

View File

@ -65,16 +65,9 @@ let ModuleJob;
let createDynamicModule; let createDynamicModule;
const { const {
CHAR_UPPERCASE_A,
CHAR_LOWERCASE_A,
CHAR_UPPERCASE_Z,
CHAR_LOWERCASE_Z,
CHAR_FORWARD_SLASH, CHAR_FORWARD_SLASH,
CHAR_BACKWARD_SLASH, CHAR_BACKWARD_SLASH,
CHAR_COLON, CHAR_COLON
CHAR_UNDERSCORE,
CHAR_0,
CHAR_9,
} = require('internal/constants'); } = require('internal/constants');
const isWindows = process.platform === 'win32'; const isWindows = process.platform === 'win32';
@ -472,14 +465,10 @@ if (isWindows) {
}; };
} }
Module._resolveLookupPaths = function(request, parent) {
// 'index.' character codes
const indexChars = [ 105, 110, 100, 101, 120, 46 ];
const indexLen = indexChars.length;
Module._resolveLookupPaths = function(request, parent, newReturn) {
if (NativeModule.canBeRequiredByUsers(request)) { if (NativeModule.canBeRequiredByUsers(request)) {
debug('looking for %j in []', request); debug('looking for %j in []', request);
return (newReturn ? null : [request, []]); return null;
} }
// Check for node modules paths. // Check for node modules paths.
@ -495,71 +484,24 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
} }
debug('looking for %j in %j', request, paths); debug('looking for %j in %j', request, paths);
return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]); return paths.length > 0 ? paths : null;
} }
// With --eval, parent.id is not set and parent.filename is null. // With --eval, parent.id is not set and parent.filename is null.
if (!parent || !parent.id || !parent.filename) { if (!parent || !parent.id || !parent.filename) {
// Make require('./path/to/foo') work - normally the path is taken // Make require('./path/to/foo') work - normally the path is taken
// from realpath(__filename) but with eval there is no filename // from realpath(__filename) but with eval there is no filename
var mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths); const mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);
debug('looking for %j in %j', request, mainPaths); debug('looking for %j in %j', request, mainPaths);
return (newReturn ? mainPaths : [request, mainPaths]); return mainPaths;
} }
// Is the parent an index module? debug('RELATIVE: requested: %s from parent.id %s', request, parent.id);
// We can assume the parent has a valid extension,
// as it already has been accepted as a module.
const base = path.basename(parent.filename);
var parentIdPath;
if (base.length > indexLen) {
var i = 0;
for (; i < indexLen; ++i) {
if (indexChars[i] !== base.charCodeAt(i))
break;
}
if (i === indexLen) {
// We matched 'index.', let's validate the rest
for (; i < base.length; ++i) {
const code = base.charCodeAt(i);
if (code !== CHAR_UNDERSCORE &&
(code < CHAR_0 || code > CHAR_9) &&
(code < CHAR_UPPERCASE_A || code > CHAR_UPPERCASE_Z) &&
(code < CHAR_LOWERCASE_A || code > CHAR_LOWERCASE_Z))
break;
}
if (i === base.length) {
// Is an index module
parentIdPath = parent.id;
} else {
// Not an index module
parentIdPath = path.dirname(parent.id);
}
} else {
// Not an index module
parentIdPath = path.dirname(parent.id);
}
} else {
// Not an index module
parentIdPath = path.dirname(parent.id);
}
var id = path.resolve(parentIdPath, request);
// Make sure require('./path') and require('path') get distinct ids, even
// when called from the toplevel js file
if (parentIdPath === '.' &&
id.indexOf('/') === -1 &&
(!isWindows || id.indexOf('\\') === -1)) {
id = './' + id;
}
debug('RELATIVE: requested: %s set ID to: %s from %s', request, id,
parent.id);
const parentDir = [path.dirname(parent.filename)]; const parentDir = [path.dirname(parent.filename)];
debug('looking for %j in %j', id, parentDir); debug('looking for %j', parentDir);
return (newReturn ? parentDir : [id, parentDir]); return parentDir;
}; };
// Check the cache for the requested file. // Check the cache for the requested file.
@ -647,7 +589,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
for (var i = 0; i < options.paths.length; i++) { for (var i = 0; i < options.paths.length; i++) {
const path = options.paths[i]; const path = options.paths[i];
fakeParent.paths = Module._nodeModulePaths(path); fakeParent.paths = Module._nodeModulePaths(path);
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true); const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
for (var j = 0; j < lookupPaths.length; j++) { for (var j = 0; j < lookupPaths.length; j++) {
if (!paths.includes(lookupPaths[j])) if (!paths.includes(lookupPaths[j]))
@ -655,7 +597,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
} }
} }
} else { } else {
paths = Module._resolveLookupPaths(request, parent, true); paths = Module._resolveLookupPaths(request, parent);
} }
// Look up the filename first, since that's the cache key. // Look up the filename first, since that's the cache key.

View File

@ -857,8 +857,7 @@ REPLServer.prototype.createContext = function() {
} }
const module = new CJSModule('<repl>'); const module = new CJSModule('<repl>');
module.paths = module.paths = CJSModule._resolveLookupPaths('<repl>', parentModule) || [];
CJSModule._resolveLookupPaths('<repl>', parentModule, true) || [];
Object.defineProperty(context, 'module', { Object.defineProperty(context, 'module', {
configurable: true, configurable: true,

View File

@ -10,12 +10,11 @@ function testFirstInPath(moduleName, isLocalModule) {
assert.strictEqual : assert.strictEqual :
assert.notStrictEqual; assert.notStrictEqual;
const lookupResults = _module._resolveLookupPaths(moduleName); let paths = _module._resolveLookupPaths(moduleName);
let paths = lookupResults[1];
assertFunction(paths[0], '.'); assertFunction(paths[0], '.');
paths = _module._resolveLookupPaths(moduleName, null, true); paths = _module._resolveLookupPaths(moduleName, null);
assertFunction(paths && paths[0], '.'); assertFunction(paths && paths[0], '.');
} }