refactor: simplify smart pipeline parsing (#11919)

This commit is contained in:
Huáng Jùnliàng 2020-08-05 21:15:42 -04:00 committed by GitHub
parent 76f033f8c7
commit a827ca41f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2482,17 +2482,10 @@ export default class ExpressionParser extends LValParser {
startPos: number, startPos: number,
startLoc: Position, startLoc: Position,
): N.PipelineBody { ): N.PipelineBody {
const pipelineStyle = this.checkSmartPipelineBodyStyle(childExpression); this.checkSmartPipelineBodyEarlyErrors(childExpression, startPos);
this.checkSmartPipelineBodyEarlyErrors(
childExpression,
pipelineStyle,
startPos,
);
return this.parseSmartPipelineBodyInStyle( return this.parseSmartPipelineBodyInStyle(
childExpression, childExpression,
pipelineStyle,
startPos, startPos,
startLoc, startLoc,
); );
@ -2500,59 +2493,36 @@ export default class ExpressionParser extends LValParser {
checkSmartPipelineBodyEarlyErrors( checkSmartPipelineBodyEarlyErrors(
childExpression: N.Expression, childExpression: N.Expression,
pipelineStyle: N.PipelineStyle,
startPos: number, startPos: number,
): void { ): void {
if (this.match(tt.arrow)) { if (this.match(tt.arrow)) {
// If the following token is invalidly `=>`, then throw a human-friendly error // If the following token is invalidly `=>`, then throw a human-friendly error
// instead of something like 'Unexpected token, expected ";"'. // instead of something like 'Unexpected token, expected ";"'.
throw this.raise(this.state.start, Errors.PipelineBodyNoArrow); throw this.raise(this.state.start, Errors.PipelineBodyNoArrow);
} else if ( } else if (childExpression.type === "SequenceExpression") {
pipelineStyle === "PipelineTopicExpression" &&
childExpression.type === "SequenceExpression"
) {
this.raise(startPos, Errors.PipelineBodySequenceExpression); this.raise(startPos, Errors.PipelineBodySequenceExpression);
} }
} }
parseSmartPipelineBodyInStyle( parseSmartPipelineBodyInStyle(
childExpression: N.Expression, childExpression: N.Expression,
pipelineStyle: N.PipelineStyle,
startPos: number, startPos: number,
startLoc: Position, startLoc: Position,
): N.PipelineBody { ): N.PipelineBody {
const bodyNode = this.startNodeAt(startPos, startLoc); const bodyNode = this.startNodeAt(startPos, startLoc);
switch (pipelineStyle) { const isSimpleReference = this.isSimpleReference(childExpression);
case "PipelineBareFunction": if (isSimpleReference) {
bodyNode.callee = childExpression; bodyNode.callee = childExpression;
break; } else {
case "PipelineBareConstructor": if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
bodyNode.callee = childExpression.callee; this.raise(startPos, Errors.PipelineTopicUnused);
break; }
case "PipelineBareAwaitedFunction": bodyNode.expression = childExpression;
bodyNode.callee = childExpression.argument;
break;
case "PipelineTopicExpression":
if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
this.raise(startPos, Errors.PipelineTopicUnused);
}
bodyNode.expression = childExpression;
break;
default:
throw new Error(
`Internal @babel/parser error: Unknown pipeline style (${pipelineStyle})`,
);
}
return this.finishNode(bodyNode, pipelineStyle);
}
checkSmartPipelineBodyStyle(expression: N.Expression): N.PipelineStyle {
switch (expression.type) {
default:
return this.isSimpleReference(expression)
? "PipelineBareFunction"
: "PipelineTopicExpression";
} }
return this.finishNode(
bodyNode,
isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression",
);
} }
isSimpleReference(expression: N.Expression): boolean { isSimpleReference(expression: N.Expression): boolean {