@@ -24,25 +24,28 @@ let debug = require('internal/util/debuglog').debuglog('esm', (fn) => {
2424} ) ;
2525
2626const {
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 */
3839function 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 */
5862function formatImportTrace ( trace ) {
5963 return trace
@@ -62,12 +66,13 @@ function formatImportTrace(trace) {
6266}
6367
6468/**
65- * Appends an ESM import trace to an error’ s 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 */
6872function 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