| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 63299a4 commit a7b92e0
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,9 +2,9 @@ | |||
| 2 | 2 | ||
| 3 | 3 | const { | |
| 4 | 4 | Array, | |
| 5 | + ArrayPrototypeFind, | ||
| 5 | 6 | ArrayPrototypeJoin, | |
| 6 | 7 | ArrayPrototypePush, | |
| 7 | - ArrayPrototypeSome, | ||
| 8 | 8 | FunctionPrototype, | |
| 9 | 9 | ObjectSetPrototypeOf, | |
| 10 | 10 | PromisePrototypeThen, | |
@@ -65,8 +65,8 @@ const CJSGlobalLike = [ | |||
| 65 | 65 | '__filename', | |
| 66 | 66 | '__dirname', | |
| 67 | 67 | ]; | |
| 68 | - const isCommonJSGlobalLikeNotDefinedError = (errorMessage) => | ||
| 69 | - ArrayPrototypeSome( | ||
| 68 | + const findCommonJSGlobalLikeNotDefinedError = (errorMessage) => | ||
| 69 | + ArrayPrototypeFind( | ||
| 70 | 70 | CJSGlobalLike, | |
| 71 | 71 | (globalLike) => errorMessage === `${globalLike} is not defined`, | |
| 72 | 72 | ); | |
@@ -79,11 +79,28 @@ const isCommonJSGlobalLikeNotDefinedError = (errorMessage) => | |||
| 79 | 79 | * @returns {void} | |
| 80 | 80 | */ | |
| 81 | 81 | const explainCommonJSGlobalLikeNotDefinedError = (e, url, hasTopLevelAwait) => { | |
| 82 | - if (e?.name === 'ReferenceError' && | ||
| 83 | - isCommonJSGlobalLikeNotDefinedError(e.message)) { | ||
| 82 | + const notDefinedGlobalLike = e?.name === 'ReferenceError' && findCommonJSGlobalLikeNotDefinedError(e.message); | ||
| 84 | 83 | ||
| 84 | + if (notDefinedGlobalLike) { | ||
| 85 | 85 | if (hasTopLevelAwait) { | |
| 86 | - e.message = `Cannot determine intended module format because both require() 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, replace require() with import.`; | ||
| 86 | + let advice; | ||
| 87 | + switch (notDefinedGlobalLike) { | ||
| 88 | + case 'require': | ||
| 89 | + advice = 'replace require() with import'; | ||
| 90 | + break; | ||
| 91 | + case 'module': | ||
| 92 | + case 'exports': | ||
| 93 | + advice = 'use export instead of module.exports/exports'; | ||
| 94 | + break; | ||
| 95 | + case '__filename': | ||
| 96 | + advice = 'use import.meta.filename instead'; | ||
| 97 | + break; | ||
| 98 | + case '__dirname': | ||
| 99 | + advice = 'use import.meta.dirname instead'; | ||
| 100 | + break; | ||
| 101 | + } | ||
| 102 | + | ||
| 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}.`; | ||
| 87 | 104 | e.code = 'ERR_AMBIGUOUS_MODULE_SYNTAX'; | |
| 88 | 105 | return; | |
| 89 | 106 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -283,7 +283,7 @@ describe('Module syntax detection', { concurrency: !process.env.TEST_PARALLEL }, | |||
| 283 | 283 | ||
| 284 | 284 | assert.match( | |
| 285 | 285 | stderr, | |
| 286 | - /ReferenceError: Cannot determine intended module format because both require\(\) 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, replace require\(\) with import\./ | ||
| 286 | + /ReferenceError: Cannot determine intended module format because both 'require' 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, replace require\(\) with import\./ | ||
| 287 | 287 | ); | |
| 288 | 288 | assert.strictEqual(stdout, ''); | |
| 289 | 289 | assert.strictEqual(code, 1); | |
@@ -440,7 +440,61 @@ describe('cjs & esm ambiguous syntax case', () => { | |||
| 440 | 440 | ||
| 441 | 441 | assert.match( | |
| 442 | 442 | stderr, | |
| 443 | - /ReferenceError: Cannot determine intended module format because both require\(\) 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, replace require\(\) with import\./ | ||
| 443 | + /ReferenceError: Cannot determine intended module format because both 'require' 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, replace require\(\) with import\./ | ||
| 444 | + ); | ||
| 445 | + | ||
| 446 | + assert.strictEqual(code, 1); | ||
| 447 | + assert.strictEqual(signal, null); | ||
| 448 | + }); | ||
| 449 | + | ||
| 450 | + it('should throw an ambiguous syntax error when using top-level await with exports', async () => { | ||
| 451 | + const { stderr, code, signal } = await spawnPromisified( | ||
| 452 | + process.execPath, | ||
| 453 | + [ | ||
| 454 | + '--eval', | ||
| 455 | + `exports.foo = 'bar';\nawait 1;`, | ||
| 456 | + ] | ||
| 457 | + ); | ||
| 458 | + | ||
| 459 | + assert.match( | ||
| 460 | + stderr, | ||
| 461 | + /ReferenceError: Cannot determine intended module format because both 'exports' 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, use export instead of module\.exports\/exports\./ | ||
| 462 | + ); | ||
| 463 | + | ||
| 464 | + assert.strictEqual(code, 1); | ||
| 465 | + assert.strictEqual(signal, null); | ||
| 466 | + }); | ||
| 467 | + | ||
| 468 | + it('should throw an ambiguous syntax error when using top-level await with __filename', async () => { | ||
| 469 | + const { stderr, code, signal } = await spawnPromisified( | ||
| 470 | + process.execPath, | ||
| 471 | + [ | ||
| 472 | + '--eval', | ||
| 473 | + `console.log(__filename);\nawait 1;`, | ||
| 474 | + ] | ||
| 475 | + ); | ||
| 476 | + | ||
| 477 | + assert.match( | ||
| 478 | + stderr, | ||
| 479 | + /ReferenceError: Cannot determine intended module format because both '__filename' 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, use import\.meta\.filename instead\./ | ||
| 480 | + ); | ||
| 481 | + | ||
| 482 | + assert.strictEqual(code, 1); | ||
| 483 | + assert.strictEqual(signal, null); | ||
| 484 | + }); | ||
| 485 | + | ||
| 486 | + it('should throw an ambiguous syntax error when using top-level await with __dirname', async () => { | ||
| 487 | + const { stderr, code, signal } = await spawnPromisified( | ||
| 488 | + process.execPath, | ||
| 489 | + [ | ||
| 490 | + '--eval', | ||
| 491 | + `console.log(__dirname);\nawait 1;`, | ||
| 492 | + ] | ||
| 493 | + ); | ||
| 494 | + | ||
| 495 | + assert.match( | ||
| 496 | + stderr, | ||
| 497 | + /ReferenceError: Cannot determine intended module format because both '__dirname' 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, use import\.meta\.dirname instead\./ | ||
| 444 | 498 | ); | |
| 445 | 499 | ||
| 446 | 500 | assert.strictEqual(code, 1); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments