refactor: simplify smart pipeline parsing (#11919)
This commit is contained in:
parent
76f033f8c7
commit
a827ca41f3
@ -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,60 +2493,37 @@ 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":
|
|
||||||
bodyNode.callee = childExpression.callee;
|
|
||||||
break;
|
|
||||||
case "PipelineBareAwaitedFunction":
|
|
||||||
bodyNode.callee = childExpression.argument;
|
|
||||||
break;
|
|
||||||
case "PipelineTopicExpression":
|
|
||||||
if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
|
if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
|
||||||
this.raise(startPos, Errors.PipelineTopicUnused);
|
this.raise(startPos, Errors.PipelineTopicUnused);
|
||||||
}
|
}
|
||||||
bodyNode.expression = childExpression;
|
bodyNode.expression = childExpression;
|
||||||
break;
|
}
|
||||||
default:
|
return this.finishNode(
|
||||||
throw new Error(
|
bodyNode,
|
||||||
`Internal @babel/parser error: Unknown pipeline style (${pipelineStyle})`,
|
isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return this.finishNode(bodyNode, pipelineStyle);
|
|
||||||
}
|
|
||||||
|
|
||||||
checkSmartPipelineBodyStyle(expression: N.Expression): N.PipelineStyle {
|
|
||||||
switch (expression.type) {
|
|
||||||
default:
|
|
||||||
return this.isSimpleReference(expression)
|
|
||||||
? "PipelineBareFunction"
|
|
||||||
: "PipelineTopicExpression";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
isSimpleReference(expression: N.Expression): boolean {
|
isSimpleReference(expression: N.Expression): boolean {
|
||||||
switch (expression.type) {
|
switch (expression.type) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user