Skip to content

Commit d11c4be

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. PR-URL: #26983 Refs: #25362 Reviewed-By: Jan Krems <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent a3f30a4 commit d11c4be

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
@@ -65,16 +65,9 @@ let ModuleJob;
6565
let createDynamicModule;
6666

6767
const {
68-
CHAR_UPPERCASE_A,
69-
CHAR_LOWERCASE_A,
70-
CHAR_UPPERCASE_Z,
71-
CHAR_LOWERCASE_Z,
7268
CHAR_FORWARD_SLASH,
7369
CHAR_BACKWARD_SLASH,
74-
CHAR_COLON,
75-
CHAR_UNDERSCORE,
76-
CHAR_0,
77-
CHAR_9,
70+
CHAR_COLON
7871
} = require('internal/constants');
7972

8073
const isWindows = process.platform === 'win32';
@@ -472,14 +465,10 @@ if (isWindows) {
472465
};
473466
}
474467

475-
476-
// 'index.' character codes
477-
const indexChars = [ 105, 110, 100, 101, 120, 46 ];
478-
const indexLen = indexChars.length;
479-
Module._resolveLookupPaths = function(request, parent, newReturn) {
468+
Module._resolveLookupPaths = function(request, parent) {
480469
if (NativeModule.canBeRequiredByUsers(request)) {
481470
debug('looking for %j in []', request);
482-
return (newReturn ? null : [request, []]);
471+
return null;
483472
}
484473

485474
// Check for node modules paths.
@@ -495,71 +484,24 @@ Module._resolveLookupPaths = function(request, parent, newReturn) {
495484
}
496485

497486
debug('looking for %j in %j', request, paths);
498-
return (newReturn ? (paths.length > 0 ? paths : null) : [request, paths]);
487+
return paths.length > 0 ? paths : null;
499488
}
500489

501490
// With --eval, parent.id is not set and parent.filename is null.
502491
if (!parent || !parent.id || !parent.filename) {
503492
// Make require('./path/to/foo') work - normally the path is taken
504493
// from realpath(__filename) but with eval there is no filename
505-
var mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);
494+
const mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);
506495

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

557-
debug('RELATIVE: requested: %s set ID to: %s from %s', request, id,
558-
parent.id);
500+
debug('RELATIVE: requested: %s from parent.id %s', request, parent.id);
559501

560502
const parentDir = [path.dirname(parent.filename)];
561-
debug('looking for %j in %j', id, parentDir);
562-
return (newReturn ? parentDir : [id, parentDir]);
503+
debug('looking for %j', parentDir);
504+
return parentDir;
563505
};
564506

565507
// Check the cache for the requested file.
@@ -647,15 +589,15 @@ Module._resolveFilename = function(request, parent, isMain, options) {
647589
for (var i = 0; i < options.paths.length; i++) {
648590
const path = options.paths[i];
649591
fakeParent.paths = Module._nodeModulePaths(path);
650-
const lookupPaths = Module._resolveLookupPaths(request, fakeParent, true);
592+
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
651593

652594
for (var j = 0; j < lookupPaths.length; j++) {
653595
if (!paths.includes(lookupPaths[j]))
654596
paths.push(lookupPaths[j]);
655597
}
656598
}
657599
} else {
658-
paths = Module._resolveLookupPaths(request, parent, true);
600+
paths = Module._resolveLookupPaths(request, parent);
659601
}
660602

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

lib/repl.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -857,8 +857,7 @@ REPLServer.prototype.createContext = function() {
857857
}
858858

859859
const module = new CJSModule('<repl>');
860-
module.paths =
861-
CJSModule._resolveLookupPaths('<repl>', parentModule, true) || [];
860+
module.paths = CJSModule._resolveLookupPaths('<repl>', parentModule) || [];
862861

863862
Object.defineProperty(context, 'module', {
864863
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)