Skip to content

Commit 0103979

Browse files
GeoffreyBoothtargos
authored andcommitted
esm: require braces for modules code
PR-URL: #49657 Backport-PR-URL: #50268 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 0aaab45 commit 0103979

13 files changed

Lines changed: 146 additions & 107 deletions

File tree

‎.eslintrc.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ module.exports = {
5353
overrides: [
5454
{
5555
files: [
56-
'test/es-module/test-esm-type-flag.js',
57-
'test/es-module/test-esm-type-flag-alias.js',
5856
'*.mjs',
5957
'test/es-module/test-esm-example-loader.js',
58+
'test/es-module/test-esm-type-flag.js',
59+
'test/es-module/test-esm-type-flag-alias.js',
6060
],
6161
parserOptions: {sourceType: 'module'},
6262
},
@@ -111,6 +111,14 @@ module.exports = {
111111
},
112112
]},
113113
},
114+
{
115+
files: [
116+
'lib/internal/modules/**/*.js',
117+
],
118+
rules: {
119+
'curly': 'error',
120+
},
121+
},
114122
{
115123
files: [
116124
'lib/internal/test_runner/**/*.js',

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

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function stat(filename) {
173173
filename=path.toNamespacedPath(filename);
174174
if(statCache!==null){
175175
constresult=statCache.get(filename);
176-
if(result!==undefined)returnresult;
176+
if(result!==undefined){returnresult;}
177177
}
178178
constresult=internalModuleStat(filename);
179179
if(statCache!==null&&result>=0){
@@ -197,8 +197,9 @@ ObjectDefineProperty(Module, '_stat', {
197197

198198
functionupdateChildren(parent,child,scan){
199199
constchildren=parent?.children;
200-
if(children&&!(scan&&ArrayPrototypeIncludes(children,child)))
200+
if(children&&!(scan&&ArrayPrototypeIncludes(children,child))){
201201
ArrayPrototypePush(children,child);
202+
}
202203
}
203204

204205
functionreportModuleToWatchMode(filename){
@@ -386,13 +387,16 @@ function readPackageScope(checkPath) {
386387
if(enabledPermission&&!permission.has('fs.read',checkPath+sep)){
387388
returnfalse;
388389
}
389-
if(StringPrototypeEndsWith(checkPath,sep+'node_modules'))
390+
if(StringPrototypeEndsWith(checkPath,sep+'node_modules')){
390391
returnfalse;
392+
}
391393
constpjson=_readPackage(checkPath+sep);
392-
if(pjson.exists)return{
393-
data: pjson,
394-
path: checkPath,
395-
};
394+
if(pjson.exists){
395+
return{
396+
data: pjson,
397+
path: checkPath,
398+
};
399+
}
396400
}while(separatorIndex>rootSeparatorIndex);
397401
returnfalse;
398402
}
@@ -445,7 +449,7 @@ const realpathCache = new SafeMap();
445449
// absolute realpath.
446450
functiontryFile(requestPath,isMain){
447451
constrc=_stat(requestPath);
448-
if(rc!==0)return;
452+
if(rc!==0){return;}
449453
if(getOptionValue('--preserve-symlinks')&&!isMain){
450454
returnpath.resolve(requestPath);
451455
}
@@ -479,15 +483,15 @@ function findLongestRegisteredExtension(filename) {
479483
letstartIndex=0;
480484
while((index=StringPrototypeIndexOf(name,'.',startIndex))!==-1){
481485
startIndex=index+1;
482-
if(index===0)continue;// Skip dotfiles like .gitignore
486+
if(index===0){continue;}// Skip dotfiles like .gitignore
483487
currentExtension=StringPrototypeSlice(name,index);
484-
if(Module._extensions[currentExtension])returncurrentExtension;
488+
if(Module._extensions[currentExtension]){returncurrentExtension;}
485489
}
486490
return'.js';
487491
}
488492

489493
functiontrySelfParentPath(parent){
490-
if(!parent)returnfalse;
494+
if(!parent){returnfalse;}
491495

492496
if(parent.filename){
493497
returnparent.filename;
@@ -501,7 +505,7 @@ function trySelfParentPath(parent) {
501505
}
502506

503507
functiontrySelf(parentPath,request){
504-
if(!parentPath)returnfalse;
508+
if(!parentPath){returnfalse;}
505509

506510
const{data: pkg,path: pkgPath}=readPackageScope(parentPath);
507511
if(!pkg||pkg.exports==null||pkg.name===undefined){
@@ -523,8 +527,9 @@ function trySelf(parentPath, request) {
523527
pathToFileURL(pkgPath+'/package.json'),expansion,pkg,
524528
pathToFileURL(parentPath),getCjsConditions()),parentPath,pkgPath);
525529
}catch(e){
526-
if(e.code==='ERR_MODULE_NOT_FOUND')
530+
if(e.code==='ERR_MODULE_NOT_FOUND'){
527531
throwcreateEsmNotFoundErr(request,pkgPath+'/package.json');
532+
}
528533
throwe;
529534
}
530535
}
@@ -537,8 +542,7 @@ function resolveExports(nmPath, request) {
537542
// The implementation's behavior is meant to mirror resolution in ESM.
538543
const{1: name,2: expansion=''}=
539544
RegExpPrototypeExec(EXPORTS_PATTERN,request)||kEmptyObject;
540-
if(!name)
541-
return;
545+
if(!name){return;}
542546
constpkgPath=path.resolve(nmPath,name);
543547
constpkg=_readPackage(pkgPath);
544548
if(pkg.exists&&pkg.exports!=null){
@@ -548,8 +552,9 @@ function resolveExports(nmPath, request) {
548552
pathToFileURL(pkgPath+'/package.json'),'.'+expansion,pkg,null,
549553
getCjsConditions()),null,pkgPath);
550554
}catch(e){
551-
if(e.code==='ERR_MODULE_NOT_FOUND')
555+
if(e.code==='ERR_MODULE_NOT_FOUND'){
552556
throwcreateEsmNotFoundErr(request,pkgPath+'/package.json');
557+
}
553558
throwe;
554559
}
555560
}
@@ -571,8 +576,9 @@ Module._findPath = function(request, paths, isMain) {
571576

572577
constcacheKey=request+'\x00'+ArrayPrototypeJoin(paths,'\x00');
573578
constentry=Module._pathCache[cacheKey];
574-
if(entry)
579+
if(entry){
575580
returnentry;
581+
}
576582

577583
letexts;
578584
consttrailingSlash=request.length>0&&
@@ -619,8 +625,9 @@ Module._findPath = function(request, paths, isMain) {
619625

620626
if(!absoluteRequest){
621627
constexportsResolved=resolveExports(curPath,request);
622-
if(exportsResolved)
628+
if(exportsResolved){
623629
returnexportsResolved;
630+
}
624631
}
625632

626633
constbasePath=path.resolve(curPath,request);
@@ -652,16 +659,18 @@ Module._findPath = function(request, paths, isMain) {
652659

653660
if(!filename){
654661
// Try it with each of the extensions
655-
if(exts===undefined)
662+
if(exts===undefined){
656663
exts=ObjectKeys(Module._extensions);
664+
}
657665
filename=tryExtensions(basePath,exts,isMain);
658666
}
659667
}
660668

661669
if(!filename&&rc===1){// Directory.
662670
// try it with each of the extensions at "index"
663-
if(exts===undefined)
671+
if(exts===undefined){
664672
exts=ObjectKeys(Module._extensions);
673+
}
665674
filename=tryPackage(basePath,exts,isMain,request);
666675
}
667676

@@ -697,8 +706,9 @@ if (isWindows) {
697706
// path.resolve will make sure from.length >=3 in Windows.
698707
if(StringPrototypeCharCodeAt(from,from.length-1)===
699708
CHAR_BACKWARD_SLASH&&
700-
StringPrototypeCharCodeAt(from,from.length-2)===CHAR_COLON)
709+
StringPrototypeCharCodeAt(from,from.length-2)===CHAR_COLON){
701710
return[from+'node_modules'];
711+
}
702712

703713
constpaths=[];
704714
for(leti=from.length-1,p=0,last=from.length;i>=0;--i){
@@ -711,11 +721,12 @@ if (isWindows) {
711721
if(code===CHAR_BACKWARD_SLASH||
712722
code===CHAR_FORWARD_SLASH||
713723
code===CHAR_COLON){
714-
if(p!==nmLen)
724+
if(p!==nmLen){
715725
ArrayPrototypePush(
716726
paths,
717727
StringPrototypeSlice(from,0,last)+'\\node_modules',
718728
);
729+
}
719730
last=i;
720731
p=0;
721732
}elseif(p!==-1){
@@ -736,8 +747,9 @@ if (isWindows) {
736747
from=path.resolve(from);
737748
// Return early not only to avoid unnecessary work, but to *avoid* returning
738749
// an array of two items for a root: [ '//node_modules', '/node_modules' ]
739-
if(from==='/')
750+
if(from==='/'){
740751
return['/node_modules'];
752+
}
741753

742754
// note: this approach *only* works when the path is guaranteed
743755
// to be absolute. Doing a fully-edge-case-correct path.split
@@ -746,11 +758,12 @@ if (isWindows) {
746758
for(leti=from.length-1,p=0,last=from.length;i>=0;--i){
747759
constcode=StringPrototypeCharCodeAt(from,i);
748760
if(code===CHAR_FORWARD_SLASH){
749-
if(p!==nmLen)
761+
if(p!==nmLen){
750762
ArrayPrototypePush(
751763
paths,
752764
StringPrototypeSlice(from,0,last)+'/node_modules',
753765
);
766+
}
754767
last=i;
755768
p=0;
756769
}elseif(p!==-1){
@@ -827,14 +840,15 @@ const CircularRequirePrototypeWarningProxy = new Proxy({}, {
827840
// Allow __esModule access in any case because it is used in the output
828841
// of transpiled code to determine whether something comes from an
829842
// ES module, and is not used as a regular key of `module.exports`.
830-
if(propintarget||prop==='__esModule')returntarget[prop];
843+
if(propintarget||prop==='__esModule'){returntarget[prop];}
831844
emitCircularRequireWarning(prop);
832845
returnundefined;
833846
},
834847

835848
getOwnPropertyDescriptor(target,prop){
836-
if(ObjectPrototypeHasOwnProperty(target,prop)||prop==='__esModule')
849+
if(ObjectPrototypeHasOwnProperty(target,prop)||prop==='__esModule'){
837850
returnObjectGetOwnPropertyDescriptor(target,prop);
851+
}
838852
emitCircularRequireWarning(prop);
839853
returnundefined;
840854
},
@@ -878,8 +892,9 @@ Module._load = function(request, parent, isMain) {
878892
constcachedModule=Module._cache[filename];
879893
if(cachedModule!==undefined){
880894
updateChildren(parent,cachedModule,true);
881-
if(!cachedModule.loaded)
895+
if(!cachedModule.loaded){
882896
returngetExportsForCircularRequire(cachedModule);
897+
}
883898
returncachedModule.exports;
884899
}
885900
deleterelativeResolveCache[relResolveCacheIdentifier];
@@ -904,8 +919,9 @@ Module._load = function(request, parent, isMain) {
904919
updateChildren(parent,cachedModule,true);
905920
if(!cachedModule.loaded){
906921
constparseCachedModule=cjsParseCache.get(cachedModule);
907-
if(!parseCachedModule||parseCachedModule.loaded)
922+
if(!parseCachedModule||parseCachedModule.loaded){
908923
returngetExportsForCircularRequire(cachedModule);
924+
}
909925
parseCachedModule.loaded=true;
910926
}else{
911927
returncachedModule.exports;
@@ -988,8 +1004,9 @@ Module._resolveFilename = function(request, parent, isMain, options) {
9881004
constlookupPaths=Module._resolveLookupPaths(request,fakeParent);
9891005

9901006
for(letj=0;j<lookupPaths.length;j++){
991-
if(!ArrayPrototypeIncludes(paths,lookupPaths[j]))
1007+
if(!ArrayPrototypeIncludes(paths,lookupPaths[j])){
9921008
ArrayPrototypePush(paths,lookupPaths[j]);
1009+
}
9931010
}
9941011
}
9951012
}
@@ -1013,8 +1030,9 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10131030
getCjsConditions()),parentPath,
10141031
pkg.path);
10151032
}catch(e){
1016-
if(e.code==='ERR_MODULE_NOT_FOUND')
1033+
if(e.code==='ERR_MODULE_NOT_FOUND'){
10171034
throwcreateEsmNotFoundErr(request);
1035+
}
10181036
throwe;
10191037
}
10201038
}
@@ -1032,7 +1050,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10321050

10331051
// Look up the filename first, since that's the cache key.
10341052
constfilename=Module._findPath(request,paths,isMain);
1035-
if(filename)returnfilename;
1053+
if(filename){returnfilename;}
10361054
constrequireStack=[];
10371055
for(letcursor=parent;
10381056
cursor;
@@ -1053,13 +1071,15 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10531071

10541072
functionfinalizeEsmResolution(resolved,parentPath,pkgPath){
10551073
const{ encodedSepRegEx }=require('internal/modules/esm/resolve');
1056-
if(RegExpPrototypeExec(encodedSepRegEx,resolved)!==null)
1074+
if(RegExpPrototypeExec(encodedSepRegEx,resolved)!==null){
10571075
thrownewERR_INVALID_MODULE_SPECIFIER(
10581076
resolved,'must not include encoded "/" or "\\" characters',parentPath);
1077+
}
10591078
constfilename=fileURLToPath(resolved);
10601079
constactual=tryFile(filename);
1061-
if(actual)
1080+
if(actual){
10621081
returnactual;
1082+
}
10631083
consterr=createEsmNotFoundErr(filename,
10641084
path.resolve(pkgPath,'package.json'));
10651085
throwerr;
@@ -1069,8 +1089,9 @@ function createEsmNotFoundErr(request, path) {
10691089
// eslint-disable-next-line no-restricted-syntax
10701090
consterr=newError(`Cannot find module '${request}'`);
10711091
err.code='MODULE_NOT_FOUND';
1072-
if(path)
1092+
if(path){
10731093
err.path=path;
1094+
}
10741095
// TODO(BridgeAR): Add the requireStack as well.
10751096
returnerr;
10761097
}
@@ -1085,8 +1106,9 @@ Module.prototype.load = function(filename) {
10851106

10861107
constextension=findLongestRegisteredExtension(filename);
10871108
// allow .mjs to be overridden
1088-
if(StringPrototypeEndsWith(filename,'.mjs')&&!Module._extensions['.mjs'])
1109+
if(StringPrototypeEndsWith(filename,'.mjs')&&!Module._extensions['.mjs']){
10891110
thrownewERR_REQUIRE_ESM(filename,true);
1111+
}
10901112

10911113
Module._extensions[extension](this,filename);
10921114
this.loaded=true;
@@ -1097,8 +1119,9 @@ Module.prototype.load = function(filename) {
10971119
// Preemptively cache
10981120
if((module?.module===undefined||
10991121
module.module.getStatus()<kEvaluated)&&
1100-
!cascadedLoader.cjsCache.has(this))
1122+
!cascadedLoader.cjsCache.has(this)){
11011123
cascadedLoader.cjsCache.set(this,exports);
1124+
}
11021125
};
11031126

11041127
// Loads a module at the given file path. Returns that module's
@@ -1233,7 +1256,7 @@ Module.prototype._compile = function(content, filename) {
12331256
constexports=this.exports;
12341257
constthisValue=exports;
12351258
constmodule=this;
1236-
if(requireDepth===0)statCache=newSafeMap();
1259+
if(requireDepth===0){statCache=newSafeMap();}
12371260
if(inspectorWrapper){
12381261
result=inspectorWrapper(compiledWrapper,thisValue,exports,
12391262
require,module,filename,dirname);
@@ -1242,7 +1265,7 @@ Module.prototype._compile = function(content, filename) {
12421265
[exports,require,module,filename,dirname]);
12431266
}
12441267
hasLoadedAnyUserCJSModule=true;
1245-
if(requireDepth===0)statCache=null;
1268+
if(requireDepth===0){statCache=null;}
12461269
returnresult;
12471270
};
12481271

@@ -1399,8 +1422,7 @@ Module._initPaths = function() {
13991422
};
14001423

14011424
Module._preloadModules=function(requests){
1402-
if(!ArrayIsArray(requests))
1403-
return;
1425+
if(!ArrayIsArray(requests)){return;}
14041426

14051427
isPreloading=true;
14061428

@@ -1416,8 +1438,9 @@ Module._preloadModules = function(requests) {
14161438
throwe;
14171439
}
14181440
}
1419-
for(letn=0;n<requests.length;n++)
1441+
for(letn=0;n<requests.length;n++){
14201442
internalRequire(parent,requests[n]);
1443+
}
14211444
isPreloading=false;
14221445
};
14231446

0 commit comments

Comments
 (0)