Skip to content

Commit e13162d

Browse files
aduh95targos
authored andcommitted
module: refine enrichCJSError
PR-URL: #39507 Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent cbf6a01 commit e13162d

3 files changed

Lines changed: 14 additions & 17 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ function normalizeReferrerURL(referrer) {
187187

188188
// For error messages only - used to check if ESM syntax is in use.
189189
functionhasEsmSyntax(code){
190+
debug('Checking for ESM syntax');
190191
constparser=require('internal/deps/acorn/acorn/dist/acorn').Parser;
191192
letroot;
192193
try{
@@ -196,6 +197,7 @@ function hasEsmSyntax(code) {
196197
}
197198

198199
returnArrayPrototypeSome(root.body,(stmt)=>
200+
stmt.type==='ExportDefaultDeclaration'||
199201
stmt.type==='ExportNamedDeclaration'||
200202
stmt.type==='ImportDeclaration'||
201203
stmt.type==='ExportAllDeclaration');

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ function wrapSafe(filename, content, cjsModuleInstance) {
10431043
});
10441044
}catch(err){
10451045
if(process.mainModule===cjsModuleInstance)
1046-
enrichCJSError(err);
1046+
enrichCJSError(err,content);
10471047
throwerr;
10481048
}
10491049
}

‎lib/internal/modules/esm/translators.js‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ const {
1010
ObjectKeys,
1111
PromisePrototypeCatch,
1212
PromiseReject,
13-
RegExpPrototypeTest,
1413
SafeArrayIterator,
1514
SafeMap,
1615
SafeSet,
1716
StringPrototypeReplace,
1817
StringPrototypeSlice,
19-
StringPrototypeSplit,
2018
StringPrototypeStartsWith,
2119
SyntaxErrorPrototype,
2220
globalThis: { WebAssembly },
@@ -31,8 +29,9 @@ function lazyTypes() {
3129
const{ readFileSync }=require('fs');
3230
const{ extname, isAbsolute }=require('path');
3331
const{
32+
hasEsmSyntax,
33+
loadNativeModule,
3434
stripBOM,
35-
loadNativeModule
3635
}=require('internal/modules/cjs/helpers');
3736
const{
3837
Module: CJSModule,
@@ -152,18 +151,14 @@ translators.set('module', async function moduleStrategy(url) {
152151
returnmodule;
153152
});
154153

155-
functionenrichCJSError(err){
156-
if(err==null||ObjectGetPrototypeOf(err)!==SyntaxErrorPrototype){
157-
return;
158-
}
159-
conststack=StringPrototypeSplit(err.stack,'\n');
160-
/*
161-
* The regular expression below targets the most common import statement
162-
* usage. However, some cases are not matching, cases like import statement
163-
* after a comment block and/or after a variable definition.
164-
*/
165-
if(StringPrototypeStartsWith(err.message,'Unexpected token \'export\'')||
166-
RegExpPrototypeTest(/^\s*import(?=[{'"*])\s*(?![(])/,stack[1])){
154+
/**
155+
* @param {Error | any} err
156+
* @param {string} [content] Content of the file, if known.
157+
* @param {string} [filename] Useful only if `content` is unknown.
158+
*/
159+
functionenrichCJSError(err,content,filename){
160+
if(err!=null&&ObjectGetPrototypeOf(err)===SyntaxErrorPrototype&&
161+
hasEsmSyntax(content||readFileSync(filename,'utf-8'))){
167162
// Emit the warning synchronously because we are in the middle of handling
168163
// a SyntaxError that will throw and likely terminate the process before an
169164
// asynchronous warning would be emitted.
@@ -200,7 +195,7 @@ translators.set('commonjs', async function commonjsStrategy(url, isMain) {
200195
try{
201196
exports=CJSModule._load(filename,undefined,isMain);
202197
}catch(err){
203-
enrichCJSError(err);
198+
enrichCJSError(err,undefined,filename);
204199
throwerr;
205200
}
206201
}

0 commit comments

Comments
 (0)