Parse async arrows with flow type parameters (#6802)

Fixes #6712
This commit is contained in:
Nicolò Ribaudo
2017-11-14 16:24:14 +01:00
committed by GitHub
parent 3c359698a7
commit 9ae23639ad
6 changed files with 909 additions and 4 deletions

View File

@@ -1498,11 +1498,12 @@ export default class ExpressionParser extends LValParser {
return node;
}
// Parse arrow function expression with given parameters.
// Parse arrow function expression.
// If the parameters are provided, they will be converted to an
// assignable list.
parseArrowExpression(
node: N.ArrowFunctionExpression,
params: N.Expression[],
params?: ?(N.Expression[]),
isAsync?: boolean,
): N.ArrowFunctionExpression {
// if we got there, it's no more "yield in possible arrow parameters";
@@ -1518,7 +1519,7 @@ export default class ExpressionParser extends LValParser {
const oldInFunc = this.state.inFunction;
this.state.inFunction = true;
this.initFunction(node, isAsync);
this.setArrowFunctionParameters(node, params);
if (params) this.setArrowFunctionParameters(node, params);
const oldInGenerator = this.state.inGenerator;
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;

View File

@@ -2219,8 +2219,45 @@ export default (superClass: Class<Parser>): Class<Parser> =>
node.callee = base;
node.arguments = this.parseCallExpressionArguments(tt.parenR, false);
base = this.finishNode(node, "CallExpression");
} else if (
base.type === "Identifier" &&
base.name === "async" &&
this.isRelational("<")
) {
const state = this.state.clone();
let error;
try {
const node = this.parseAsyncArrowWithTypeParameters(
startPos,
startLoc,
);
if (node) return node;
} catch (e) {
error = e;
}
this.state = state;
try {
return super.parseSubscripts(base, startPos, startLoc, noCalls);
} catch (e) {
throw error || e;
}
}
return super.parseSubscripts(base, startPos, startLoc, noCalls);
}
parseAsyncArrowWithTypeParameters(
startPos: number,
startLoc: Position,
): ?N.ArrowFunctionExpression {
const node = this.startNodeAt(startPos, startLoc);
this.parseFunctionParams(node);
if (!this.parseArrow(node)) return;
return this.parseArrowExpression(
node,
/* params */ undefined,
/* isAsync */ true,
);
}
};