Skip to content

Commit a693baa

Browse files
RaisinTendanielleadams
authored andcommitted
module: use optional chaining in cjs/loader.js
PR-URL: #37238 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent cd50e93 commit a693baa

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

lib/internal/modules/cjs/loader.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function stat(filename) {
153153
}
154154

155155
function updateChildren(parent, child, scan) {
156-
const children = parent && parent.children;
156+
const children = parent?.children;
157157
if (children && !(scan && ArrayPrototypeIncludes(children, child)))
158158
ArrayPrototypePush(children, child);
159159
}
@@ -467,7 +467,7 @@ function resolveExports(nmPath, request) {
467467
return;
468468
const pkgPath = path.resolve(nmPath, name);
469469
const pkg = readPackage(pkgPath);
470-
if (pkg && pkg.exports !== null && pkg.exports !== undefined) {
470+
if (pkg?.exports != null) {
471471
try {
472472
return finalizeEsmResolution(packageExportsResolve(
473473
pathToFileURL(pkgPath + '/package.json'), '.' + expansion, pkg, null,
@@ -668,7 +668,7 @@ Module._resolveLookupPaths = function(request, parent) {
668668
(!isWindows || StringPrototypeCharAt(request, 1) !== '\\'))) {
669669

670670
let paths = modulePaths;
671-
if (parent != null && parent.paths && parent.paths.length) {
671+
if (parent?.paths?.length) {
672672
paths = ArrayPrototypeConcat(parent.paths, paths);
673673
}
674674

@@ -781,7 +781,7 @@ Module._load = function(request, parent, isMain) {
781781
}
782782

783783
const mod = loadNativeModule(filename, request);
784-
if (mod && mod.canBeRequiredByUsers) return mod.exports;
784+
if (mod?.canBeRequiredByUsers) return mod.exports;
785785

786786
// Don't call updateChildren(), Module constructor already does.
787787
const module = cachedModule || new Module(filename, parent);
@@ -817,7 +817,7 @@ Module._load = function(request, parent, isMain) {
817817
delete Module._cache[filename];
818818
if (parent !== undefined) {
819819
delete relativeResolveCache[relResolveCacheIdentifier];
820-
const children = parent && parent.children;
820+
const children = parent?.children;
821821
if (ArrayIsArray(children)) {
822822
const index = ArrayPrototypeIndexOf(children, module);
823823
if (index !== -1) {
@@ -877,11 +877,10 @@ Module._resolveFilename = function(request, parent, isMain, options) {
877877
paths = Module._resolveLookupPaths(request, parent);
878878
}
879879

880-
if (parent && parent.filename) {
880+
if (parent?.filename) {
881881
if (request[0] === '#') {
882882
const pkg = readPackageScope(parent.filename) || {};
883-
if (pkg.data && pkg.data.imports !== null &&
884-
pkg.data.imports !== undefined) {
883+
if (pkg.data?.imports != null) {
885884
try {
886885
return finalizeEsmResolution(
887886
packageImportsResolve(request, pathToFileURL(parent.filename),
@@ -1102,17 +1101,17 @@ Module._extensions['.js'] = function(module, filename) {
11021101
if (StringPrototypeEndsWith(filename, '.js')) {
11031102
const pkg = readPackageScope(filename);
11041103
// Function require shouldn't be used in ES modules.
1105-
if (pkg && pkg.data && pkg.data.type === 'module') {
1104+
if (pkg?.data?.type === 'module') {
11061105
const parent = moduleParentCache.get(module);
1107-
const parentPath = parent && parent.filename;
1106+
const parentPath = parent?.filename;
11081107
const packageJsonPath = path.resolve(pkg.path, 'package.json');
11091108
throw new ERR_REQUIRE_ESM(filename, parentPath, packageJsonPath);
11101109
}
11111110
}
11121111
// If already analyzed the source, then it will be cached.
11131112
const cached = cjsParseCache.get(module);
11141113
let content;
1115-
if (cached && cached.source) {
1114+
if (cached?.source) {
11161115
content = cached.source;
11171116
cached.source = undefined;
11181117
} else {

0 commit comments

Comments
 (0)