Skip to content

Commit 1abfc93

Browse files
committed
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.
1 parent dd0d6df commit 1abfc93

File tree

4 files changed

+15
-75
lines changed

4 files changed

+15
-75
lines changed

lib/internal/modules/cjs/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function makeRequireFunction(mod) {
2323

2424
function paths(request) {
2525
validateString(request, 'request');
26-
return Module._resolveLookupPaths(request, mod, true);
26+
return Module._resolveLookupPaths(request, mod);
2727
}
2828

2929
resolve.paths = paths;

lib/internal/modules/cjs/loader.js

+11-69
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,9 @@ let ModuleJob;
6363
let createDynamicModule;
6464

6565
const {
66-
CHAR_UPPERCASE_A,
67-
CHAR_LOWERCASE_A,
68-
CHAR_UPPERCASE_Z,
69-
CHAR_LOWERCASE_Z,
7066
CHAR_FORWARD_SLASH,
7167
CHAR_BACKWARD_SLASH,
72-
CHAR_COLON,
73-
CHAR_UNDERSCORE,
74-
CHAR_0,
75-
CHAR_9,
68+
CHAR_COLON
7669
} = require('internal/constants');
7770

7871
const isWindows = process.platform === 'win32';
@@ -466,14 +459,10 @@ if (isWindows) {
466459
};
467460
}
468461

469-
470-
// 'index.' character codes
471-
const indexChars = [ 105, 110, 100, 101, 120, 46 ];
472-
const indexLen = indexChars.length;
473-
Module._resolveLookupPaths = function(request, parent, newReturn) {
462+
Module._resolveLookupPaths = function(request, parent) {
474463
if (NativeModule.canBeRequiredByUsers(request)) {
475464
debug('looking for %j in []', request);
476-
return (newReturn ? null : [request, []]);
465+
return null;
477466
}
478467

479468
// Check for node modules paths.
@@ -489,71 +478,24 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
489478
}
490479

491480
debug('looking for %j in %j', request, paths);
492-
return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]);
481+
return paths.length > 0 ? paths : null;
493482
}
494483

495484
// With --eval, parent.id is not set and parent.filename is null.
496485
if (!parent || !parent.id || !parent.filename) {
497486
// Make require('./path/to/foo') work - normally the path is taken
498487
// from realpath(__filename) but with eval there is no filename
499-
var mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);
488+
const mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);
500489

501490
debug('looking for %j in %j', request, mainPaths);
502-
return (newReturn ? mainPaths : [request, mainPaths]);
503-
}
504-
505-
// Is the parent an index module?
506-
// We can assume the parent has a valid extension,
507-
// as it already has been accepted as a module.
508-
const base = path.basename(parent.filename);
509-
var parentIdPath;
510-
if (base.length > indexLen) {
511-
var i = 0;
512-
for (; i < indexLen; ++i) {
513-
if (indexChars[i] !== base.charCodeAt(i))
514-
break;
515-
}
516-
if (i === indexLen) {
517-
// We matched 'index.', let's validate the rest
518-
for (; i < base.length; ++i) {
519-
const code = base.charCodeAt(i);
520-
if (code !== CHAR_UNDERSCORE &&
521-
(code < CHAR_0 || code > CHAR_9) &&
522-
(code < CHAR_UPPERCASE_A || code > CHAR_UPPERCASE_Z) &&
523-
(code < CHAR_LOWERCASE_A || code > CHAR_LOWERCASE_Z))
524-
break;
525-
}
526-
if (i === base.length) {
527-
// Is an index module
528-
parentIdPath = parent.id;
529-
} else {
530-
// Not an index module
531-
parentIdPath = path.dirname(parent.id);
532-
}
533-
} else {
534-
// Not an index module
535-
parentIdPath = path.dirname(parent.id);
536-
}
537-
} else {
538-
// Not an index module
539-
parentIdPath = path.dirname(parent.id);
540-
}
541-
var id = path.resolve(parentIdPath, request);
542-
543-
// Make sure require('./path') and require('path') get distinct ids, even
544-
// when called from the toplevel js file
545-
if (parentIdPath === '.' &&
546-
id.indexOf('/') === -1 &&
547-
(!isWindows || id.indexOf('\\') === -1)) {
548-
id = './' + id;
491+
return mainPaths;
549492
}
550493

551-
debug('RELATIVE: requested: %s set ID to: %s from %s', request, id,
552-
parent.id);
494+
debug('RELATIVE: requested: %s from parent.id %s', request, parent.id);
553495

554496
const parentDir = [path.dirname(parent.filename)];
555-
debug('looking for %j in %j', id, parentDir);
556-
return (newReturn ? parentDir : [id, parentDir]);
497+
debug('looking for %j', parentDir);
498+
return parentDir;
557499
};
558500

559501
// Check the cache for the requested file.
@@ -625,15 +567,15 @@ Module._resolveFilename = function(request, parent, isMain, options) {
625567
for (var i = 0; i < options.paths.length; i++) {
626568
const path = options.paths[i];
627569
fakeParent.paths = Module._nodeModulePaths(path);
628-
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true);
570+
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
629571

630572
for (var j = 0; j < lookupPaths.length; j++) {
631573
if (!paths.includes(lookupPaths[j]))
632574
paths.push(lookupPaths[j]);
633575
}
634576
}
635577
} else {
636-
paths = Module._resolveLookupPaths(request, parent, true);
578+
paths = Module._resolveLookupPaths(request, parent);
637579
}
638580

639581
// Look up the filename first, since that's the cache key.

lib/repl.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,7 @@ REPLServer.prototype.createContext = function() {
855855
}
856856

857857
const module = new CJSModule('<repl>');
858-
module.paths =
859-
CJSModule._resolveLookupPaths('<repl>', parentModule, true) || [];
858+
module.paths = CJSModule._resolveLookupPaths('<repl>', parentModule) || [];
860859

861860
Object.defineProperty(context, 'module', {
862861
configurable: true,

test/parallel/test-module-relative-lookup.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ function testFirstInPath(moduleName, isLocalModule) {
1010
assert.strictEqual :
1111
assert.notStrictEqual;
1212

13-
const lookupResults = _module._resolveLookupPaths(moduleName);
13+
let paths = _module._resolveLookupPaths(moduleName);
1414

15-
let paths = lookupResults[1];
1615
assertFunction(paths[0], '.');
1716

18-
paths = _module._resolveLookupPaths(moduleName, null, true);
17+
paths = _module._resolveLookupPaths(moduleName, null);
1918
assertFunction(paths && paths[0], '.');
2019
}
2120

0 commit comments

Comments
 (0)