Skip to content

Commit 1fdf727

Browse files
targosBethGriggs
authored andcommittedDec 15, 2020
module: only try to enrich CJS syntax errors
It is guaranteed that V8 throws a syntax error when `import` or `export` is used outside of ESM. Fixes: #35687 PR-URL: #35691 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Guy Bedford <guybedford@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 4937a34 commit 1fdf727

10 files changed

+69
-0
lines changed
 

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

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
const {
66
Boolean,
77
JSONParse,
8+
ObjectGetPrototypeOf,
89
ObjectPrototypeHasOwnProperty,
910
ObjectKeys,
1011
PromisePrototypeCatch,
@@ -15,6 +16,7 @@ const {
1516
StringPrototypeReplace,
1617
StringPrototypeSplit,
1718
StringPrototypeStartsWith,
19+
SyntaxErrorPrototype,
1820
} = primordials;
1921

2022
let _TYPES = null;
@@ -147,6 +149,9 @@ translators.set('module', async function moduleStrategy(url) {
147149
});
148150

149151
function enrichCJSError(err) {
152+
if (err == null || ObjectGetPrototypeOf(err) !== SyntaxErrorPrototype) {
153+
return;
154+
}
150155
const stack = StringPrototypeSplit(err.stack, '\n');
151156
/*
152157
* The regular expression below targets the most common import statement

‎test/es-module/test-esm-cjs-load-error-note.mjs

+56
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ const Import2 = fixtures.path('/es-modules/es-note-promiserej-import-2.cjs');
1010
const Import3 = fixtures.path('/es-modules/es-note-unexpected-import-3.cjs');
1111
const Import4 = fixtures.path('/es-modules/es-note-unexpected-import-4.cjs');
1212
const Import5 = fixtures.path('/es-modules/es-note-unexpected-import-5.cjs');
13+
const Error1 = fixtures.path('/es-modules/es-note-error-1.mjs');
14+
const Error2 = fixtures.path('/es-modules/es-note-error-2.mjs');
15+
const Error3 = fixtures.path('/es-modules/es-note-error-3.mjs');
16+
const Error4 = fixtures.path('/es-modules/es-note-error-4.mjs');
1317

1418
// Expect note to be included in the error output
1519
const expectedNote = 'To load an ES module, ' +
@@ -106,3 +110,55 @@ pImport5.on('close', mustCall((code) => {
106110
assert.ok(!pImport5Stderr.includes(expectedNote),
107111
`${expectedNote} must not be included in ${pImport5Stderr}`);
108112
}));
113+
114+
const pError1 = spawn(process.execPath, [Error1]);
115+
let pError1Stderr = '';
116+
pError1.stderr.setEncoding('utf8');
117+
pError1.stderr.on('data', (data) => {
118+
pError1Stderr += data;
119+
});
120+
pError1.on('close', mustCall((code) => {
121+
assert.strictEqual(code, expectedCode);
122+
assert.ok(pError1Stderr.includes('Error: some error'));
123+
assert.ok(!pError1Stderr.includes(expectedNote),
124+
`${expectedNote} must not be included in ${pError1Stderr}`);
125+
}));
126+
127+
const pError2 = spawn(process.execPath, [Error2]);
128+
let pError2Stderr = '';
129+
pError2.stderr.setEncoding('utf8');
130+
pError2.stderr.on('data', (data) => {
131+
pError2Stderr += data;
132+
});
133+
pError2.on('close', mustCall((code) => {
134+
assert.strictEqual(code, expectedCode);
135+
assert.ok(pError2Stderr.includes('string'));
136+
assert.ok(!pError2Stderr.includes(expectedNote),
137+
`${expectedNote} must not be included in ${pError2Stderr}`);
138+
}));
139+
140+
const pError3 = spawn(process.execPath, [Error3]);
141+
let pError3Stderr = '';
142+
pError3.stderr.setEncoding('utf8');
143+
pError3.stderr.on('data', (data) => {
144+
pError3Stderr += data;
145+
});
146+
pError3.on('close', mustCall((code) => {
147+
assert.strictEqual(code, expectedCode);
148+
assert.ok(pError3Stderr.includes('null'));
149+
assert.ok(!pError3Stderr.includes(expectedNote),
150+
`${expectedNote} must not be included in ${pError3Stderr}`);
151+
}));
152+
153+
const pError4 = spawn(process.execPath, [Error4]);
154+
let pError4Stderr = '';
155+
pError4.stderr.setEncoding('utf8');
156+
pError4.stderr.on('data', (data) => {
157+
pError4Stderr += data;
158+
});
159+
pError4.on('close', mustCall((code) => {
160+
assert.strictEqual(code, expectedCode);
161+
assert.ok(pError4Stderr.includes('undefined'));
162+
assert.ok(!pError4Stderr.includes(expectedNote),
163+
`${expectedNote} must not be included in ${pError4Stderr}`);
164+
}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw new Error('some error');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './es-note-error-1.cjs';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw 'string';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './es-note-error-2.cjs';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './es-note-error-3.cjs';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
throw undefined;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './es-note-error-4.cjs';

0 commit comments

Comments
 (0)
Please sign in to comment.