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

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

‎lib/internal/modules/cjs/loader.js‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function stat(filename) {
153153
}
154154

155155
functionupdateChildren(parent,child,scan){
156-
constchildren=parent&&parent.children;
156+
constchildren=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
constpkgPath=path.resolve(nmPath,name);
469469
constpkg=readPackage(pkgPath);
470-
if(pkg&&pkg.exports!==null&&pkg.exports!==undefined){
470+
if(pkg?.exports!=null){
471471
try{
472472
returnfinalizeEsmResolution(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
letpaths=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
constmod=loadNativeModule(filename,request);
784-
if(mod&&mod.canBeRequiredByUsers)returnmod.exports;
784+
if(mod?.canBeRequiredByUsers)returnmod.exports;
785785

786786
// Don't call updateChildren(), Module constructor already does.
787787
constmodule=cachedModule||newModule(filename,parent);
@@ -817,7 +817,7 @@ Module._load = function(request, parent, isMain) {
817817
deleteModule._cache[filename];
818818
if(parent!==undefined){
819819
deleterelativeResolveCache[relResolveCacheIdentifier];
820-
constchildren=parent&&parent.children;
820+
constchildren=parent?.children;
821821
if(ArrayIsArray(children)){
822822
constindex=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
constpkg=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
returnfinalizeEsmResolution(
887886
packageImportsResolve(request,pathToFileURL(parent.filename),
@@ -1102,17 +1101,17 @@ Module._extensions['.js'] = function(module, filename) {
11021101
if(StringPrototypeEndsWith(filename,'.js')){
11031102
constpkg=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
constparent=moduleParentCache.get(module);
1107-
constparentPath=parent&&parent.filename;
1106+
constparentPath=parent?.filename;
11081107
constpackageJsonPath=path.resolve(pkg.path,'package.json');
11091108
thrownewERR_REQUIRE_ESM(filename,parentPath,packageJsonPath);
11101109
}
11111110
}
11121111
// If already analyzed the source, then it will be cached.
11131112
constcached=cjsParseCache.get(module);
11141113
letcontent;
1115-
if(cached&&cached.source){
1114+
if(cached?.source){
11161115
content=cached.source;
11171116
cached.source=undefined;
11181117
}else{

0 commit comments

Comments
 (0)