From a693baa0cb25904aae5c08417c143ad254f55714 Mon Sep 17 00:00:00 2001 From: Darshan Sen Date: Fri, 5 Feb 2021 19:44:44 +0530 Subject: [PATCH] module: use optional chaining in cjs/loader.js PR-URL: https://github.com/nodejs/node/pull/37238 Reviewed-By: Antoine du Hamel Reviewed-By: Zijian Liu Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rich Trott --- lib/internal/modules/cjs/loader.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 5abfa465e0407d..e7298a2acd782e 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -153,7 +153,7 @@ function stat(filename) { } function updateChildren(parent, child, scan) { - const children = parent && parent.children; + const children = parent?.children; if (children && !(scan && ArrayPrototypeIncludes(children, child))) ArrayPrototypePush(children, child); } @@ -467,7 +467,7 @@ function resolveExports(nmPath, request) { return; const pkgPath = path.resolve(nmPath, name); const pkg = readPackage(pkgPath); - if (pkg && pkg.exports !== null && pkg.exports !== undefined) { + if (pkg?.exports != null) { try { return finalizeEsmResolution(packageExportsResolve( pathToFileURL(pkgPath + '/package.json'), '.' + expansion, pkg, null, @@ -668,7 +668,7 @@ Module._resolveLookupPaths = function(request, parent) { (!isWindows || StringPrototypeCharAt(request, 1) !== '\\'))) { let paths = modulePaths; - if (parent != null && parent.paths && parent.paths.length) { + if (parent?.paths?.length) { paths = ArrayPrototypeConcat(parent.paths, paths); } @@ -781,7 +781,7 @@ Module._load = function(request, parent, isMain) { } const mod = loadNativeModule(filename, request); - if (mod && mod.canBeRequiredByUsers) return mod.exports; + if (mod?.canBeRequiredByUsers) return mod.exports; // Don't call updateChildren(), Module constructor already does. const module = cachedModule || new Module(filename, parent); @@ -817,7 +817,7 @@ Module._load = function(request, parent, isMain) { delete Module._cache[filename]; if (parent !== undefined) { delete relativeResolveCache[relResolveCacheIdentifier]; - const children = parent && parent.children; + const children = parent?.children; if (ArrayIsArray(children)) { const index = ArrayPrototypeIndexOf(children, module); if (index !== -1) { @@ -877,11 +877,10 @@ Module._resolveFilename = function(request, parent, isMain, options) { paths = Module._resolveLookupPaths(request, parent); } - if (parent && parent.filename) { + if (parent?.filename) { if (request[0] === '#') { const pkg = readPackageScope(parent.filename) || {}; - if (pkg.data && pkg.data.imports !== null && - pkg.data.imports !== undefined) { + if (pkg.data?.imports != null) { try { return finalizeEsmResolution( packageImportsResolve(request, pathToFileURL(parent.filename), @@ -1102,9 +1101,9 @@ Module._extensions['.js'] = function(module, filename) { if (StringPrototypeEndsWith(filename, '.js')) { const pkg = readPackageScope(filename); // Function require shouldn't be used in ES modules. - if (pkg && pkg.data && pkg.data.type === 'module') { + if (pkg?.data?.type === 'module') { const parent = moduleParentCache.get(module); - const parentPath = parent && parent.filename; + const parentPath = parent?.filename; const packageJsonPath = path.resolve(pkg.path, 'package.json'); throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath); } @@ -1112,7 +1111,7 @@ Module._extensions['.js'] = function(module, filename) { // If already analyzed the source, then it will be cached. const cached = cjsParseCache.get(module); let content; - if (cached && cached.source) { + if (cached?.source) { content = cached.source; cached.source = undefined; } else {