Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Parse for-await statements when asyncGenerators plugin is active #17

Merged
merged 1 commit into from Apr 5, 2016
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 26 additions & 6 deletions src/parser/statement.js
Expand Up @@ -224,9 +224,18 @@ pp.parseDoStatement = function (node) {
pp.parseForStatement = function (node) {
this.next();
this.state.labels.push(loopLabel);

let forAwait = false;
if (this.hasPlugin("asyncGenerators") && this.state.inAsync && this.isContextual("await")) {
forAwait = true;
this.next();
}
this.expect(tt.parenL);

if (this.match(tt.semi)) {
if (forAwait) {
this.unexpected();
}
return this.parseFor(node, null);
}

Expand All @@ -238,10 +247,12 @@ pp.parseForStatement = function (node) {

if (this.match(tt._in) || this.isContextual("of")) {
if (init.declarations.length === 1 && !init.declarations[0].init) {
return this.parseForIn(node, init);
return this.parseForIn(node, init, forAwait);
}
}

if (forAwait) {
this.unexpected();
}
return this.parseFor(node, init);
}

Expand All @@ -250,10 +261,13 @@ pp.parseForStatement = function (node) {
if (this.match(tt._in) || this.isContextual("of")) {
this.toAssignable(init);
this.checkLVal(init);
return this.parseForIn(node, init);
return this.parseForIn(node, init, forAwait);
} else if (refShorthandDefaultPos.start) {
this.unexpected(refShorthandDefaultPos.start);
}
if (forAwait) {
this.unexpected();
}
return this.parseFor(node, init);
};

Expand Down Expand Up @@ -508,9 +522,15 @@ pp.parseFor = function (node, init) {
// Parse a `for`/`in` and `for`/`of` loop, which are almost
// same from parser's perspective.

pp.parseForIn = function (node, init) {
let type = this.match(tt._in) ? "ForInStatement" : "ForOfStatement";
this.next();
pp.parseForIn = function (node, init, forAwait) {
let type;
if (forAwait) {
this.eatContextual("of");
type = "ForAwaitStatement";
} else {
type = this.match(tt._in) ? "ForInStatement" : "ForOfStatement";
this.next();
}
node.left = init;
node.right = this.parseExpression();
this.expect(tt.parenR);
Expand Down
@@ -0,0 +1,3 @@
function f() {
for await (let x of y);
}
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (2:6)"
}
@@ -0,0 +1,3 @@
async function f() {
for await (let x in y);
}
@@ -0,0 +1,184 @@
{
"type": "File",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 15,
"end": 16,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 16
}
},
"name": "f"
},
"generator": false,
"expression": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 19,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 19
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ForAwaitStatement",
"start": 23,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 25
}
},
"await": true,
"left": {
"type": "VariableDeclaration",
"start": 34,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 13
},
"end": {
"line": 2,
"column": 18
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 38,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 18
}
},
"id": {
"type": "Identifier",
"start": 38,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 18
}
},
"name": "x"
},
"init": null
}
],
"kind": "let"
},
"right": {
"type": "Identifier",
"start": 43,
"end": 44,
"loc": {
"start": {
"line": 2,
"column": 22
},
"end": {
"line": 2,
"column": 23
}
},
"name": "y"
},
"body": {
"type": "EmptyStatement",
"start": 45,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 24
},
"end": {
"line": 2,
"column": 25
}
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (2:19)"
}
@@ -0,0 +1,3 @@
async function f() {
for await (;false;);
}
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (2:13)"
}
@@ -0,0 +1,3 @@
async function f() {
for await (let i = 0;false;);
}
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (2:22)"
}
@@ -0,0 +1,3 @@
async function f() {
for await (x = 0;false;);
}
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token (2:18)"
}
@@ -0,0 +1,3 @@
async function f() {
for await (let x of y);
}