-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support Async/Await based on #12 #125
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,6 +176,53 @@ export default class BlockStatement extends Node { | |
|
||
super.transpile(code, transforms); | ||
|
||
if (transforms.asyncAwait && this.isFunctionBlock && this.parent.async && this.body.length) { | ||
const first = this.body[0]; | ||
const last = this.body[this.body.length - 1]; | ||
const hasOnlyOneLine = this.body.length === 1; | ||
|
||
// TODO refactor :) | ||
if (this.parent.type === 'FunctionDeclaration') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This does not handle |
||
if (hasOnlyOneLine) { | ||
if (first.type === 'ReturnStatement') { | ||
code.insertLeft(first.argument.start, 'Promise.resolve().then(function() { '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
code.insertLeft(first.end, ' })'); | ||
} else { | ||
code.insertLeft(first.start, 'return Promise.resolve().then(function() { '); | ||
code.insertRight(last.end, ' }).then(function() {})'); | ||
} | ||
} else { | ||
code.insertLeft(first.start, 'return Promise.resolve()'); | ||
code.insertRight(last.end, '.then(function() {})'); | ||
|
||
for (let i = 0; i < this.body.length; i++) { | ||
const prev = this.body[i - 1]; | ||
const cur = this.body[i]; | ||
const next = this.body[i + 1]; | ||
|
||
if (cur.expression.type === 'AwaitExpression') { | ||
code.insertLeft(cur.start, '.then(function() { '); | ||
code.insertRight(cur.end, ' })'); | ||
} else { | ||
if (!prev || prev.expression.type === 'AwaitExpression') { | ||
code.insertLeft(cur.start, '.then(function() { '); | ||
} | ||
|
||
if (!next || next.expression.type === 'AwaitExpression') { | ||
code.insertRight(cur.end, ' })'); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} else if (this.parent.type === 'ArrowFunctionExpression') { | ||
// TODO merge with ^ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODOs should be new issues IMO, and not included in the code. |
||
// wrap the function's body in a promise | ||
code.insertLeft(first.start + 1, 'Promise.resolve().then(function() { return '); | ||
code.insertLeft(last.end, ' })'); | ||
} | ||
} | ||
|
||
if (this.createdDeclarations.length) { | ||
introStatementGenerators.push((start, prefix, suffix) => { | ||
const assignment = `${prefix}var ${this.createdDeclarations.join(', ')}${suffix}`; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
import Node from '../Node.js'; | ||
import CompileError from '../../utils/CompileError.js'; | ||
|
||
const noop = () => {}; | ||
|
||
export default class AwaitExpression extends Node { | ||
initialise(transforms) { | ||
if (transforms.asyncAwait) { | ||
CompileError.missingTransform("await", "asyncAwait", this); | ||
static removeAsync(code, transforms, async, start, callback = noop) { | ||
if (transforms.asyncAwait && async) { | ||
code.remove(start, start + 6); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Magic number 6 is mysterious here. Better as named variable, or with a comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could this be "async ".length? |
||
callback(); | ||
} | ||
super.initialise(transforms); | ||
} | ||
|
||
transpile (code, transforms) { | ||
AwaitExpression.removeAsync(code, transforms, true, this.start, () => { | ||
code.insertLeft(this.argument.start, 'return '); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This handles the expression as though it was a statement. See for example |
||
}); | ||
|
||
super.transpile(code, transforms); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module.exports = [ | ||
{ | ||
description: 'transpiles await arrow function call', | ||
input: `async () => await a()`, | ||
output: `!function() { return Promise.resolve().then(function() { return a() }); }` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the point of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It turns it from a function declaration into an expression, see: https://stackoverflow.com/questions/3755606/what-does-the-exclamation-mark-do-before-the-function As for why I'm not entirely sure. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The input is a function expression, too. |
||
}, | ||
|
||
{ | ||
description: 'transpiles await function call', | ||
input: `async function f() { await a(); }`, | ||
output: `function f() { return Promise.resolve().then(function() { return a(); }).then(function() {}) }` | ||
}, | ||
|
||
{ | ||
description: 'transpiles await function call with return statement', | ||
input: `async function f() { return await a(); }`, | ||
output: `function f() { return Promise.resolve().then(function() { return a(); }) }` | ||
}, | ||
|
||
{ | ||
description: 'transpiles await function call with more than one line of code', | ||
input: `async function f() { await a(); thing(); await a2(); stuff(); await a3(); await a4(); }`, | ||
output: `function f() { return Promise.resolve().then(function() { return a(); }) .then(function() { thing(); }) .then(function() { return a2(); }) .then(function() { stuff(); }) .then(function() { return a3(); }) .then(function() { return a4(); }).then(function() {}) }` | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is not test that verifies return values and closures
|
||
]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactor how? Who wrote this comment?
Better refactor now before merging, as later it will most likely not ever be refactored.