Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify smart pipeline parsing #11919

Merged
merged 1 commit into from Aug 6, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 14 additions & 44 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -2479,77 +2479,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"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

childExpression.type === "SequenceExpression" implies the fact that pipelineStyle === "PipelineTopicExpression".

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