Skip to content

Commit 56c57d8

Browse files
khoumanitargos
authored andcommitted
lib: replace var with let/const
PR-URL: #30440 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent c722421 commit 56c57d8

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

lib/internal/modules/cjs/loader.js

+27-41
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Module.builtinModules = builtinModules;
153153
Module._cache = Object.create(null);
154154
Module._pathCache = Object.create(null);
155155
Module._extensions = Object.create(null);
156-
var modulePaths = [];
156+
let modulePaths = [];
157157
Module.globalPaths = [];
158158

159159
let patched = false;
@@ -341,7 +341,7 @@ function toRealPath(requestPath) {
341341

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

347347
if (filename) {
@@ -474,22 +474,22 @@ Module._findPath = function(request, paths, isMain) {
474474
if (entry)
475475
return entry;
476476

477-
var exts;
478-
var trailingSlash = request.length > 0 &&
477+
let exts;
478+
let trailingSlash = request.length > 0 &&
479479
request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH;
480480
if (!trailingSlash) {
481481
trailingSlash = /(?:^|\/)\.?\.$/.test(request);
482482
}
483483

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

492-
var rc = stat(basePath);
492+
const rc = stat(basePath);
493493
if (!trailingSlash) {
494494
if (rc === 0) { // File.
495495
if (!isMain) {
@@ -556,9 +556,7 @@ if (isWindows) {
556556
return [from + 'node_modules'];
557557

558558
const paths = [];
559-
var p = 0;
560-
var last = from.length;
561-
for (var i = from.length - 1; i >= 0; --i) {
559+
for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
562560
const code = from.charCodeAt(i);
563561
// The path segment separator check ('\' and '/') was used to get
564562
// node_modules path for every path segment.
@@ -597,9 +595,7 @@ if (isWindows) {
597595
// to be absolute. Doing a fully-edge-case-correct path.split
598596
// that works on both Windows and Posix is non-trivial.
599597
const paths = [];
600-
var p = 0;
601-
var last = from.length;
602-
for (var i = from.length - 1; i >= 0; --i) {
598+
for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
603599
const code = from.charCodeAt(i);
604600
if (code === CHAR_FORWARD_SLASH) {
605601
if (p !== nmLen)
@@ -744,7 +740,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
744740
return request;
745741
}
746742

747-
var paths;
743+
let paths;
748744

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

761757
paths = [];
762758

763-
for (var i = 0; i < options.paths.length; i++) {
759+
for (let i = 0; i < options.paths.length; i++) {
764760
const path = options.paths[i];
765761
fakeParent.paths = Module._nodeModulePaths(path);
766762
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
767763

768-
for (var j = 0; j < lookupPaths.length; j++) {
764+
for (let j = 0; j < lookupPaths.length; j++) {
769765
if (!paths.includes(lookupPaths[j]))
770766
paths.push(lookupPaths[j]);
771767
}
@@ -784,7 +780,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
784780
const filename = Module._findPath(request, paths, isMain);
785781
if (!filename) {
786782
const requireStack = [];
787-
for (var cursor = parent;
783+
for (let cursor = parent;
788784
cursor;
789785
cursor = cursor.parent) {
790786
requireStack.push(cursor.filename || cursor.id);
@@ -794,7 +790,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
794790
message = message + '\nRequire stack:\n- ' + requireStack.join('\n- ');
795791
}
796792
// eslint-disable-next-line no-restricted-syntax
797-
var err = new Error(message);
793+
const err = new Error(message);
798794
err.code = 'MODULE_NOT_FOUND';
799795
err.requireStack = requireStack;
800796
throw err;
@@ -858,7 +854,7 @@ Module.prototype.require = function(id) {
858854

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

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

931-
var inspectorWrapper = null;
927+
let inspectorWrapper = null;
932928
if (getOptionValue('--inspect-brk') && process._eval == null) {
933929
if (!resolvedArgv) {
934930
// We enter the repl if we're not given a filename argument.
@@ -947,7 +943,7 @@ Module.prototype._compile = function(content, filename) {
947943
}
948944
const dirname = path.dirname(filename);
949945
const require = makeRequireFunction(this, redirects);
950-
var result;
946+
let result;
951947
const exports = this.exports;
952948
const thisValue = exports;
953949
const module = this;
@@ -1090,26 +1086,16 @@ function createRequire(filename) {
10901086
Module.createRequire = createRequire;
10911087

10921088
Module._initPaths = function() {
1093-
var homeDir;
1094-
var nodePath;
1095-
if (isWindows) {
1096-
homeDir = process.env.USERPROFILE;
1097-
nodePath = process.env.NODE_PATH;
1098-
} else {
1099-
homeDir = safeGetenv('HOME');
1100-
nodePath = safeGetenv('NODE_PATH');
1101-
}
1089+
const homeDir = isWindows ? process.env.USERPROFILE : safeGetenv('HOME');
1090+
const nodePath = isWindows ? process.env.NODE_PATH : safeGetenv('NODE_PATH');
11021091

1103-
// $PREFIX/lib/node, where $PREFIX is the root of the Node.js installation.
1104-
var prefixDir;
11051092
// process.execPath is $PREFIX/bin/node except on Windows where it is
1106-
// $PREFIX\node.exe.
1107-
if (isWindows) {
1108-
prefixDir = path.resolve(process.execPath, '..');
1109-
} else {
1110-
prefixDir = path.resolve(process.execPath, '..', '..');
1111-
}
1112-
var paths = [path.resolve(prefixDir, 'lib', 'node')];
1093+
// $PREFIX\node.exe where $PREFIX is the root of the Node.js installation.
1094+
const prefixDir = isWindows ?
1095+
path.resolve(process.execPath, '..') :
1096+
path.resolve(process.execPath, '..', '..');
1097+
1098+
let paths = [path.resolve(prefixDir, 'lib', 'node')];
11131099

11141100
if (homeDir) {
11151101
paths.unshift(path.resolve(homeDir, '.node_libraries'));
@@ -1143,7 +1129,7 @@ Module._preloadModules = function(requests) {
11431129
throw e;
11441130
}
11451131
}
1146-
for (var n = 0; n < requests.length; n++)
1132+
for (let n = 0; n < requests.length; n++)
11471133
parent.require(requests[n]);
11481134
};
11491135

0 commit comments

Comments
 (0)