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 e13162d

Browse filesBrowse 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
Copy full SHA for e13162d

3 files changed

+14-17Lines changed: 14 additions & 17 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/helpers.js
+2Lines 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
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');
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/cjs/loader.js
+1-1Lines 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
throw err;
10481048
}
10491049
}
Collapse file

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

Copy file name to clipboardExpand all lines: lib/internal/modules/esm/translators.js
+11-16Lines 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
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.