Skip to content

Commit 3ce6e15

Browse files
khoumaniBridgeAR
authored andcommitted
lib: replace var with let/const
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>
1 parent a93345b commit 3ce6e15

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
@@ -158,7 +158,7 @@ Module.builtinModules = builtinModules;
158158
Module._cache = Object.create(null);
159159
Module._pathCache = Object.create(null);
160160
Module._extensions = Object.create(null);
161-
var modulePaths = [];
161+
let modulePaths = [];
162162
Module.globalPaths = [];
163163

164164
let patched = false;
@@ -347,7 +347,7 @@ function toRealPath(requestPath) {
347347

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

353353
if (filename) {
@@ -625,22 +625,22 @@ Module._findPath = function(request, paths, isMain) {
625625
if (entry)
626626
return entry;
627627

628-
var exts;
629-
var trailingSlash = request.length > 0 &&
628+
let exts;
629+
let trailingSlash = request.length > 0 &&
630630
request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH;
631631
if (!trailingSlash) {
632632
trailingSlash = /(?:^|\/)\.?\.$/.test(request);
633633
}
634634

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

643-
var rc = stat(basePath);
643+
const rc = stat(basePath);
644644
if (!trailingSlash) {
645645
if (rc === 0) { // File.
646646
if (!isMain) {
@@ -714,9 +714,7 @@ if (isWindows) {
714714
return [from + 'node_modules'];
715715

716716
const paths = [];
717-
var p = 0;
718-
var last = from.length;
719-
for (var i = from.length - 1; i >= 0; --i) {
717+
for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
720718
const code = from.charCodeAt(i);
721719
// The path segment separator check ('\' and '/') was used to get
722720
// node_modules path for every path segment.
@@ -755,9 +753,7 @@ if (isWindows) {
755753
// to be absolute. Doing a fully-edge-case-correct path.split
756754
// that works on both Windows and Posix is non-trivial.
757755
const paths = [];
758-
var p = 0;
759-
var last = from.length;
760-
for (var i = from.length - 1; i >= 0; --i) {
756+
for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
761757
const code = from.charCodeAt(i);
762758
if (code === CHAR_FORWARD_SLASH) {
763759
if (p !== nmLen)
@@ -902,7 +898,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
902898
return request;
903899
}
904900

905-
var paths;
901+
let paths;
906902

907903
if (typeof options === 'object' && options !== null) {
908904
if (Array.isArray(options.paths)) {
@@ -918,12 +914,12 @@ Module._resolveFilename = function(request, parent, isMain, options) {
918914

919915
paths = [];
920916

921-
for (var i = 0; i < options.paths.length; i++) {
917+
for (let i = 0; i < options.paths.length; i++) {
922918
const path = options.paths[i];
923919
fakeParent.paths = Module._nodeModulePaths(path);
924920
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
925921

926-
for (var j = 0; j < lookupPaths.length; j++) {
922+
for (let j = 0; j < lookupPaths.length; j++) {
927923
if (!paths.includes(lookupPaths[j]))
928924
paths.push(lookupPaths[j]);
929925
}
@@ -942,7 +938,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
942938
const filename = Module._findPath(request, paths, isMain);
943939
if (!filename) {
944940
const requireStack = [];
945-
for (var cursor = parent;
941+
for (let cursor = parent;
946942
cursor;
947943
cursor = cursor.parent) {
948944
requireStack.push(cursor.filename || cursor.id);
@@ -952,7 +948,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
952948
message = message + '\nRequire stack:\n- ' + requireStack.join('\n- ');
953949
}
954950
// eslint-disable-next-line no-restricted-syntax
955-
var err = new Error(message);
951+
const err = new Error(message);
956952
err.code = 'MODULE_NOT_FOUND';
957953
err.requireStack = requireStack;
958954
throw err;
@@ -1023,7 +1019,7 @@ Module.prototype.require = function(id) {
10231019

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

10291025
function wrapSafe(filename, content, cjsModuleInstance) {
@@ -1091,7 +1087,7 @@ Module.prototype._compile = function(content, filename) {
10911087
maybeCacheSourceMap(filename, content, this);
10921088
const compiledWrapper = wrapSafe(filename, content, this);
10931089

1094-
var inspectorWrapper = null;
1090+
let inspectorWrapper = null;
10951091
if (getOptionValue('--inspect-brk') && process._eval == null) {
10961092
if (!resolvedArgv) {
10971093
// We enter the repl if we're not given a filename argument.
@@ -1110,7 +1106,7 @@ Module.prototype._compile = function(content, filename) {
11101106
}
11111107
const dirname = path.dirname(filename);
11121108
const require = makeRequireFunction(this, redirects);
1113-
var result;
1109+
let result;
11141110
const exports = this.exports;
11151111
const thisValue = exports;
11161112
const module = this;
@@ -1249,26 +1245,16 @@ function createRequire(filename) {
12491245
Module.createRequire = createRequire;
12501246

12511247
Module._initPaths = function() {
1252-
var homeDir;
1253-
var nodePath;
1254-
if (isWindows) {
1255-
homeDir = process.env.USERPROFILE;
1256-
nodePath = process.env.NODE_PATH;
1257-
} else {
1258-
homeDir = safeGetenv('HOME');
1259-
nodePath = safeGetenv('NODE_PATH');
1260-
}
1248+
const homeDir = isWindows ? process.env.USERPROFILE : safeGetenv('HOME');
1249+
const nodePath = isWindows ? process.env.NODE_PATH : safeGetenv('NODE_PATH');
12611250

1262-
// $PREFIX/lib/node, where $PREFIX is the root of the Node.js installation.
1263-
var prefixDir;
12641251
// process.execPath is $PREFIX/bin/node except on Windows where it is
1265-
// $PREFIX\node.exe.
1266-
if (isWindows) {
1267-
prefixDir = path.resolve(process.execPath, '..');
1268-
} else {
1269-
prefixDir = path.resolve(process.execPath, '..', '..');
1270-
}
1271-
var paths = [path.resolve(prefixDir, 'lib', 'node')];
1252+
// $PREFIX\node.exe where $PREFIX is the root of the Node.js installation.
1253+
const prefixDir = isWindows ?
1254+
path.resolve(process.execPath, '..') :
1255+
path.resolve(process.execPath, '..', '..');
1256+
1257+
let paths = [path.resolve(prefixDir, 'lib', 'node')];
12721258

12731259
if (homeDir) {
12741260
paths.unshift(path.resolve(homeDir, '.node_libraries'));
@@ -1302,7 +1288,7 @@ Module._preloadModules = function(requests) {
13021288
throw e;
13031289
}
13041290
}
1305-
for (var n = 0; n < requests.length; n++)
1291+
for (let n = 0; n < requests.length; n++)
13061292
parent.require(requests[n]);
13071293
};
13081294

0 commit comments

Comments
 (0)