Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fixup: pr feedback
  • Loading branch information
guybedford committed Jun 30, 2021
commit 0cb7f22b10b4feb766da0b0e0c50c9958f3e9e9b
20 changes: 11 additions & 9 deletions lib/internal/repl/await.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ const {
ArrayPrototypePush,
FunctionPrototype,
ObjectKeys,
RegExpPrototypeSymbolReplace,
StringPrototypeEndsWith,
StringPrototypeIncludes,
StringPrototypeReplace,
StringPrototypeIndexOf,
StringPrototypeRepeat,
StringPrototypeSplit,
SyntaxError,
} = primordials;
Expand Down Expand Up @@ -85,8 +87,8 @@ for (const nodeType of ObjectKeys(walk.base)) {
}

function processTopLevelAwait(src) {
const wrapPrefix = '(async () => {\n';
const wrapped = `${wrapPrefix}${src}\n})()`;
const wrapPrefix = '(async () => { ';
const wrapped = `${wrapPrefix}${src} })()`;
const wrappedArray = ArrayFrom(wrapped);
let root;
try {
Expand All @@ -95,7 +97,7 @@ function processTopLevelAwait(src) {
// If the parse error is before the first "await", then use the execution
// error. Otherwise we must emit this parse error, making it look like a
// proper syntax error.
const awaitPos = src.indexOf('await');
const awaitPos = StringPrototypeIndexOf(src, 'await');
const errPos = e.pos - wrapPrefix.length;
if (awaitPos > errPos)
return null;
Expand All @@ -107,11 +109,11 @@ function processTopLevelAwait(src) {
if (errPos === awaitPos + 7 &&
StringPrototypeIncludes(e.message, 'Unexpected token'))
return null;
const { line, column } = e.loc;
let message = '\n' + StringPrototypeSplit(src, '\n')[line - 2] + '\n';
let i = 0;
while (i++ < column) message += ' ';
message += '^\n\n' + StringPrototypeReplace(e.message, / \([^)]+\)/, '');
const line = e.loc.line;
const column = line === 1 ? e.loc.column - wrapPrefix.length : e.loc.column;
let message = '\n' + StringPrototypeSplit(src, '\n')[line - 1] + '\n' +
StringPrototypeRepeat(' ', column) +
'^\n\n' + RegExpPrototypeSymbolReplace(/ \([^)]+\)/, e.message, '');
// V8 unexpected token errors include the token string.
if (StringPrototypeEndsWith(message, 'Unexpected token'))
message += " '" + src[e.pos - wrapPrefix.length] + "'";
Expand Down
40 changes: 20 additions & 20 deletions test/parallel/test-repl-preprocess-top-level-await.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ const testCases = [
[ '0',
null ],
[ 'await 0',
'(async () => {\nreturn (await 0)\n})()' ],
'(async () => { return (await 0) })()' ],
[ 'await 0;',
'(async () => {\nreturn (await 0);\n})()' ],
'(async () => { return (await 0); })()' ],
[ '(await 0)',
'(async () => {\nreturn ((await 0))\n})()' ],
'(async () => { return ((await 0)) })()' ],
[ '(await 0);',
'(async () => {\nreturn ((await 0));\n})()' ],
'(async () => { return ((await 0)); })()' ],
[ 'async function foo() { await 0; }',
null ],
[ 'async () => await 0',
Expand All @@ -29,38 +29,38 @@ const testCases = [
[ 'await 0; return 0;',
null ],
[ 'var a = await 1',
'(async () => {\nvoid (a = await 1)\n})()' ],
'(async () => { void (a = await 1) })()' ],
[ 'let a = await 1',
'(async () => {\nvoid (a = await 1)\n})()' ],
'(async () => { void (a = await 1) })()' ],
[ 'const a = await 1',
'(async () => {\nvoid (a = await 1)\n})()' ],
'(async () => { void (a = await 1) })()' ],
[ 'for (var i = 0; i < 1; ++i) { await i }',
'(async () => {\nfor (void (i = 0); i < 1; ++i) { await i }\n})()' ],
'(async () => { for (void (i = 0); i < 1; ++i) { await i } })()' ],
[ 'for (let i = 0; i < 1; ++i) { await i }',
'(async () => {\nfor (let i = 0; i < 1; ++i) { await i }\n})()' ],
'(async () => { for (let i = 0; i < 1; ++i) { await i } })()' ],
[ 'var {a} = {a:1}, [b] = [1], {c:{d}} = {c:{d: await 1}}',
'(async () => {\nvoid ( ({a} = {a:1}), ([b] = [1]), ' +
'({c:{d}} = {c:{d: await 1}}))\n})()' ],
'(async () => { void ( ({a} = {a:1}), ([b] = [1]), ' +
'({c:{d}} = {c:{d: await 1}})) })()' ],
/* eslint-disable no-template-curly-in-string */
[ 'console.log(`${(await { a: 1 }).a}`)',
'(async () => {\nreturn (console.log(`${(await { a: 1 }).a}`))\n})()' ],
'(async () => { return (console.log(`${(await { a: 1 }).a}`)) })()' ],
/* eslint-enable no-template-curly-in-string */
[ 'await 0; function foo() {}',
'(async () => {\nawait 0; foo=function foo() {}\n})()' ],
'(async () => { await 0; foo=function foo() {} })()' ],
[ 'await 0; class Foo {}',
'(async () => {\nawait 0; Foo=class Foo {}\n})()' ],
'(async () => { await 0; Foo=class Foo {} })()' ],
[ 'if (await true) { function foo() {} }',
'(async () => {\nif (await true) { foo=function foo() {} }\n})()' ],
'(async () => { if (await true) { foo=function foo() {} } })()' ],
[ 'if (await true) { class Foo{} }',
'(async () => {\nif (await true) { class Foo{} }\n})()' ],
'(async () => { if (await true) { class Foo{} } })()' ],
[ 'if (await true) { var a = 1; }',
'(async () => {\nif (await true) { void (a = 1); }\n})()' ],
'(async () => { if (await true) { void (a = 1); } })()' ],
[ 'if (await true) { let a = 1; }',
'(async () => {\nif (await true) { let a = 1; }\n})()' ],
'(async () => { if (await true) { let a = 1; } })()' ],
[ 'var a = await 1; let b = 2; const c = 3;',
'(async () => {\nvoid (a = await 1); void (b = 2); void (c = 3);\n})()' ],
'(async () => { void (a = await 1); void (b = 2); void (c = 3); })()' ],
[ 'let o = await 1, p',
'(async () => {\nvoid ( (o = await 1), (p=undefined))\n})()' ],
'(async () => { void ( (o = await 1), (p=undefined)) })()' ],
];

for (const [input, expected] of testCases) {
Expand Down