Skip to content
This repository was archived by the owner on Jan 19, 2019. It is now read-only.

Commit d2cb428

Browse files
committed
Fix: handle async/await (fixes #119)
1 parent 1dc4039 commit d2cb428

File tree

4 files changed

+376
-0
lines changed

4 files changed

+376
-0
lines changed

lib/ast-converter.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ function isESTreeClassMember(node) {
105105
return node.kind !== SyntaxKind.SemicolonClassElement;
106106
}
107107

108+
/**
109+
* Returns true if the given node is an async function
110+
* @param {TSNode} node TypeScript AST node
111+
* @returns {boolean} is an async function
112+
*/
113+
function isAsyncFunction(node) {
114+
return !!node.modifiers && !!node.modifiers.length && node.modifiers.some(function(modifier) {
115+
return modifier.kind === SyntaxKind.AsyncKeyword;
116+
});
117+
}
118+
108119
/**
109120
* Returns true if the given TSToken is a comma
110121
* @param {TSToken} token the TypeScript token
@@ -852,6 +863,7 @@ module.exports = function(ast, extra) {
852863
id: convertChild(node.name),
853864
generator: !!node.asteriskToken,
854865
expression: false,
866+
async: isAsyncFunction(node),
855867
params: node.parameters.map(convertChild),
856868
body: convertChild(node.body)
857869
});
@@ -1057,6 +1069,7 @@ module.exports = function(ast, extra) {
10571069
id: null,
10581070
generator: false,
10591071
expression: false,
1072+
async: isAsyncFunction(node),
10601073
body: convertChild(node.body),
10611074
range: [ node.name.end, result.range[1]],
10621075
loc: {
@@ -1158,6 +1171,7 @@ module.exports = function(ast, extra) {
11581171
}),
11591172
generator: false,
11601173
expression: false,
1174+
async: false,
11611175
body: convertChild(node.body),
11621176
range: [ result.range[0] + constructorStartOffset, result.range[1]],
11631177
loc: {
@@ -1226,6 +1240,7 @@ module.exports = function(ast, extra) {
12261240
generator: !!node.asteriskToken,
12271241
params: node.parameters.map(convertChild),
12281242
body: convertChild(node.body),
1243+
async: isAsyncFunction(node),
12291244
expression: false
12301245
});
12311246
// Process returnType
@@ -1308,6 +1323,7 @@ module.exports = function(ast, extra) {
13081323
id: null,
13091324
params: node.parameters.map(convertChild),
13101325
body: convertChild(node.body),
1326+
async: isAsyncFunction(node),
13111327
expression: node.body.kind !== SyntaxKind.Block
13121328
});
13131329
// Process returnType
@@ -1328,6 +1344,13 @@ module.exports = function(ast, extra) {
13281344
});
13291345
break;
13301346

1347+
case SyntaxKind.AwaitExpression:
1348+
assign(result, {
1349+
type: "AwaitExpression",
1350+
expression: convertChild(node.expression)
1351+
});
1352+
break;
1353+
13311354
// Template Literals
13321355

13331356
case SyntaxKind.NoSubstitutionTemplateLiteral:

lib/ast-node-types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
ArrayExpression: "ArrayExpression",
2424
ArrayPattern: "ArrayPattern",
2525
ArrowFunctionExpression: "ArrowFunctionExpression",
26+
AwaitExpression: "AwaitExpression",
2627
BlockStatement: "BlockStatement",
2728
BinaryExpression: "BinaryExpression",
2829
BreakStatement: "BreakStatement",

0 commit comments

Comments
 (0)