Fix parsing of newline between 'async' and 'function' (#8698)

This commit is contained in:
Brian Ng
2018-09-16 22:09:23 -05:00
committed by GitHub
parent aa33303112
commit 9b4b436e1f
10 changed files with 285 additions and 13 deletions

View File

@@ -3,7 +3,8 @@
import * as N from "../types";
import { types as tt, type TokenType } from "../tokenizer/types";
import ExpressionParser from "./expression";
import { lineBreak } from "../util/whitespace";
import { isIdentifierChar } from "../util/identifier";
import { lineBreak, skipWhiteSpace } from "../util/whitespace";
// Reused empty array added for node fields that are always empty.
@@ -1425,18 +1426,37 @@ export default class StatementParser extends ExpressionParser {
return this.finishNode(node, "ExportNamedDeclaration");
}
isAsyncFunction() {
if (!this.isContextual("async")) return false;
const { input, pos } = this.state;
skipWhiteSpace.lastIndex = pos;
const skip = skipWhiteSpace.exec(input);
if (!skip || !skip.length) return false;
const next = pos + skip[0].length;
return (
!lineBreak.test(input.slice(pos, next)) &&
input.slice(next, next + 8) === "function" &&
(next + 8 === input.length || !isIdentifierChar(input.charAt(next + 8)))
);
}
parseExportDefaultExpression(): N.Expression | N.Declaration {
const expr = this.startNode();
if (this.eat(tt._function)) {
return this.parseFunction(expr, true, false, false, true);
} else if (
this.isContextual("async") &&
this.lookahead().type === tt._function
) {
// async function declaration
this.eatContextual("async");
this.eat(tt._function);
return this.parseFunction(expr, true, false, true, true);
const isAsync = this.isAsyncFunction();
if (this.eat(tt._function) || isAsync) {
if (isAsync) {
this.eatContextual("async");
this.expect(tt._function);
}
return this.parseFunction(expr, true, false, isAsync, true);
} else if (this.match(tt._class)) {
return this.parseClass(expr, true, true);
} else if (this.match(tt.at)) {
@@ -1569,7 +1589,7 @@ export default class StatementParser extends ExpressionParser {
this.state.type.keyword === "let" ||
this.state.type.keyword === "function" ||
this.state.type.keyword === "class" ||
this.isContextual("async")
this.isAsyncFunction()
);
}

View File

@@ -21,6 +21,8 @@ export function isNewLine(code: number): boolean {
}
}
export const skipWhiteSpace = /(?:\s|\/\/.*|\/\*[^]*?\*\/)*/g;
// https://tc39.github.io/ecma262/#sec-white-space
export function isWhitespace(code: number): boolean {
switch (code) {

View File

@@ -1,4 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected \"function\" (1:12)"
"throws": "Unexpected token, expected \"{\" (1:7)"
}

View File

@@ -0,0 +1,2 @@
export default async
function bar() {}

View File

@@ -0,0 +1,3 @@
{
"sourceType": "module"
}

View File

@@ -0,0 +1,119 @@
{
"type": "File",
"start": 0,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 17
}
},
"program": {
"type": "Program",
"start": 0,
"end": 38,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 17
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 0,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 20
}
},
"declaration": {
"type": "Identifier",
"start": 15,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 20
},
"identifierName": "async"
},
"name": "async"
}
},
{
"type": "FunctionDeclaration",
"start": 21,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 17
}
},
"id": {
"type": "Identifier",
"start": 30,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "bar"
},
"name": "bar"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 36,
"end": 38,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 17
}
},
"body": [],
"directives": []
}
}
],
"directives": []
}
}

View File

@@ -0,0 +1 @@
export default async functionX () {}

View File

@@ -0,0 +1,4 @@
{
"sourceType": "module",
"throws": "Unexpected token, expected \"function\" (1:21)"
}

View File

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

View File

@@ -0,0 +1,119 @@
{
"type": "File",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"program": {
"type": "Program",
"start": 0,
"end": 24,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
}
},
"expression": {
"type": "Identifier",
"start": 0,
"end": 5,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 5
},
"identifierName": "async"
},
"name": "async"
}
},
{
"type": "FunctionDeclaration",
"start": 6,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"id": {
"type": "Identifier",
"start": 15,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 12
},
"identifierName": "foo"
},
"name": "foo"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 21,
"end": 24,
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 18
}
},
"body": [],
"directives": []
}
}
],
"directives": []
}
}