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) {