forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Much of the AST visitor code was ported from Chrome DevTools code written by Aleksey Kozyatinskiy <kozyatinskiy@chromium.org>. PR-URL: nodejs#15566 Fixes: nodejs#13209 Refs: https://chromium.googlesource.com/chromium/src/+/e8111c396fef38da6654093433b4be93bed01dce Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
- Loading branch information
Showing
5 changed files
with
449 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
'use strict'; | ||
|
||
const acorn = require('internal/deps/acorn/dist/acorn'); | ||
const walk = require('internal/deps/acorn/dist/walk'); | ||
|
||
const noop = () => {}; | ||
const visitorsWithoutAncestors = { | ||
ClassDeclaration(node, state, c) { | ||
if (state.ancestors[state.ancestors.length - 2] === state.body) { | ||
state.prepend(node, `${node.id.name}=`); | ||
} | ||
walk.base.ClassDeclaration(node, state, c); | ||
}, | ||
FunctionDeclaration(node, state, c) { | ||
state.prepend(node, `${node.id.name}=`); | ||
}, | ||
FunctionExpression: noop, | ||
ArrowFunctionExpression: noop, | ||
MethodDefinition: noop, | ||
AwaitExpression(node, state, c) { | ||
state.containsAwait = true; | ||
walk.base.AwaitExpression(node, state, c); | ||
}, | ||
ReturnStatement(node, state, c) { | ||
state.containsReturn = true; | ||
walk.base.ReturnStatement(node, state, c); | ||
}, | ||
VariableDeclaration(node, state, c) { | ||
if (node.kind === 'var' || | ||
state.ancestors[state.ancestors.length - 2] === state.body) { | ||
if (node.declarations.length === 1) { | ||
state.replace(node.start, node.start + node.kind.length, 'void'); | ||
} else { | ||
state.replace(node.start, node.start + node.kind.length, 'void ('); | ||
} | ||
|
||
for (const decl of node.declarations) { | ||
state.prepend(decl, '('); | ||
state.append(decl, decl.init ? ')' : '=undefined)'); | ||
} | ||
|
||
if (node.declarations.length !== 1) { | ||
state.append(node.declarations[node.declarations.length - 1], ')'); | ||
} | ||
} | ||
|
||
walk.base.VariableDeclaration(node, state, c); | ||
} | ||
}; | ||
|
||
const visitors = {}; | ||
for (const nodeType of Object.keys(walk.base)) { | ||
const callback = visitorsWithoutAncestors[nodeType] || walk.base[nodeType]; | ||
visitors[nodeType] = (node, state, c) => { | ||
const isNew = node !== state.ancestors[state.ancestors.length - 1]; | ||
if (isNew) { | ||
state.ancestors.push(node); | ||
} | ||
callback(node, state, c); | ||
if (isNew) { | ||
state.ancestors.pop(); | ||
} | ||
}; | ||
} | ||
|
||
function processTopLevelAwait(src) { | ||
const wrapped = `(async () => { ${src} })()`; | ||
const wrappedArray = wrapped.split(''); | ||
let root; | ||
try { | ||
root = acorn.parse(wrapped, { ecmaVersion: 8 }); | ||
} catch (err) { | ||
return null; | ||
} | ||
const body = root.body[0].expression.callee.body; | ||
const state = { | ||
body, | ||
ancestors: [], | ||
replace(from, to, str) { | ||
for (var i = from; i < to; i++) { | ||
wrappedArray[i] = ''; | ||
} | ||
if (from === to) str += wrappedArray[from]; | ||
wrappedArray[from] = str; | ||
}, | ||
prepend(node, str) { | ||
wrappedArray[node.start] = str + wrappedArray[node.start]; | ||
}, | ||
append(node, str) { | ||
wrappedArray[node.end - 1] += str; | ||
}, | ||
containsAwait: false, | ||
containsReturn: false | ||
}; | ||
|
||
walk.recursive(body, state, visitors); | ||
|
||
// Do not transform if | ||
// 1. False alarm: there isn't actually an await expression. | ||
// 2. There is a top-level return, which is not allowed. | ||
if (!state.containsAwait || state.containsReturn) { | ||
return null; | ||
} | ||
|
||
const last = body.body[body.body.length - 1]; | ||
if (last.type === 'ExpressionStatement') { | ||
// For an expression statement of the form | ||
// ( expr ) ; | ||
// ^^^^^^^^^^ // last | ||
// ^^^^ // last.expression | ||
// | ||
// We do not want the left parenthesis before the `return` keyword; | ||
// therefore we prepend the `return (` to `last`. | ||
// | ||
// On the other hand, we do not want the right parenthesis after the | ||
// semicolon. Since there can only be more right parentheses between | ||
// last.expression.end and the semicolon, appending one more to | ||
// last.expression should be fine. | ||
state.prepend(last, 'return ('); | ||
state.append(last.expression, ')'); | ||
} | ||
|
||
return wrappedArray.join(''); | ||
} | ||
|
||
module.exports = { | ||
processTopLevelAwait | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const { processTopLevelAwait } = require('internal/repl/await'); | ||
|
||
// Flags: --expose-internals | ||
|
||
// This test was created based on | ||
// https://cs.chromium.org/chromium/src/third_party/WebKit/LayoutTests/http/tests/inspector-unit/preprocess-top-level-awaits.js?rcl=358caaba5e763e71c4abb9ada2d9cd8b1188cac9 | ||
|
||
const testCases = [ | ||
[ '0', | ||
null ], | ||
[ 'await 0', | ||
'(async () => { return (await 0) })()' ], | ||
[ 'await 0;', | ||
'(async () => { return (await 0); })()' ], | ||
[ '(await 0)', | ||
'(async () => { return ((await 0)) })()' ], | ||
[ '(await 0);', | ||
'(async () => { return ((await 0)); })()' ], | ||
[ 'async function foo() { await 0; }', | ||
null ], | ||
[ 'async () => await 0', | ||
null ], | ||
[ 'class A { async method() { await 0 } }', | ||
null ], | ||
[ 'await 0; return 0;', | ||
null ], | ||
[ 'var a = await 1', | ||
'(async () => { void (a = await 1) })()' ], | ||
[ 'let a = await 1', | ||
'(async () => { void (a = await 1) })()' ], | ||
[ 'const a = await 1', | ||
'(async () => { void (a = await 1) })()' ], | ||
[ 'for (var i = 0; i < 1; ++i) { await i }', | ||
'(async () => { for (void (i = 0); i < 1; ++i) { await i } })()' ], | ||
[ 'for (let i = 0; i < 1; ++i) { await i }', | ||
'(async () => { for (let i = 0; i < 1; ++i) { await i } })()' ], | ||
[ 'var {a} = {a:1}, [b] = [1], {c:{d}} = {c:{d: await 1}}', | ||
'(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 () => { return (console.log(`${(await { a: 1 }).a}`)) })()' ], | ||
/* eslint-enable no-template-curly-in-string */ | ||
[ 'await 0; function foo() {}', | ||
'(async () => { await 0; foo=function foo() {} })()' ], | ||
[ 'await 0; class Foo {}', | ||
'(async () => { await 0; Foo=class Foo {} })()' ], | ||
[ 'if (await true) { function foo() {} }', | ||
'(async () => { if (await true) { foo=function foo() {} } })()' ], | ||
[ 'if (await true) { class Foo{} }', | ||
'(async () => { if (await true) { class Foo{} } })()' ], | ||
[ 'if (await true) { var a = 1; }', | ||
'(async () => { if (await true) { void (a = 1); } })()' ], | ||
[ 'if (await true) { let a = 1; }', | ||
'(async () => { if (await true) { let a = 1; } })()' ], | ||
[ 'var a = await 1; let b = 2; const c = 3;', | ||
'(async () => { void (a = await 1); void (b = 2); void (c = 3); })()' ], | ||
[ 'let o = await 1, p', | ||
'(async () => { void ( (o = await 1), (p=undefined)) })()' ] | ||
]; | ||
|
||
for (const [input, expected] of testCases) { | ||
assert.strictEqual(processTopLevelAwait(input), expected); | ||
} |
Oops, something went wrong.