From 200a63d3987626abe56928b9bd4f1131a84bfb57 Mon Sep 17 00:00:00 2001 From: Kenza Houmani Date: Tue, 12 Nov 2019 16:03:15 +0000 Subject: [PATCH 1/2] lib: replace var with let/const --- lib/internal/modules/cjs/loader.js | 54 +++++++++++++++--------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 243b9867cdc999..a9484cbf4a978d 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -160,7 +160,7 @@ Module.builtinModules = builtinModules; Module._cache = Object.create(null); Module._pathCache = Object.create(null); Module._extensions = Object.create(null); -var modulePaths = []; +let modulePaths = []; Module.globalPaths = []; let patched = false; @@ -349,7 +349,7 @@ function toRealPath(requestPath) { // Given a path, check if the file exists with any of the set extensions 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); if (filename) { @@ -627,22 +627,22 @@ Module._findPath = function(request, paths, isMain) { if (entry) return entry; - var exts; - var trailingSlash = request.length > 0 && + let exts; + let trailingSlash = request.length > 0 && request.charCodeAt(request.length - 1) === CHAR_FORWARD_SLASH; if (!trailingSlash) { trailingSlash = /(?:^|\/)\.?\.$/.test(request); } // 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 const curPath = paths[i]; if (curPath && stat(curPath) < 1) continue; - var basePath = resolveExports(curPath, request, absoluteRequest); - var filename; + const basePath = resolveExports(curPath, request, absoluteRequest); + let filename; - var rc = stat(basePath); + const rc = stat(basePath); if (!trailingSlash) { if (rc === 0) { // File. if (!isMain) { @@ -716,9 +716,9 @@ if (isWindows) { return [from + 'node_modules']; const paths = []; - var p = 0; - var last = from.length; - for (var i = from.length - 1; i >= 0; --i) { + let p = 0; + let last = from.length; + for (let i = from.length - 1; i >= 0; --i) { const code = from.charCodeAt(i); // The path segment separator check ('\' and '/') was used to get // node_modules path for every path segment. @@ -757,9 +757,9 @@ if (isWindows) { // to be absolute. Doing a fully-edge-case-correct path.split // that works on both Windows and Posix is non-trivial. const paths = []; - var p = 0; - var last = from.length; - for (var i = from.length - 1; i >= 0; --i) { + let p = 0; + let last = from.length; + for (let i = from.length - 1; i >= 0; --i) { const code = from.charCodeAt(i); if (code === CHAR_FORWARD_SLASH) { if (p !== nmLen) @@ -958,7 +958,7 @@ Module._resolveFilename = function(request, parent, isMain, options) { return request; } - var paths; + let paths; if (typeof options === 'object' && options !== null) { if (Array.isArray(options.paths)) { @@ -974,12 +974,12 @@ Module._resolveFilename = function(request, parent, isMain, options) { paths = []; - for (var i = 0; i < options.paths.length; i++) { + for (let i = 0; i < options.paths.length; i++) { const path = options.paths[i]; fakeParent.paths = Module._nodeModulePaths(path); 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])) paths.push(lookupPaths[j]); } @@ -998,7 +998,7 @@ Module._resolveFilename = function(request, parent, isMain, options) { const filename = Module._findPath(request, paths, isMain); if (!filename) { const requireStack = []; - for (var cursor = parent; + for (let cursor = parent; cursor; cursor = cursor.parent) { requireStack.push(cursor.filename || cursor.id); @@ -1008,7 +1008,7 @@ Module._resolveFilename = function(request, parent, isMain, options) { message = message + '\nRequire stack:\n- ' + requireStack.join('\n- '); } // eslint-disable-next-line no-restricted-syntax - var err = new Error(message); + const err = new Error(message); err.code = 'MODULE_NOT_FOUND'; err.requireStack = requireStack; throw err; @@ -1081,7 +1081,7 @@ Module.prototype.require = function(id) { // Resolved path to process.argv[1] will be lazily placed here // (needed for setting breakpoint when called with --inspect-brk) -var resolvedArgv; +let resolvedArgv; let hasPausedEntry = false; function wrapSafe(filename, content, cjsModuleInstance) { @@ -1151,7 +1151,7 @@ Module.prototype._compile = function(content, filename) { maybeCacheSourceMap(filename, content, this); const compiledWrapper = wrapSafe(filename, content, this); - var inspectorWrapper = null; + let inspectorWrapper = null; if (getOptionValue('--inspect-brk') && process._eval == null) { if (!resolvedArgv) { // We enter the repl if we're not given a filename argument. @@ -1170,7 +1170,7 @@ Module.prototype._compile = function(content, filename) { } const dirname = path.dirname(filename); const require = makeRequireFunction(this, redirects); - var result; + let result; const exports = this.exports; const thisValue = exports; const module = this; @@ -1311,8 +1311,8 @@ function createRequire(filename) { Module.createRequire = createRequire; Module._initPaths = function() { - var homeDir; - var nodePath; + let homeDir; + let nodePath; if (isWindows) { homeDir = process.env.USERPROFILE; nodePath = process.env.NODE_PATH; @@ -1322,7 +1322,7 @@ Module._initPaths = function() { } // $PREFIX/lib/node, where $PREFIX is the root of the Node.js installation. - var prefixDir; + let prefixDir; // process.execPath is $PREFIX/bin/node except on Windows where it is // $PREFIX\node.exe. if (isWindows) { @@ -1330,7 +1330,7 @@ Module._initPaths = function() { } else { prefixDir = path.resolve(process.execPath, '..', '..'); } - var paths = [path.resolve(prefixDir, 'lib', 'node')]; + let paths = [path.resolve(prefixDir, 'lib', 'node')]; if (homeDir) { paths.unshift(path.resolve(homeDir, '.node_libraries')); @@ -1364,7 +1364,7 @@ Module._preloadModules = function(requests) { throw e; } } - for (var n = 0; n < requests.length; n++) + for (let n = 0; n < requests.length; n++) parent.require(requests[n]); }; From 90684fae7a27d98615bcb27ec778678e1f7cf3a1 Mon Sep 17 00:00:00 2001 From: Kenza Houmani Date: Sat, 16 Nov 2019 15:43:33 +0000 Subject: [PATCH 2/2] lib: address comments --- lib/internal/modules/cjs/loader.js | 32 +++++++++--------------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index a9484cbf4a978d..048c66b7a30c9b 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -716,9 +716,7 @@ if (isWindows) { return [from + 'node_modules']; const paths = []; - let p = 0; - let last = from.length; - for (let i = from.length - 1; i >= 0; --i) { + for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) { const code = from.charCodeAt(i); // The path segment separator check ('\' and '/') was used to get // node_modules path for every path segment. @@ -757,9 +755,7 @@ if (isWindows) { // to be absolute. Doing a fully-edge-case-correct path.split // that works on both Windows and Posix is non-trivial. const paths = []; - let p = 0; - let last = from.length; - for (let i = from.length - 1; i >= 0; --i) { + for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) { const code = from.charCodeAt(i); if (code === CHAR_FORWARD_SLASH) { if (p !== nmLen) @@ -1311,25 +1307,15 @@ function createRequire(filename) { Module.createRequire = createRequire; Module._initPaths = function() { - let homeDir; - let nodePath; - if (isWindows) { - homeDir = process.env.USERPROFILE; - nodePath = process.env.NODE_PATH; - } else { - homeDir = safeGetenv('HOME'); - nodePath = safeGetenv('NODE_PATH'); - } + const homeDir = isWindows ? process.env.USERPROFILE : safeGetenv('HOME'); + const nodePath = isWindows ? process.env.NODE_PATH : safeGetenv('NODE_PATH'); - // $PREFIX/lib/node, where $PREFIX is the root of the Node.js installation. - let prefixDir; // process.execPath is $PREFIX/bin/node except on Windows where it is - // $PREFIX\node.exe. - if (isWindows) { - prefixDir = path.resolve(process.execPath, '..'); - } else { - prefixDir = path.resolve(process.execPath, '..', '..'); - } + // $PREFIX\node.exe where $PREFIX is the root of the Node.js installation. + const prefixDir = isWindows ? + path.resolve(process.execPath, '..') : + path.resolve(process.execPath, '..', '..'); + let paths = [path.resolve(prefixDir, 'lib', 'node')]; if (homeDir) {