Skip to content

Commit

Permalink
module: use optional chaining in cjs/loader.js
Browse files Browse the repository at this point in the history
PR-URL: #37238
Reviewed-By: Antoine du Hamel <[email protected]>
Reviewed-By: Zijian Liu <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
RaisinTen authored and danielleadams committed Feb 16, 2021
1 parent cd50e93 commit a693baa
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -1102,17 +1101,17 @@ 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);
}
}
// 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 {
Expand Down

0 comments on commit a693baa

Please sign in to comment.