Skip to content

Commit e13162d

Browse files
aduh95targos
authored andcommitted
module: refine enrichCJSError
PR-URL: #39507 Reviewed-By: Guy Bedford <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent cbf6a01 commit e13162d

File tree

3 files changed

+14
-17
lines changed

3 files changed

+14
-17
lines changed

Diff for: lib/internal/modules/cjs/helpers.js

+2
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
function hasEsmSyntax(code) {
190+
debug('Checking for ESM syntax');
190191
const parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
191192
let root;
192193
try {
@@ -196,6 +197,7 @@ function hasEsmSyntax(code) {
196197
}
197198

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

Diff for: lib/internal/modules/cjs/loader.js

+1-1
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
throw err;
10481048
}
10491049
}

Diff for: lib/internal/modules/esm/translators.js

+11-16
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
return module;
153152
});
154153

155-
function enrichCJSError(err) {
156-
if (err == null || ObjectGetPrototypeOf(err) !== SyntaxErrorPrototype) {
157-
return;
158-
}
159-
const stack = 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+
function enrichCJSError(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
throw err;
205200
}
206201
}

0 commit comments

Comments
 (0)