Skip to content

Commit

Permalink
lib: replace var with let/const
Browse files Browse the repository at this point in the history
PR-URL: #30440
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
khoumani authored and MylesBorins committed Dec 17, 2019
1 parent 793d360 commit bd853cc
Showing 1 changed file with 27 additions and 41 deletions.
68 changes: 27 additions & 41 deletions lib/internal/modules/cjs/loader.js
Expand Up @@ -153,7 +153,7 @@ Module.builtinModules = builtinModules;
Module._cache = Object.create(null); Module._cache = Object.create(null);
Module._pathCache = Object.create(null); Module._pathCache = Object.create(null);
Module._extensions = Object.create(null); Module._extensions = Object.create(null);
var modulePaths = []; let modulePaths = [];
Module.globalPaths = []; Module.globalPaths = [];


let patched = false; let patched = false;
Expand Down Expand Up @@ -341,7 +341,7 @@ function toRealPath(requestPath) {


// Given a path, check if the file exists with any of the set extensions // Given a path, check if the file exists with any of the set extensions
function tryExtensions(p, exts, isMain) { function tryExtensions(p, exts, isMain) {
for (var i = 0; i < exts.length; i++) { for (let i = 0; i < exts.length; i++) {
const filename = tryFile(p + exts[i], isMain); const filename = tryFile(p + exts[i], isMain);


if (filename) { if (filename) {
Expand Down Expand Up @@ -474,22 +474,22 @@ Module._findPath = function(request, paths, isMain) {
if (entry) if (entry)
return entry; return entry;


var exts; let exts;
var trailingSlash = request.length > 0 && let trailingSlash = request.length > 0 &&
request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH; request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH;
if (!trailingSlash) { if (!trailingSlash) {
trailingSlash = /(?:^|\/)\.?\.$/.test(request); trailingSlash = /(?:^|\/)\.?\.$/.test(request);
} }


// For each path // For each path
for (var i = 0; i < paths.length; i++) { for (let i = 0; i < paths.length; i++) {
// Don't search further if path doesn't exist // Don't search further if path doesn't exist
const curPath = paths[i]; const curPath = paths[i];
if (curPath && stat(curPath) < 1) continue; if (curPath && stat(curPath) < 1) continue;
var basePath = resolveExports(curPath, request, absoluteRequest); const basePath = resolveExports(curPath, request, absoluteRequest);
var filename; let filename;


var rc = stat(basePath); const rc = stat(basePath);
if (!trailingSlash) { if (!trailingSlash) {
if (rc === 0) { // File. if (rc === 0) { // File.
if (!isMain) { if (!isMain) {
Expand Down Expand Up @@ -556,9 +556,7 @@ if (isWindows) {
return [from + 'node_modules']; return [from + 'node_modules'];


const paths = []; const paths = [];
var p = 0; for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
var last = from.length;
for (var i = from.length - 1; i >= 0; --i) {
const code = from.charCodeAt(i); const code = from.charCodeAt(i);
// The path segment separator check ('\' and '/') was used to get // The path segment separator check ('\' and '/') was used to get
// node_modules path for every path segment. // node_modules path for every path segment.
Expand Down Expand Up @@ -597,9 +595,7 @@ if (isWindows) {
// to be absolute. Doing a fully-edge-case-correct path.split // to be absolute. Doing a fully-edge-case-correct path.split
// that works on both Windows and Posix is non-trivial. // that works on both Windows and Posix is non-trivial.
const paths = []; const paths = [];
var p = 0; for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
var last = from.length;
for (var i = from.length - 1; i >= 0; --i) {
const code = from.charCodeAt(i); const code = from.charCodeAt(i);
if (code === CHAR_FORWARD_SLASH) { if (code === CHAR_FORWARD_SLASH) {
if (p !== nmLen) if (p !== nmLen)
Expand Down Expand Up @@ -744,7 +740,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
return request; return request;
} }


var paths; let paths;


if (typeof options === 'object' && options !== null) { if (typeof options === 'object' && options !== null) {
if (Array.isArray(options.paths)) { if (Array.isArray(options.paths)) {
Expand All @@ -760,12 +756,12 @@ Module._resolveFilename = function(request, parent, isMain, options) {


paths = []; paths = [];


for (var i = 0; i < options.paths.length; i++) { for (let 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); const lookupPaths = Module._resolveLookupPaths(request, fakeParent);


for (var j = 0; j < lookupPaths.length; j++) { for (let j = 0; j < lookupPaths.length; j++) {
if (!paths.includes(lookupPaths[j])) if (!paths.includes(lookupPaths[j]))
paths.push(lookupPaths[j]); paths.push(lookupPaths[j]);
} }
Expand All @@ -784,7 +780,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
const filename = Module._findPath(request, paths, isMain); const filename = Module._findPath(request, paths, isMain);
if (!filename) { if (!filename) {
const requireStack = []; const requireStack = [];
for (var cursor = parent; for (let cursor = parent;
cursor; cursor;
cursor = cursor.parent) { cursor = cursor.parent) {
requireStack.push(cursor.filename || cursor.id); requireStack.push(cursor.filename || cursor.id);
Expand All @@ -794,7 +790,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
message = message + '\nRequire stack:\n- ' + requireStack.join('\n- '); message = message + '\nRequire stack:\n- ' + requireStack.join('\n- ');
} }
// eslint-disable-next-line no-restricted-syntax // eslint-disable-next-line no-restricted-syntax
var err = new Error(message); const err = new Error(message);
err.code = 'MODULE_NOT_FOUND'; err.code = 'MODULE_NOT_FOUND';
err.requireStack = requireStack; err.requireStack = requireStack;
throw err; throw err;
Expand Down Expand Up @@ -858,7 +854,7 @@ Module.prototype.require = function(id) {


// Resolved path to process.argv[1] will be lazily placed here // Resolved path to process.argv[1] will be lazily placed here
// (needed for setting breakpoint when called with --inspect-brk) // (needed for setting breakpoint when called with --inspect-brk)
var resolvedArgv; let resolvedArgv;
let hasPausedEntry = false; let hasPausedEntry = false;


// Run the file contents in the correct scope or sandbox. Expose // Run the file contents in the correct scope or sandbox. Expose
Expand Down Expand Up @@ -928,7 +924,7 @@ Module.prototype._compile = function(content, filename) {
compiledWrapper = compiled.function; compiledWrapper = compiled.function;
} }


var inspectorWrapper = null; let inspectorWrapper = null;
if (getOptionValue('--inspect-brk') && process._eval == null) { if (getOptionValue('--inspect-brk') && process._eval == null) {
if (!resolvedArgv) { if (!resolvedArgv) {
// We enter the repl if we're not given a filename argument. // We enter the repl if we're not given a filename argument.
Expand All @@ -947,7 +943,7 @@ Module.prototype._compile = function(content, filename) {
} }
const dirname = path.dirname(filename); const dirname = path.dirname(filename);
const require = makeRequireFunction(this, redirects); const require = makeRequireFunction(this, redirects);
var result; let result;
const exports = this.exports; const exports = this.exports;
const thisValue = exports; const thisValue = exports;
const module = this; const module = this;
Expand Down Expand Up @@ -1090,26 +1086,16 @@ function createRequire(filename) {
Module.createRequire = createRequire; Module.createRequire = createRequire;


Module._initPaths = function() { Module._initPaths = function() {
var homeDir; const homeDir = isWindows ? process.env.USERPROFILE : safeGetenv('HOME');
var nodePath; const nodePath = isWindows ? process.env.NODE_PATH : safeGetenv('NODE_PATH');
if (isWindows) {
homeDir = process.env.USERPROFILE;
nodePath = process.env.NODE_PATH;
} else {
homeDir = safeGetenv('HOME');
nodePath = safeGetenv('NODE_PATH');
}


// $PREFIX/lib/node, where $PREFIX is the root of the Node.js installation.
var prefixDir;
// process.execPath is $PREFIX/bin/node except on Windows where it is // process.execPath is $PREFIX/bin/node except on Windows where it is
// $PREFIX\node.exe. // $PREFIX\node.exe where $PREFIX is the root of the Node.js installation.
if (isWindows) { const prefixDir = isWindows ?
prefixDir = path.resolve(process.execPath, '..'); path.resolve(process.execPath, '..') :
} else { path.resolve(process.execPath, '..', '..');
prefixDir = path.resolve(process.execPath, '..', '..');
} let paths = [path.resolve(prefixDir, 'lib', 'node')];
var paths = [path.resolve(prefixDir, 'lib', 'node')];


if (homeDir) { if (homeDir) {
paths.unshift(path.resolve(homeDir, '.node_libraries')); paths.unshift(path.resolve(homeDir, '.node_libraries'));
Expand Down Expand Up @@ -1143,7 +1129,7 @@ Module._preloadModules = function(requests) {
throw e; throw e;
} }
} }
for (var n = 0; n < requests.length; n++) for (let n = 0; n < requests.length; n++)
parent.require(requests[n]); parent.require(requests[n]);
}; };


Expand Down

0 comments on commit bd853cc

Please sign in to comment.