Correctly disambiguate / after async fuctions (#10475)

* Correctly disambiguate / after async fuctions
This commit is contained in:
Nicolò Ribaudo 2019-12-02 22:41:39 +01:00 committed by GitHub
parent 8f89167486
commit 5440ae1cae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 325 additions and 0 deletions

View File

@ -19,6 +19,7 @@
// [opp]: http://en.wikipedia.org/wiki/Operator-precedence_parser
import { types as tt, type TokenType } from "../tokenizer/types";
import { types as ct } from "../tokenizer/context";
import * as N from "../types";
import LValParser from "./lval";
import {
@ -970,6 +971,19 @@ export default class ExpressionParser extends LValParser {
this.match(tt._function) &&
!this.canInsertSemicolon()
) {
const last = this.state.context.length - 1;
if (this.state.context[last] !== ct.functionStatement) {
// Since "async" is an identifier and normally identifiers
// can't be followed by expression, the tokenizer assumes
// that "function" starts a statement.
// Fixing it in the tokenizer would mean tracking not only the
// previous token ("async"), but also the one before to know
// its beforeExpr value.
// It's easier and more efficient to adjust the context here.
throw new Error("Internal error");
}
this.state.context[last] = ct.functionExpression;
this.next();
return this.parseFunction(node, undefined, true);
} else if (

View File

@ -0,0 +1,2 @@
void async function fn() {}
/foo/g

View File

@ -0,0 +1,185 @@
{
"type": "File",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 6
}
},
"program": {
"type": "Program",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 6
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 6
}
},
"expression": {
"type": "BinaryExpression",
"start": 0,
"end": 34,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 6
}
},
"left": {
"type": "BinaryExpression",
"start": 0,
"end": 32,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 4
}
},
"left": {
"type": "UnaryExpression",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"operator": "void",
"prefix": true,
"argument": {
"type": "FunctionExpression",
"start": 5,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 5
},
"end": {
"line": 1,
"column": 27
}
},
"id": {
"type": "Identifier",
"start": 20,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 22
},
"identifierName": "fn"
},
"name": "fn"
},
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 25,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 25
},
"end": {
"line": 1,
"column": 27
}
},
"body": [],
"directives": []
}
}
},
"operator": "/",
"right": {
"type": "Identifier",
"start": 29,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 4
},
"identifierName": "foo"
},
"name": "foo"
}
},
"operator": "/",
"right": {
"type": "Identifier",
"start": 33,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 6
},
"identifierName": "g"
},
"name": "g"
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,2 @@
async function fn() {}
/foo/g

View File

@ -0,0 +1,122 @@
{
"type": "File",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 6
}
},
"program": {
"type": "Program",
"start": 0,
"end": 29,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 6
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 22
}
},
"id": {
"type": "Identifier",
"start": 15,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 17
},
"identifierName": "fn"
},
"name": "fn"
},
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 20,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 22
}
},
"body": [],
"directives": []
}
},
{
"type": "ExpressionStatement",
"start": 23,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 6
}
},
"expression": {
"type": "RegExpLiteral",
"start": 23,
"end": 29,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 6
}
},
"extra": {
"raw": "/foo/g"
},
"pattern": "foo",
"flags": "g"
}
}
],
"directives": []
}
}