Skip to content

Commit 89e45e0

Browse files
committed
esm: optimize ReferenceError check
Skip function call for non-ReferenceErrors and reduce optional chaining.
1 parent 354f6a5 commit 89e45e0

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

lib/internal/modules/esm/module_job.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const CJSGlobalLike = [
6565
'__filename',
6666
'__dirname',
6767
];
68-
const getUndefinedCJSGlobalLike = (errorMessage) =>
68+
const findCommonJSGlobalLikeNotDefinedError = (errorMessage) =>
6969
ArrayPrototypeFind(
7070
CJSGlobalLike,
7171
(globalLike) => errorMessage === `${globalLike} is not defined`,
@@ -79,12 +79,12 @@ const getUndefinedCJSGlobalLike = (errorMessage) =>
7979
* @returns {void}
8080
*/
8181
const explainCommonJSGlobalLikeNotDefinedError = (e, url, hasTopLevelAwait) => {
82-
const undefinedGlobal = getUndefinedCJSGlobalLike(e?.message);
83-
if (e?.name === 'ReferenceError' && undefinedGlobal !== null) {
82+
const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message);
83+
if (notDefinedGlobalLike) {
8484

8585
if (hasTopLevelAwait) {
8686
let advice;
87-
switch (undefinedGlobal) {
87+
switch (notDefinedGlobalLike) {
8888
case 'require':
8989
advice = 'replace require() with import';
9090
break;
@@ -100,7 +100,7 @@ const explainCommonJSGlobalLikeNotDefinedError = (e, url, hasTopLevelAwait) => {
100100
break;
101101
}
102102

103-
e.message = `Cannot determine intended module format because both '${undefinedGlobal}' and top-level await are present. If the code is intended to be CommonJS, wrap await in an async function. If the code is intended to be an ES module, ${advice}.`;
103+
e.message = `Cannot determine intended module format because both '${notDefinedGlobalLike}' and top-level await are present. If the code is intended to be CommonJS, wrap await in an async function. If the code is intended to be an ES module, ${advice}.`;
104104
e.code = 'ERR_AMBIGUOUS_MODULE_SYNTAX';
105105
return;
106106
}

0 commit comments

Comments
 (0)