| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -119,7 +119,10 @@ function processTopLevelAwait(src) { | |||
| 119 | 119 | '^\n\n' + RegExpPrototypeSymbolReplace(/ \([^)]+\)/, e.message, ''); | |
| 120 | 120 | // V8 unexpected token errors include the token string. | |
| 121 | 121 | if (StringPrototypeEndsWith(message, 'Unexpected token')) | |
| 122 | - message += " '" + src[e.pos - wrapPrefix.length] + "'"; | ||
| 122 | + message += " '" + | ||
| 123 | + // Wrapper end may cause acorn to report error position after the source | ||
| 124 | + (src[e.pos - wrapPrefix.length] ?? src[src.length - 1]) + | ||
| 125 | + "'"; | ||
| 123 | 126 | // eslint-disable-next-line no-restricted-syntax | |
| 124 | 127 | throw new SyntaxError(message); | |
| 125 | 128 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -78,6 +78,7 @@ const { | |||
| 78 | 78 | ReflectApply, | |
| 79 | 79 | RegExp, | |
| 80 | 80 | RegExpPrototypeExec, | |
| 81 | + RegExpPrototypeSymbolReplace, | ||
| 81 | 82 | RegExpPrototypeTest, | |
| 82 | 83 | SafeSet, | |
| 83 | 84 | SafeWeakSet, | |
@@ -439,8 +440,39 @@ function REPLServer(prompt, | |||
| 439 | 440 | awaitPromise = true; | |
| 440 | 441 | } | |
| 441 | 442 | } catch (e) { | |
| 442 | - decorateErrorStack(e); | ||
| 443 | - err = e; | ||
| 443 | + let recoverableError = false; | ||
| 444 | + if (e.name === 'SyntaxError') { | ||
| 445 | + let parentURL; | ||
| 446 | + try { | ||
| 447 | + const { pathToFileURL } = require('url'); | ||
| 448 | + // Adding `/repl` prevents dynamic imports from loading relative | ||
| 449 | + // to the parent of `process.cwd()`. | ||
| 450 | + parentURL = pathToFileURL(path.join(process.cwd(), 'repl')).href; | ||
| 451 | + } catch { | ||
| 452 | + } | ||
| 453 | + | ||
| 454 | + // Remove all "await"s and attempt running the script | ||
| 455 | + // in order to detect if error is truly non recoverable | ||
| 456 | + const fallbackCode = RegExpPrototypeSymbolReplace(/\bawait\b/g, code, ''); | ||
| 457 | + try { | ||
| 458 | + vm.createScript(fallbackCode, { | ||
| 459 | + filename: file, | ||
| 460 | + displayErrors: true, | ||
| 461 | + importModuleDynamically: async (specifier) => { | ||
| 462 | + return asyncESM.ESMLoader.import(specifier, parentURL); | ||
| 463 | + } | ||
| 464 | + }); | ||
| 465 | + } catch (fallbackError) { | ||
| 466 | + if (isRecoverableError(fallbackError, fallbackCode)) { | ||
| 467 | + recoverableError = true; | ||
| 468 | + err = new Recoverable(e); | ||
| 469 | + } | ||
| 470 | + } | ||
| 471 | + } | ||
| 472 | + if (!recoverableError) { | ||
| 473 | + decorateErrorStack(e); | ||
| 474 | + err = e; | ||
| 475 | + } | ||
| 444 | 476 | } | |
| 445 | 477 | } | |
| 446 | 478 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -152,6 +152,36 @@ async function ordinaryTests() { | |||
| 152 | 152 | 'Unexpected token \'.\'', | |
| 153 | 153 | ], | |
| 154 | 154 | ], | |
| 155 | + ['for (const x of [1,2,3]) {\nawait x\n}', [ | ||
| 156 | + 'for (const x of [1,2,3]) {\r', | ||
| 157 | + '... await x\r', | ||
| 158 | + '... }\r', | ||
| 159 | + 'undefined', | ||
| 160 | + ]], | ||
| 161 | + ['for (const x of [1,2,3]) {\nawait x;\n}', [ | ||
| 162 | + 'for (const x of [1,2,3]) {\r', | ||
| 163 | + '... await x;\r', | ||
| 164 | + '... }\r', | ||
| 165 | + 'undefined', | ||
| 166 | + ]], | ||
| 167 | + ['for await (const x of [1,2,3]) {\nconsole.log(x)\n}', [ | ||
| 168 | + 'for await (const x of [1,2,3]) {\r', | ||
| 169 | + '... console.log(x)\r', | ||
| 170 | + '... }\r', | ||
| 171 | + '1', | ||
| 172 | + '2', | ||
| 173 | + '3', | ||
| 174 | + 'undefined', | ||
| 175 | + ]], | ||
| 176 | + ['for await (const x of [1,2,3]) {\nconsole.log(x);\n}', [ | ||
| 177 | + 'for await (const x of [1,2,3]) {\r', | ||
| 178 | + '... console.log(x);\r', | ||
| 179 | + '... }\r', | ||
| 180 | + '1', | ||
| 181 | + '2', | ||
| 182 | + '3', | ||
| 183 | + 'undefined', | ||
| 184 | + ]], | ||
| 155 | 185 | ]; | |
| 156 | 186 | ||
| 157 | 187 | for (const [input, expected = [`${input}\r`], options = {}] of testCases) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments