Skip to content

Commit 009f613

Browse files
committed
esm: require braces for modules code
PR-URL: nodejs/node#49657 Backport-PR-URL: nodejs/node#50669 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Jacob Smith <[email protected]>
1 parent 1769bf3 commit 009f613

File tree

12 files changed

+181
-132
lines changed

12 files changed

+181
-132
lines changed

graal-nodejs/.eslintrc.js

+10-2
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
rules: {
116124
// ESLint built-in rules

graal-nodejs/lib/internal/modules/cjs/loader.js

+67-42
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function stat(filename) {
172172
filename = path.toNamespacedPath(filename);
173173
if (statCache !== null) {
174174
const result = statCache.get(filename);
175-
if (result !== undefined) return result;
175+
if (result !== undefined) { return result; }
176176
}
177177
const result = internalModuleStat(filename);
178178
if (statCache !== null && result >= 0) {
@@ -196,8 +196,9 @@ ObjectDefineProperty(Module, '_stat', {
196196

197197
function updateChildren(parent, child, scan) {
198198
const children = parent?.children;
199-
if (children && !(scan && ArrayPrototypeIncludes(children, child)))
199+
if (children && !(scan && ArrayPrototypeIncludes(children, child))) {
200200
ArrayPrototypePush(children, child);
201+
}
201202
}
202203

203204
function reportModuleToWatchMode(filename) {
@@ -379,13 +380,16 @@ function readPackageScope(checkPath) {
379380
do {
380381
separatorIndex = StringPrototypeLastIndexOf(checkPath, sep);
381382
checkPath = StringPrototypeSlice(checkPath, 0, separatorIndex);
382-
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
383+
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules')) {
383384
return false;
385+
}
384386
const pjson = _readPackage(checkPath + sep);
385-
if (pjson.exists) return {
386-
data: pjson,
387-
path: checkPath,
388-
};
387+
if (pjson.exists) {
388+
return {
389+
data: pjson,
390+
path: checkPath,
391+
};
392+
}
389393
} while (separatorIndex > rootSeparatorIndex);
390394
return false;
391395
}
@@ -438,7 +442,7 @@ const realpathCache = new SafeMap();
438442
// absolute realpath.
439443
function tryFile(requestPath, isMain) {
440444
const rc = _stat(requestPath);
441-
if (rc !== 0) return;
445+
if (rc !== 0) { return; }
442446
if (getOptionValue('--preserve-symlinks') && !isMain) {
443447
return path.resolve(requestPath);
444448
}
@@ -472,15 +476,15 @@ function findLongestRegisteredExtension(filename) {
472476
let startIndex = 0;
473477
while ((index = StringPrototypeIndexOf(name, '.', startIndex)) !== -1) {
474478
startIndex = index + 1;
475-
if (index === 0) continue; // Skip dotfiles like .gitignore
479+
if (index === 0) { continue; } // Skip dotfiles like .gitignore
476480
currentExtension = StringPrototypeSlice(name, index);
477-
if (Module._extensions[currentExtension]) return currentExtension;
481+
if (Module._extensions[currentExtension]) { return currentExtension; }
478482
}
479483
return '.js';
480484
}
481485

482486
function trySelfParentPath(parent) {
483-
if (!parent) return false;
487+
if (!parent) { return false; }
484488

485489
if (parent.filename) {
486490
return parent.filename;
@@ -494,7 +498,7 @@ function trySelfParentPath(parent) {
494498
}
495499

496500
function trySelf(parentPath, request) {
497-
if (!parentPath) return false;
501+
if (!parentPath) { return false; }
498502

499503
const { data: pkg, path: pkgPath } = readPackageScope(parentPath);
500504
if (!pkg || pkg.exports == null || pkg.name === undefined) {
@@ -516,8 +520,9 @@ function trySelf(parentPath, request) {
516520
pathToFileURL(pkgPath + '/package.json'), expansion, pkg,
517521
pathToFileURL(parentPath), getCjsConditions()), parentPath, pkgPath);
518522
} catch (e) {
519-
if (e.code === 'ERR_MODULE_NOT_FOUND')
523+
if (e.code === 'ERR_MODULE_NOT_FOUND') {
520524
throw createEsmNotFoundErr(request, pkgPath + '/package.json');
525+
}
521526
throw e;
522527
}
523528
}
@@ -530,8 +535,7 @@ function resolveExports(nmPath, request) {
530535
// The implementation's behavior is meant to mirror resolution in ESM.
531536
const { 1: name, 2: expansion = '' } =
532537
RegExpPrototypeExec(EXPORTS_PATTERN, request) || kEmptyObject;
533-
if (!name)
534-
return;
538+
if (!name) { return; }
535539
const pkgPath = path.resolve(nmPath, name);
536540
const pkg = _readPackage(pkgPath);
537541
if (pkg.exists && pkg.exports != null) {
@@ -541,8 +545,9 @@ function resolveExports(nmPath, request) {
541545
pathToFileURL(pkgPath + '/package.json'), '.' + expansion, pkg, null,
542546
getCjsConditions()), null, pkgPath);
543547
} catch (e) {
544-
if (e.code === 'ERR_MODULE_NOT_FOUND')
548+
if (e.code === 'ERR_MODULE_NOT_FOUND') {
545549
throw createEsmNotFoundErr(request, pkgPath + '/package.json');
550+
}
546551
throw e;
547552
}
548553
}
@@ -564,8 +569,9 @@ Module._findPath = function(request, paths, isMain) {
564569

565570
const cacheKey = request + '\x00' + ArrayPrototypeJoin(paths, '\x00');
566571
const entry = Module._pathCache[cacheKey];
567-
if (entry)
572+
if (entry) {
568573
return entry;
574+
}
569575

570576
let exts;
571577
const trailingSlash = request.length > 0 &&
@@ -603,12 +609,15 @@ Module._findPath = function(request, paths, isMain) {
603609
for (let i = 0; i < paths.length; i++) {
604610
// Don't search further if path doesn't exist and request is inside the path
605611
const curPath = paths[i];
606-
if (insidePath && curPath && _stat(curPath) < 1) continue;
612+
if (insidePath && curPath && _stat(curPath) < 1) {
613+
continue;
614+
}
607615

608616
if (!absoluteRequest) {
609617
const exportsResolved = resolveExports(curPath, request);
610-
if (exportsResolved)
618+
if (exportsResolved) {
611619
return exportsResolved;
620+
}
612621
}
613622

614623
const basePath = path.resolve(curPath, request);
@@ -640,16 +649,18 @@ Module._findPath = function(request, paths, isMain) {
640649

641650
if (!filename) {
642651
// Try it with each of the extensions
643-
if (exts === undefined)
652+
if (exts === undefined) {
644653
exts = ObjectKeys(Module._extensions);
654+
}
645655
filename = tryExtensions(basePath, exts, isMain);
646656
}
647657
}
648658

649659
if (!filename && rc === 1) { // Directory.
650660
// try it with each of the extensions at "index"
651-
if (exts === undefined)
661+
if (exts === undefined) {
652662
exts = ObjectKeys(Module._extensions);
663+
}
653664
filename = tryPackage(basePath, exts, isMain, request);
654665
}
655666

@@ -685,8 +696,9 @@ if (isWindows) {
685696
// path.resolve will make sure from.length >=3 in Windows.
686697
if (StringPrototypeCharCodeAt(from, from.length - 1) ===
687698
CHAR_BACKWARD_SLASH &&
688-
StringPrototypeCharCodeAt(from, from.length - 2) === CHAR_COLON)
699+
StringPrototypeCharCodeAt(from, from.length - 2) === CHAR_COLON) {
689700
return [from + 'node_modules'];
701+
}
690702

691703
const paths = [];
692704
for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
@@ -699,11 +711,12 @@ if (isWindows) {
699711
if (code === CHAR_BACKWARD_SLASH ||
700712
code === CHAR_FORWARD_SLASH ||
701713
code === CHAR_COLON) {
702-
if (p !== nmLen)
714+
if (p !== nmLen) {
703715
ArrayPrototypePush(
704716
paths,
705717
StringPrototypeSlice(from, 0, last) + '\\node_modules',
706718
);
719+
}
707720
last = i;
708721
p = 0;
709722
} else if (p !== -1) {
@@ -724,8 +737,9 @@ if (isWindows) {
724737
from = path.resolve(from);
725738
// Return early not only to avoid unnecessary work, but to *avoid* returning
726739
// an array of two items for a root: [ '//node_modules', '/node_modules' ]
727-
if (from === '/')
740+
if (from === '/') {
728741
return ['/node_modules'];
742+
}
729743

730744
// note: this approach *only* works when the path is guaranteed
731745
// to be absolute. Doing a fully-edge-case-correct path.split
@@ -734,11 +748,12 @@ if (isWindows) {
734748
for (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
735749
const code = StringPrototypeCharCodeAt(from, i);
736750
if (code === CHAR_FORWARD_SLASH) {
737-
if (p !== nmLen)
751+
if (p !== nmLen) {
738752
ArrayPrototypePush(
739753
paths,
740754
StringPrototypeSlice(from, 0, last) + '/node_modules',
741755
);
756+
}
742757
last = i;
743758
p = 0;
744759
} else if (p !== -1) {
@@ -815,14 +830,15 @@ const CircularRequirePrototypeWarningProxy = new Proxy({}, {
815830
// Allow __esModule access in any case because it is used in the output
816831
// of transpiled code to determine whether something comes from an
817832
// ES module, and is not used as a regular key of `module.exports`.
818-
if (prop in target || prop === '__esModule') return target[prop];
833+
if (prop in target || prop === '__esModule') { return target[prop]; }
819834
emitCircularRequireWarning(prop);
820835
return undefined;
821836
},
822837

823838
getOwnPropertyDescriptor(target, prop) {
824-
if (ObjectPrototypeHasOwnProperty(target, prop) || prop === '__esModule')
839+
if (ObjectPrototypeHasOwnProperty(target, prop) || prop === '__esModule') {
825840
return ObjectGetOwnPropertyDescriptor(target, prop);
841+
}
826842
emitCircularRequireWarning(prop);
827843
return undefined;
828844
},
@@ -866,8 +882,9 @@ Module._load = function(request, parent, isMain) {
866882
const cachedModule = Module._cache[filename];
867883
if (cachedModule !== undefined) {
868884
updateChildren(parent, cachedModule, true);
869-
if (!cachedModule.loaded)
885+
if (!cachedModule.loaded) {
870886
return getExportsForCircularRequire(cachedModule);
887+
}
871888
return cachedModule.exports;
872889
}
873890
delete relativeResolveCache[relResolveCacheIdentifier];
@@ -892,8 +909,9 @@ Module._load = function(request, parent, isMain) {
892909
updateChildren(parent, cachedModule, true);
893910
if (!cachedModule.loaded) {
894911
const parseCachedModule = cjsParseCache.get(cachedModule);
895-
if (!parseCachedModule || parseCachedModule.loaded)
912+
if (!parseCachedModule || parseCachedModule.loaded) {
896913
return getExportsForCircularRequire(cachedModule);
914+
}
897915
parseCachedModule.loaded = true;
898916
} else {
899917
return cachedModule.exports;
@@ -976,8 +994,9 @@ Module._resolveFilename = function(request, parent, isMain, options) {
976994
const lookupPaths = Module._resolveLookupPaths(request, fakeParent);
977995

978996
for (let j = 0; j < lookupPaths.length; j++) {
979-
if (!ArrayPrototypeIncludes(paths, lookupPaths[j]))
997+
if (!ArrayPrototypeIncludes(paths, lookupPaths[j])) {
980998
ArrayPrototypePush(paths, lookupPaths[j]);
999+
}
9811000
}
9821001
}
9831002
}
@@ -1001,8 +1020,9 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10011020
getCjsConditions()), parentPath,
10021021
pkg.path);
10031022
} catch (e) {
1004-
if (e.code === 'ERR_MODULE_NOT_FOUND')
1023+
if (e.code === 'ERR_MODULE_NOT_FOUND') {
10051024
throw createEsmNotFoundErr(request);
1025+
}
10061026
throw e;
10071027
}
10081028
}
@@ -1020,7 +1040,7 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10201040

10211041
// Look up the filename first, since that's the cache key.
10221042
const filename = Module._findPath(request, paths, isMain);
1023-
if (filename) return filename;
1043+
if (filename) { return filename; }
10241044
const requireStack = [];
10251045
for (let cursor = parent;
10261046
cursor;
@@ -1041,13 +1061,15 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10411061

10421062
function finalizeEsmResolution(resolved, parentPath, pkgPath) {
10431063
const { encodedSepRegEx } = require('internal/modules/esm/resolve');
1044-
if (RegExpPrototypeExec(encodedSepRegEx, resolved) !== null)
1064+
if (RegExpPrototypeExec(encodedSepRegEx, resolved) !== null) {
10451065
throw new ERR_INVALID_MODULE_SPECIFIER(
10461066
resolved, 'must not include encoded "/" or "\\" characters', parentPath);
1067+
}
10471068
const filename = fileURLToPath(resolved);
10481069
const actual = tryFile(filename);
1049-
if (actual)
1070+
if (actual) {
10501071
return actual;
1072+
}
10511073
const err = createEsmNotFoundErr(filename,
10521074
path.resolve(pkgPath, 'package.json'));
10531075
throw err;
@@ -1057,8 +1079,9 @@ function createEsmNotFoundErr(request, path) {
10571079
// eslint-disable-next-line no-restricted-syntax
10581080
const err = new Error(`Cannot find module '${request}'`);
10591081
err.code = 'MODULE_NOT_FOUND';
1060-
if (path)
1082+
if (path) {
10611083
err.path = path;
1084+
}
10621085
// TODO(BridgeAR): Add the requireStack as well.
10631086
return err;
10641087
}
@@ -1073,8 +1096,9 @@ Module.prototype.load = function(filename) {
10731096

10741097
const extension = findLongestRegisteredExtension(filename);
10751098
// allow .mjs to be overridden
1076-
if (StringPrototypeEndsWith(filename, '.mjs') && !Module._extensions['.mjs'])
1099+
if (StringPrototypeEndsWith(filename, '.mjs') && !Module._extensions['.mjs']) {
10771100
throw new ERR_REQUIRE_ESM(filename, true);
1101+
}
10781102

10791103
Module._extensions[extension](this, filename);
10801104
this.loaded = true;
@@ -1085,8 +1109,9 @@ Module.prototype.load = function(filename) {
10851109
// Preemptively cache
10861110
if ((module?.module === undefined ||
10871111
module.module.getStatus() < kEvaluated) &&
1088-
!cascadedLoader.cjsCache.has(this))
1112+
!cascadedLoader.cjsCache.has(this)) {
10891113
cascadedLoader.cjsCache.set(this, exports);
1114+
}
10901115
};
10911116

10921117
// Loads a module at the given file path. Returns that module's
@@ -1213,7 +1238,7 @@ Module.prototype._compile = function(content, filename) {
12131238
const exports = this.exports;
12141239
const thisValue = exports;
12151240
const module = this;
1216-
if (requireDepth === 0) statCache = new SafeMap();
1241+
if (requireDepth === 0) { statCache = new SafeMap(); }
12171242
if (inspectorWrapper) {
12181243
result = inspectorWrapper(compiledWrapper, thisValue, exports,
12191244
require, module, filename, dirname);
@@ -1222,7 +1247,7 @@ Module.prototype._compile = function(content, filename) {
12221247
[exports, require, module, filename, dirname]);
12231248
}
12241249
hasLoadedAnyUserCJSModule = true;
1225-
if (requireDepth === 0) statCache = null;
1250+
if (requireDepth === 0) { statCache = null; }
12261251
return result;
12271252
};
12281253

@@ -1379,8 +1404,7 @@ Module._initPaths = function() {
13791404
};
13801405

13811406
Module._preloadModules = function(requests) {
1382-
if (!ArrayIsArray(requests))
1383-
return;
1407+
if (!ArrayIsArray(requests)) { return; }
13841408

13851409
isPreloading = true;
13861410

@@ -1396,8 +1420,9 @@ Module._preloadModules = function(requests) {
13961420
throw e;
13971421
}
13981422
}
1399-
for (let n = 0; n < requests.length; n++)
1423+
for (let n = 0; n < requests.length; n++) {
14001424
internalRequire(parent, requests[n]);
1425+
}
14011426
isPreloading = false;
14021427
};
14031428

0 commit comments

Comments
 (0)