Skip to content

Commit

Permalink
refactor: simplify smart pipeline parsing (#11919)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Aug 6, 2020
1 parent 76f033f commit a827ca4
Showing 1 changed file with 14 additions and 44 deletions.
58 changes: 14 additions & 44 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -2482,77 +2482,47 @@ export default class ExpressionParser extends LValParser {
startPos: number,
startLoc: Position,
): N.PipelineBody {
const pipelineStyle = this.checkSmartPipelineBodyStyle(childExpression);

this.checkSmartPipelineBodyEarlyErrors(
childExpression,
pipelineStyle,
startPos,
);
this.checkSmartPipelineBodyEarlyErrors(childExpression, startPos);

return this.parseSmartPipelineBodyInStyle(
childExpression,
pipelineStyle,
startPos,
startLoc,
);
}

checkSmartPipelineBodyEarlyErrors(
childExpression: N.Expression,
pipelineStyle: N.PipelineStyle,
startPos: number,
): void {
if (this.match(tt.arrow)) {
// If the following token is invalidly `=>`, then throw a human-friendly error
// instead of something like 'Unexpected token, expected ";"'.
throw this.raise(this.state.start, Errors.PipelineBodyNoArrow);
} else if (
pipelineStyle === "PipelineTopicExpression" &&
childExpression.type === "SequenceExpression"
) {
} else if (childExpression.type === "SequenceExpression") {
this.raise(startPos, Errors.PipelineBodySequenceExpression);
}
}

parseSmartPipelineBodyInStyle(
childExpression: N.Expression,
pipelineStyle: N.PipelineStyle,
startPos: number,
startLoc: Position,
): N.PipelineBody {
const bodyNode = this.startNodeAt(startPos, startLoc);
switch (pipelineStyle) {
case "PipelineBareFunction":
bodyNode.callee = childExpression;
break;
case "PipelineBareConstructor":
bodyNode.callee = childExpression.callee;
break;
case "PipelineBareAwaitedFunction":
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";
const isSimpleReference = this.isSimpleReference(childExpression);
if (isSimpleReference) {
bodyNode.callee = childExpression;
} else {
if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
this.raise(startPos, Errors.PipelineTopicUnused);
}
bodyNode.expression = childExpression;
}
return this.finishNode(
bodyNode,
isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression",
);
}

isSimpleReference(expression: N.Expression): boolean {
Expand Down

0 comments on commit a827ca4

Please sign in to comment.