Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module: remove dead code #26983

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function makeRequireFunction(mod) {

function paths(request) {
validateString(request, 'request');
return Module._resolveLookupPaths(request, mod, true);
return Module._resolveLookupPaths(request, mod);
}

resolve.paths = paths;
Expand Down
80 changes: 11 additions & 69 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,9 @@ let ModuleJob;
let createDynamicModule;

const {
CHAR_UPPERCASE_A,
CHAR_LOWERCASE_A,
CHAR_UPPERCASE_Z,
CHAR_LOWERCASE_Z,
CHAR_FORWARD_SLASH,
CHAR_BACKWARD_SLASH,
CHAR_COLON,
CHAR_UNDERSCORE,
CHAR_0,
CHAR_9,
CHAR_COLON
} = require('internal/constants');

const isWindows = process.platform === 'win32';
Expand Down Expand Up @@ -466,14 +459,10 @@ if (isWindows) {
};
}


// 'index.' character codes
const indexChars = [ 105, 110, 100, 101, 120, 46 ];
const indexLen = indexChars.length;
Module._resolveLookupPaths = function(request, parent, newReturn) {
Module._resolveLookupPaths = function(request, parent) {
if (NativeModule.canBeRequiredByUsers(request)) {
debug('looking for %j in []', request);
return (newReturn ? null : [request, []]);
return null;
}

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

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.
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);
const mainPaths = ['.'].concat(Module._nodeModulePaths('.'), modulePaths);

debug('looking for %j in %j', request, mainPaths);
return (newReturn ? mainPaths : [request, mainPaths]);
}

// Is the parent an index module?
// 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) {
BridgeAR marked this conversation as resolved.
Show resolved Hide resolved
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;
return mainPaths;
}

debug('RELATIVE: requested: %s set ID to: %s from %s', request, id,
parent.id);
debug('RELATIVE: requested: %s from parent.id %s', request, parent.id);

const parentDir = [path.dirname(parent.filename)];
debug('looking for %j in %j', id, parentDir);
return (newReturn ? parentDir : [id, parentDir]);
debug('looking for %j', parentDir);
return parentDir;
};

// Check the cache for the requested file.
Expand Down Expand Up @@ -625,15 +567,15 @@ Module._resolveFilename = function(request, parent, isMain, options) {
for (var i = 0; i < options.paths.length; i++) {
const path = options.paths[i];
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++) {
if (!paths.includes(lookupPaths[j]))
paths.push(lookupPaths[j]);
}
}
} else {
paths = Module._resolveLookupPaths(request, parent, true);
paths = Module._resolveLookupPaths(request, parent);
}

// Look up the filename first, since that's the cache key.
Expand Down
3 changes: 1 addition & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -855,8 +855,7 @@ REPLServer.prototype.createContext = function() {
}

const module = new CJSModule('<repl>');
module.paths =
CJSModule._resolveLookupPaths('<repl>', parentModule, true) || [];
module.paths = CJSModule._resolveLookupPaths('<repl>', parentModule) || [];

Object.defineProperty(context, 'module', {
configurable: true,
Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-module-relative-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ function testFirstInPath(moduleName, isLocalModule) {
assert.strictEqual :
assert.notStrictEqual;

const lookupResults = _module._resolveLookupPaths(moduleName);
let paths = _module._resolveLookupPaths(moduleName);

let paths = lookupResults[1];
assertFunction(paths[0], '.');

paths = _module._resolveLookupPaths(moduleName, null, true);
paths = _module._resolveLookupPaths(moduleName, null);
assertFunction(paths && paths[0], '.');
}

Expand Down