Skip to content

Commit 7888512

Browse files
committed
esm: Fix linting errors and test coverage
Remove importParents guard. Use primordials instead.
1 parent d117c74 commit 7888512

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

lib/internal/modules/esm/loader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
Promise,
1111
PromisePrototypeThen,
1212
RegExpPrototypeSymbolReplace,
13+
SafeMap,
1314
encodeURIComponent,
1415
hardenRegExp,
1516
} = primordials;
@@ -195,7 +196,7 @@ class ModuleLoader {
195196

196197
constructor(asyncLoaderHooks) {
197198
this.#setAsyncLoaderHooks(asyncLoaderHooks);
198-
this.importParents = new Map();
199+
this.importParents = new SafeMap();
199200
}
200201

201202
/**

lib/internal/modules/esm/module_job.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,28 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
2424
});
2525

2626
const {
27-
overrideStackTrace,
2827
ErrorPrepareStackTrace,
29-
codes,
28+
codes: {
29+
ERR_REQUIRE_ASYNC_MODULE,
30+
},
31+
overrideStackTrace,
3032
} = require('internal/errors');
3133

32-
const { ERR_REQUIRE_ASYNC_MODULE } = codes;
33-
3434
/**
3535
* Builds a linear import trace by walking parent modules
3636
* from the module that threw during evaluation.
37+
* @returns {Array<{child: string, parent: string}>|null}
3738
*/
3839
function buildImportTrace(importParents, startURL) {
3940
const trace = [];
4041
let current = startURL;
41-
const seen = new Set([current]);
42+
const seen = new SafeSet([current]);
4243

4344
while (true) {
4445
const parent = importParents.get(current);
45-
if (!parent || seen.has(parent)) break;
46+
if (!parent || seen.has(parent)) {
47+
break;
48+
}
4649

4750
trace.push({ child: current, parent });
4851
seen.add(current);
@@ -54,6 +57,7 @@ function buildImportTrace(importParents, startURL) {
5457

5558
/**
5659
* Formats an import trace for inclusion in an error stack.
60+
* @returns {string}
5761
*/
5862
function formatImportTrace(trace) {
5963
return trace
@@ -62,12 +66,13 @@ function formatImportTrace(trace) {
6266
}
6367

6468
/**
65-
* Appends an ESM import trace to an errors stack output.
69+
* Appends an ESM import trace to an error's stack output.
6670
* Uses a per-error stack override; no global side effects.
6771
*/
6872
function decorateErrorWithImportTrace(e, importParents) {
69-
if (!importParents || typeof importParents.get !== 'function') return;
70-
if (!e || typeof e !== 'object') return;
73+
if (!e || typeof e !== 'object') {
74+
return;
75+
}
7176

7277
overrideStackTrace.set(e, (error, trace) => {
7378
let thrownURL;
@@ -84,7 +89,9 @@ function decorateErrorWithImportTrace(e, importParents) {
8489

8590
const importTrace = thrownURL ? buildImportTrace(importParents, thrownURL) : null;
8691
const stack = ErrorPrepareStackTrace(error, trace);
87-
if (!importTrace) return stack;
92+
if (!importTrace) {
93+
return stack;
94+
}
8895

8996
return `${stack}\n\nImport trace:\n${formatImportTrace(importTrace)}`;
9097
});

0 commit comments

Comments
 (0)