Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f42a103

Browse filesBrowse files
esm: require braces for modules code
PR-URL: #49657 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 6a489df commit f42a103
Copy full SHA for f42a103

File tree

Expand file treeCollapse file tree

13 files changed

+143
-104
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

13 files changed

+143
-104
lines changed
Open diff view settings
Collapse file

‎.eslintrc.js‎

Copy file name to clipboardExpand all lines: .eslintrc.js
+10-2Lines 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',
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js
+64-41Lines 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
const result = statCache.get(filename);
176-
if (result !== undefined) return result;
176+
if (result !== undefined) { return result; }
177177
}
178178
const result = internalModuleStat(filename);
179179
if (statCache !== null && result >= 0) {
@@ -197,8 +197,9 @@ ObjectDefineProperty(Module, '_stat', {
197197

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

204205
function reportModuleToWatchMode(filename) {
@@ -386,13 +387,16 @@ function readPackageScope(checkPath) {
386387
if (enabledPermission && !permission.has('fs.read', checkPath + sep)) {
387388
return false;
388389
}
389-
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules'))
390+
if (StringPrototypeEndsWith(checkPath, sep + 'node_modules')) {
390391
return false;
392+
}
391393
const pjson = _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
return false;
398402
}
@@ -445,7 +449,7 @@ const realpathCache = new SafeMap();
445449
// absolute realpath.
446450
function tryFile(requestPath, isMain) {
447451
const rc = _stat(requestPath);
448-
if (rc !== 0) return;
452+
if (rc !== 0) { return; }
449453
if (getOptionValue('--preserve-symlinks') && !isMain) {
450454
return path.resolve(requestPath);
451455
}
@@ -479,15 +483,15 @@ function findLongestRegisteredExtension(filename) {
479483
let startIndex = 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]) return currentExtension;
488+
if (Module._extensions[currentExtension]) { return currentExtension; }
485489
}
486490
return '.js';
487491
}
488492

489493
function trySelfParentPath(parent) {
490-
if (!parent) return false;
494+
if (!parent) { return false; }
491495

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

503507
function trySelf(parentPath, request) {
504-
if (!parentPath) return false;
508+
if (!parentPath) { return false; }
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
throw createEsmNotFoundErr(request, pkgPath + '/package.json');
532+
}
528533
throw e;
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
const pkgPath = path.resolve(nmPath, name);
543547
const pkg = _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
throw createEsmNotFoundErr(request, pkgPath + '/package.json');
557+
}
553558
throw e;
554559
}
555560
}
@@ -571,8 +576,9 @@ Module._findPath = function(request, paths, isMain) {
571576

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

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

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

626633
const basePath = 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
const paths = [];
704714
for (let i = 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
} else if (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 (let i = from.length - 1, p = 0, last = from.length; i >= 0; --i) {
747759
const code = 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
} else if (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 (prop in target || prop === '__esModule') return target[prop];
843+
if (prop in target || prop === '__esModule') { return target[prop]; }
831844
emitCircularRequireWarning(prop);
832845
return undefined;
833846
},
834847

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

9901006
for (let j = 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
throw createEsmNotFoundErr(request);
1035+
}
10181036
throw e;
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
const filename = Module._findPath(request, paths, isMain);
1035-
if (filename) return filename;
1053+
if (filename) { return filename; }
10361054
const requireStack = [];
10371055
for (let cursor = parent;
10381056
cursor;
@@ -1053,13 +1071,15 @@ Module._resolveFilename = function(request, parent, isMain, options) {
10531071

10541072
function finalizeEsmResolution(resolved, parentPath, pkgPath) {
10551073
const { encodedSepRegEx } = require('internal/modules/esm/resolve');
1056-
if (RegExpPrototypeExec(encodedSepRegEx, resolved) !== null)
1074+
if (RegExpPrototypeExec(encodedSepRegEx, resolved) !== null) {
10571075
throw new ERR_INVALID_MODULE_SPECIFIER(
10581076
resolved, 'must not include encoded "/" or "\\" characters', parentPath);
1077+
}
10591078
const filename = fileURLToPath(resolved);
10601079
const actual = tryFile(filename);
1061-
if (actual)
1080+
if (actual) {
10621081
return actual;
1082+
}
10631083
const err = createEsmNotFoundErr(filename,
10641084
path.resolve(pkgPath, 'package.json'));
10651085
throw err;
@@ -1069,8 +1089,9 @@ function createEsmNotFoundErr(request, path) {
10691089
// eslint-disable-next-line no-restricted-syntax
10701090
const err = new Error(`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
return err;
10761097
}
@@ -1085,8 +1106,9 @@ Module.prototype.load = function(filename) {
10851106

10861107
const extension = 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
throw new ERR_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
const exports = this.exports;
12341257
const thisValue = exports;
12351258
const module = this;
1236-
if (requireDepth === 0) statCache = new SafeMap();
1259+
if (requireDepth === 0) { statCache = new SafeMap(); }
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
return result;
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
throw e;
14171439
}
14181440
}
1419-
for (let n = 0; n < requests.length; n++)
1441+
for (let n = 0; n < requests.length; n++) {
14201442
internalRequire(parent, requests[n]);
1443+
}
14211444
isPreloading = false;
14221445
};
14231446

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.