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

Add async/await support #129

Merged
merged 2 commits into from
Jan 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 23 additions & 0 deletions lib/ast-converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ function isESTreeClassMember(node) {
return node.kind !== SyntaxKind.SemicolonClassElement;
}

/**
* Returns true if the given node is an async function
* @param {TSNode} node TypeScript AST node
* @returns {boolean} is an async function
*/
function isAsyncFunction(node) {
return !!node.modifiers && !!node.modifiers.length && node.modifiers.some(function(modifier) {
return modifier.kind === SyntaxKind.AsyncKeyword;
});
}

/**
* Returns true if the given TSToken is a comma
* @param {TSToken} token the TypeScript token
Expand Down Expand Up @@ -852,6 +863,7 @@ module.exports = function(ast, extra) {
id: convertChild(node.name),
generator: !!node.asteriskToken,
expression: false,
async: isAsyncFunction(node),
params: node.parameters.map(convertChild),
body: convertChild(node.body)
});
Expand Down Expand Up @@ -1057,6 +1069,7 @@ module.exports = function(ast, extra) {
id: null,
generator: false,
expression: false,
async: isAsyncFunction(node),
body: convertChild(node.body),
range: [ node.name.end, result.range[1]],
loc: {
Expand Down Expand Up @@ -1158,6 +1171,7 @@ module.exports = function(ast, extra) {
}),
generator: false,
expression: false,
async: false,
body: convertChild(node.body),
range: [ result.range[0] + constructorStartOffset, result.range[1]],
loc: {
Expand Down Expand Up @@ -1226,6 +1240,7 @@ module.exports = function(ast, extra) {
generator: !!node.asteriskToken,
params: node.parameters.map(convertChild),
body: convertChild(node.body),
async: isAsyncFunction(node),
expression: false
});
// Process returnType
Expand Down Expand Up @@ -1308,6 +1323,7 @@ module.exports = function(ast, extra) {
id: null,
params: node.parameters.map(convertChild),
body: convertChild(node.body),
async: isAsyncFunction(node),
expression: node.body.kind !== SyntaxKind.Block
});
// Process returnType
Expand All @@ -1328,6 +1344,13 @@ module.exports = function(ast, extra) {
});
break;

case SyntaxKind.AwaitExpression:
assign(result, {
type: "AwaitExpression",
expression: convertChild(node.expression)
});
break;

// Template Literals

case SyntaxKind.NoSubstitutionTemplateLiteral:
Expand Down
1 change: 1 addition & 0 deletions lib/ast-node-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = {
ArrayExpression: "ArrayExpression",
ArrayPattern: "ArrayPattern",
ArrowFunctionExpression: "ArrowFunctionExpression",
AwaitExpression: "AwaitExpression",
BlockStatement: "BlockStatement",
BinaryExpression: "BinaryExpression",
BreakStatement: "BreakStatement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = {
},
"generator": false,
"expression": false,
"async": false,
"range": [
110,
119
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ module.exports = {
]
},
"expression": false,
"async": false,
"generator": false
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ module.exports = {
]
},
"expression": false,
"async": false,
"generator": false
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module.exports = {
]
},
"expression": false,
"async": false,
"generator": false
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ module.exports = {
]
},
"expression": false,
"async": false,
"generator": false
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ module.exports = {
]
},
"expression": false,
"async": false,
"generator": false
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ module.exports = {
]
},
"expression": false,
"async": false,
"generator": false
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,11 +630,13 @@ module.exports = {
]
},
"expression": false,
"async": false,
"generator": false
}
]
},
"expression": false,
"async": false,
"generator": false
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/basics/new-without-parens.result.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ module.exports = {
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/basics/update-expression.result.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,9 @@ module.exports = {
],
"type": "BlockStatement"
},
"expression": false,
"generator": false,
"expression": false,
"async": false,
"id": {
"loc": {
"end": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module.exports = {
},
"generator": true,
"expression": false,
"async": false,
"range": [
17,
24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ module.exports = {
},
"generator": true,
"expression": false,
"async": false,
"range": [
31,
38
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ module.exports = {
},
"generator": true,
"expression": false,
"async": false,
"range": [
24,
31
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ module.exports = {
},
"generator": false,
"expression": false,
"async": false,
"range": [
6,
13
Expand Down Expand Up @@ -139,6 +140,7 @@ module.exports = {
},
"generator": false,
"expression": false,
"async": false,
"range": [
0,
20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
20
Expand Down Expand Up @@ -329,4 +330,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
17
Expand Down Expand Up @@ -275,4 +276,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ module.exports = {
},
"generator": false,
"expression": false,
"async": false,
"range": [
16,
33
Expand Down Expand Up @@ -153,6 +154,7 @@ module.exports = {
},
"generator": true,
"expression": false,
"async": false,
"range": [
0,
35
Expand Down Expand Up @@ -456,4 +458,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
10
Expand Down Expand Up @@ -257,4 +258,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
15
Expand Down Expand Up @@ -366,4 +367,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
27
Expand Down Expand Up @@ -591,4 +592,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
17
Expand Down Expand Up @@ -519,4 +520,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
10
Expand Down Expand Up @@ -296,4 +297,4 @@ module.exports = {
}
}
]
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
35
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ module.exports = {
},
"generator": false,
"expression": true,
"async": false,
"range": [
0,
15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ module.exports = {
},
"generator": false,
"expression": false,
"async": false,
"range": [
0,
24
Expand Down Expand Up @@ -437,4 +438,4 @@ module.exports = {
}
}
]
};
};
Loading