Skip to content

Commit

Permalink
module: refine enrichCJSError
Browse files Browse the repository at this point in the history
PR-URL: #39507
Reviewed-By: Guy Bedford <guybedford@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Aug 2, 2021
1 parent cbf6a01 commit e13162d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/internal/modules/cjs/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function normalizeReferrerURL(referrer) {

// For error messages only - used to check if ESM syntax is in use.
function hasEsmSyntax(code) {
debug('Checking for ESM syntax');
const parser = require('internal/deps/acorn/acorn/dist/acorn').Parser;
let root;
try {
Expand All @@ -196,6 +197,7 @@ function hasEsmSyntax(code) {
}

return ArrayPrototypeSome(root.body, (stmt) =>
stmt.type === 'ExportDefaultDeclaration' ||
stmt.type === 'ExportNamedDeclaration' ||
stmt.type === 'ImportDeclaration' ||
stmt.type === 'ExportAllDeclaration');
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,7 @@ function wrapSafe(filename, content, cjsModuleInstance) {
});
} catch (err) {
if (process.mainModule === cjsModuleInstance)
enrichCJSError(err);
enrichCJSError(err, content);
throw err;
}
}
Expand Down
27 changes: 11 additions & 16 deletions lib/internal/modules/esm/translators.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ const {
ObjectKeys,
PromisePrototypeCatch,
PromiseReject,
RegExpPrototypeTest,
SafeArrayIterator,
SafeMap,
SafeSet,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeSplit,
StringPrototypeStartsWith,
SyntaxErrorPrototype,
globalThis: { WebAssembly },
Expand All @@ -31,8 +29,9 @@ function lazyTypes() {
const { readFileSync } = require('fs');
const { extname, isAbsolute } = require('path');
const {
hasEsmSyntax,
loadNativeModule,
stripBOM,
loadNativeModule
} = require('internal/modules/cjs/helpers');
const {
Module: CJSModule,
Expand Down Expand Up @@ -152,18 +151,14 @@ translators.set('module', async function moduleStrategy(url) {
return module;
});

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

0 comments on commit e13162d

Please sign in to comment.