Add support for the "Hack" pipeline proposal (#13191)

Co-authored-by: Nicolò Ribaudo <nicolo.ribaudo@gmail.com>
This commit is contained in:
J. S. Choi
2021-06-02 17:53:55 -04:00
committed by Nicolò Ribaudo
parent 885e1e02f5
commit 6276853eb9
675 changed files with 7126 additions and 496 deletions

View File

@@ -134,6 +134,19 @@ export const ErrorMessages = makeErrorTemplates(
ParamDupe: "Argument name clash.",
PatternHasAccessor: "Object pattern can't contain getter or setter.",
PatternHasMethod: "Object pattern can't contain methods.",
PipeBodyIsTighter:
"Unexpected %0 after pipeline body; any %0 expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence.",
PipeTopicRequiresHackPipes:
'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
PipeTopicUnbound:
"Topic reference is unbound; it must be inside a pipe body.",
PipeTopicUnused:
"Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
// Messages whose codes start with “Pipeline” or “PrimaryTopic”
// are retained for backwards compatibility
// with the deprecated smart-mix pipe operator proposal plugin.
// They are subject to removal in a future major version.
PipelineBodyNoArrow:
'Unexpected arrow "=>" after pipeline body; arrow function in pipeline body must be parenthesized.',
PipelineBodySequenceExpression:
@@ -145,7 +158,8 @@ export const ErrorMessages = makeErrorTemplates(
PrimaryTopicNotAllowed:
"Topic reference was used in a lexical context without topic binding.",
PrimaryTopicRequiresSmartPipeline:
"Primary Topic Reference found but pipelineOperator not passed 'smart' for 'proposal' option.",
'Topic reference is used, but the pipelineOperator plugin was not passed a "proposal": "hack" or "smart" option.',
PrivateInExpectedIn:
"Private names are only allowed in property accesses (`obj.#%0`) or in `in` expressions (`#%0 in obj`).",
PrivateNameRedeclaration: "Duplicate private name #%0.",

View File

@@ -292,6 +292,28 @@ export default class ExpressionParser extends LValParser {
const operator = this.state.value;
node.operator = operator;
const leftIsHackPipeExpression =
left.type === "BinaryExpression" &&
left.operator === "|>" &&
this.getPluginOption("pipelineOperator", "proposal") === "hack";
if (leftIsHackPipeExpression) {
// If the pipelinePlugin is configured to use Hack pipes,
// and if an assignment expressions LHS invalidly contains `|>`,
// then the user likely meant to parenthesize the assignment expression.
// Throw a human-friendly error
// instead of something like 'Invalid left-hand side'.
// For example, `x = x |> y = #` (assuming `#` is the topic reference)
// groups into `x = (x |> y) = #`,
// and `(x |> y)` is an invalid assignment LHS.
// This is because Hack-style `|>` has tighter precedence than `=>`.
// (Unparenthesized `yield` expressions are handled
// in `parseHackPipeBody`,
// and unparenthesized `=>` expressions are handled
// in `checkHackPipeBodyEarlyErrors`.)
throw this.raise(this.state.start, Errors.PipeBodyIsTighter, operator);
}
if (this.match(tt.eq)) {
node.left = this.toAssignable(left, /* isLHS */ true);
refExpressionErrors.doubleProto = -1; // reset because double __proto__ is valid in assignment expression
@@ -386,7 +408,6 @@ export default class ExpressionParser extends LValParser {
if (this.state.inFSharpPipelineDirectBody) {
return left;
}
this.state.inPipeline = true;
this.checkPipelineAtInfixOperator(left, leftStartPos);
}
const node = this.startNodeAt(leftStartPos, leftStartLoc);
@@ -453,21 +474,30 @@ export default class ExpressionParser extends LValParser {
switch (op) {
case tt.pipeline:
switch (this.getPluginOption("pipelineOperator", "proposal")) {
case "hack":
return this.withTopicBindingContext(() => {
const bodyExpr = this.parseHackPipeBody(op, prec);
this.checkHackPipeBodyEarlyErrors(startPos);
return bodyExpr;
});
case "smart":
return this.withTopicPermittingContext(() => {
return this.parseSmartPipelineBody(
this.parseExprOpBaseRightExpr(op, prec),
return this.withTopicBindingContext(() => {
const childExpr = this.parseHackPipeBody(op, prec);
return this.parseSmartPipelineBodyInStyle(
childExpr,
startPos,
startLoc,
);
});
case "fsharp":
return this.withSoloAwaitPermittingContext(() => {
return this.parseFSharpPipelineBody(prec);
});
}
// falls through
// Falls through.
default:
return this.parseExprOpBaseRightExpr(op, prec);
}
@@ -488,6 +518,39 @@ export default class ExpressionParser extends LValParser {
);
}
// Helper function for `parseExprOpRightExpr` for the Hack-pipe operator
// (and the Hack-style smart-mix pipe operator).
parseHackPipeBody(op: TokenType, prec: number): N.Expression {
// If the following expression is invalidly a `yield` expression,
// then throw a human-friendly error.
// A `yield` expression in a generator context (i.e., a [Yield] production)
// starts a YieldExpression.
// Outside of a generator context, any `yield` as a pipe body
// is considered simply an identifier.
// This error is checked here, before actually parsing the body expression,
// because `yield`s “not allowed as identifier in generator” error
// would otherwise have immediately
// occur before the pipe body is fully parsed.
// (Unparenthesized assignment expressions are handled
// in `parseMaybeAssign`,
// and unparenthesized `=>` expressions are handled
// in `checkHackPipeBodyEarlyErrors`.)
const bodyIsInGeneratorContext = this.prodParam.hasYield;
const bodyIsYieldExpression =
bodyIsInGeneratorContext && this.isContextual("yield");
if (bodyIsYieldExpression) {
throw this.raise(
this.state.start,
Errors.PipeBodyIsTighter,
this.state.value,
);
} else {
return this.parseExprOpBaseRightExpr(op, prec);
}
}
checkExponentialAfterUnary(node: N.AwaitExpression | N.UnaryExpression) {
if (this.match(tt.exponent)) {
this.raise(
@@ -1163,26 +1226,14 @@ export default class ExpressionParser extends LValParser {
}
return node;
}
case tt.hash: {
if (this.state.inPipeline) {
node = this.startNode();
if (
this.getPluginOption("pipelineOperator", "proposal") !== "smart"
) {
this.raise(node.start, Errors.PrimaryTopicRequiresSmartPipeline);
}
this.next();
if (!this.primaryTopicReferenceIsAllowedInCurrentTopicContext()) {
this.raise(node.start, Errors.PrimaryTopicNotAllowed);
}
this.registerTopicReference();
return this.finishNode(node, "PipelinePrimaryTopicReference");
node = this.maybeParseTopicReference();
if (node) {
return node;
}
}
// fall through
case tt.relational: {
if (this.state.value === "<") {
@@ -1195,12 +1246,102 @@ export default class ExpressionParser extends LValParser {
}
}
}
// fall through
default:
throw this.unexpected();
}
}
// https://github.com/js-choi/proposal-hack-pipes
maybeParseTopicReference(): ?N.Expression {
const pipeProposal = this.getPluginOption("pipelineOperator", "proposal");
// `pipeProposal` is falsy when an input program
// contains a topic reference on its own,
// outside of a pipe expression,
// and without having turned on the pipelineOperator plugin.
if (pipeProposal) {
// A pipe-operator proposal is active.
const tokenType = this.state.type;
if (this.testTopicReferenceConfiguration(pipeProposal, tokenType)) {
// The token matches the plugins configuration.
// The token is therefore a topic reference.
const node = this.startNode();
// Determine the node type for the topic reference
// that is appropriate for the active pipe-operator proposal.
let nodeType;
if (pipeProposal === "smart") {
nodeType = "PipelinePrimaryTopicReference";
} else {
// The proposal must otherwise be "hack",
// as enforced by testTopicReferenceConfiguration.
nodeType = "TopicReference";
}
// Consume the token.
this.next();
// Register the topic reference so that its pipe body knows
// that its topic was used at least once.
this.registerTopicReference();
if (!this.topicReferenceIsAllowedInCurrentContext()) {
// The topic reference is not allowed in the current context:
// it is outside of a pipe body.
// Raise recoverable errors.
if (pipeProposal === "smart") {
this.raise(this.state.start, Errors.PrimaryTopicNotAllowed);
} else {
// In this case, `pipeProposal === "hack"` is true.
this.raise(this.state.start, Errors.PipeTopicUnbound);
}
}
return this.finishNode(node, nodeType);
} else {
// The token does not match the plugins configuration.
throw this.raise(
this.state.start,
Errors.PipeTopicUnconfiguredToken,
tokenType.label,
);
}
}
}
// This helper method tests whether the given token type
// matches the pipelineOperator parser plugins configuration.
// If the active pipe proposal is Hack style,
// and if the given token is the same as the plugin configurations `topicToken`,
// then this is a valid topic reference.
// If the active pipe proposal is smart mix,
// then the topic token must always be `#`.
// If the active pipe proposal is neither (e.g., "minimal" or "fsharp"),
// then an error is thrown.
testTopicReferenceConfiguration(
pipeProposal: string,
tokenType: TokenType,
): boolean {
switch (pipeProposal) {
case "hack": {
const pluginTopicToken = this.getPluginOption(
"pipelineOperator",
"topicToken",
);
return tokenType.label === pluginTopicToken;
}
case "smart":
return tokenType === tt.hash;
default:
throw this.raise(this.state.start, Errors.PipeTopicRequiresHackPipes);
}
}
// async [no LineTerminator here] AsyncArrowBindingIdentifier[?Yield] [no LineTerminator here] => AsyncConciseBody[?In]
parseAsyncArrowUnaryFunction(node: N.Node): N.ArrowFunctionExpression {
// We don't need to push a new ParameterDeclarationScope here since we are sure
@@ -2522,52 +2663,48 @@ export default class ExpressionParser extends LValParser {
}
}
parseSmartPipelineBody(
childExpression: N.Expression,
startPos: number,
startLoc: Position,
): N.PipelineBody {
this.checkSmartPipelineBodyEarlyErrors(childExpression, startPos);
// This helper method is to be called immediately
// after a Hack-style pipe body is parsed.
// The `startPos` is the starting position of the pipe body.
return this.parseSmartPipelineBodyInStyle(
childExpression,
startPos,
startLoc,
);
}
checkSmartPipelineBodyEarlyErrors(
childExpression: N.Expression,
startPos: number,
): void {
checkHackPipeBodyEarlyErrors(startPos: number): void {
// If the following token is invalidly `=>`,
// then throw a human-friendly error
// instead of something like 'Unexpected token, expected ";"'.
// For example, `x => x |> y => #` (assuming `#` is the topic reference)
// groups into `x => (x |> y) => #`,
// and `(x |> y) => #` is an invalid arrow function.
// This is because Hack-style `|>` has tighter precedence than `=>`.
// (Unparenthesized `yield` expressions are handled
// in `parseHackPipeBody`,
// and unparenthesized assignment expressions are handled
// in `parseMaybeAssign`.)
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 (childExpression.type === "SequenceExpression") {
this.raise(startPos, Errors.PipelineBodySequenceExpression);
throw this.raise(
this.state.start,
Errors.PipeBodyIsTighter,
tt.arrow.label,
);
} else if (!this.topicReferenceWasUsedInCurrentContext()) {
// A Hack pipe body must use the topic reference at least once.
this.raise(startPos, Errors.PipeTopicUnused);
}
}
parseSmartPipelineBodyInStyle(
childExpression: N.Expression,
childExpr: N.Expression,
startPos: number,
startLoc: Position,
): N.PipelineBody {
const bodyNode = this.startNodeAt(startPos, startLoc);
const isSimpleReference = this.isSimpleReference(childExpression);
if (isSimpleReference) {
bodyNode.callee = childExpression;
if (this.isSimpleReference(childExpr)) {
bodyNode.callee = childExpr;
return this.finishNode(bodyNode, "PipelineBareFunction");
} else {
if (!this.topicReferenceWasUsedInCurrentTopicContext()) {
this.raise(startPos, Errors.PipelineTopicUnused);
}
bodyNode.expression = childExpression;
this.checkSmartPipeTopicBodyEarlyErrors(startPos);
bodyNode.expression = childExpr;
return this.finishNode(bodyNode, "PipelineTopicExpression");
}
return this.finishNode(
bodyNode,
isSimpleReference ? "PipelineBareFunction" : "PipelineTopicExpression",
);
}
isSimpleReference(expression: N.Expression): boolean {
@@ -2583,13 +2720,34 @@ export default class ExpressionParser extends LValParser {
}
}
// Enable topic references from outer contexts within smart pipeline bodies.
// The function modifies the parser's topic-context state to enable or disable
// the use of topic references with the smartPipelines plugin. They then run a
// callback, then they reset the parser to the old topic-context state that it
// had before the function was called.
// This helper method is to be called immediately
// after a topic-style smart-mix pipe body is parsed.
// The `startPos` is the starting position of the pipe body.
withTopicPermittingContext<T>(callback: () => T): T {
checkSmartPipeTopicBodyEarlyErrors(startPos: number): void {
// If the following token is invalidly `=>`, then throw a human-friendly error
// instead of something like 'Unexpected token, expected ";"'.
// For example, `x => x |> y => #` (assuming `#` is the topic reference)
// groups into `x => (x |> y) => #`,
// and `(x |> y) => #` is an invalid arrow function.
// This is because smart-mix `|>` has tighter precedence than `=>`.
if (this.match(tt.arrow)) {
throw this.raise(this.state.start, Errors.PipelineBodyNoArrow);
}
// A topic-style smart-mix pipe body must use the topic reference at least once.
else if (!this.topicReferenceWasUsedInCurrentContext()) {
this.raise(startPos, Errors.PipelineTopicUnused);
}
}
// Enable topic references from outer contexts within Hack-style pipe bodies.
// The function modifies the parser's topic-context state to enable or disable
// the use of topic references.
// The function then calls a callback, then resets the parser
// to the old topic-context state that it had before the function was called.
withTopicBindingContext<T>(callback: () => T): T {
const outerContextTopicState = this.state.topicContext;
this.state.topicContext = {
// Enable the use of the primary topic reference.
@@ -2605,26 +2763,37 @@ export default class ExpressionParser extends LValParser {
}
}
// Disable topic references from outer contexts within syntax constructs
// This helper method is used only with the deprecated smart-mix pipe proposal.
// Disables topic references from outer contexts within syntax constructs
// such as the bodies of iteration statements.
// The function modifies the parser's topic-context state to enable or disable
// the use of topic references with the smartPipelines plugin. They then run a
// callback, then they reset the parser to the old topic-context state that it
// had before the function was called.
withTopicForbiddingContext<T>(callback: () => T): T {
const outerContextTopicState = this.state.topicContext;
this.state.topicContext = {
// Disable the use of the primary topic reference.
maxNumOfResolvableTopics: 0,
// Hide the use of any topic references from outer contexts.
maxTopicIndex: null,
};
withSmartMixTopicForbiddingContext<T>(callback: () => T): T {
const proposal = this.getPluginOption("pipelineOperator", "proposal");
if (proposal === "smart") {
// Reset the parsers topic context only if the smart-mix pipe proposal is active.
const outerContextTopicState = this.state.topicContext;
this.state.topicContext = {
// Disable the use of the primary topic reference.
maxNumOfResolvableTopics: 0,
// Hide the use of any topic references from outer contexts.
maxTopicIndex: null,
};
try {
try {
return callback();
} finally {
this.state.topicContext = outerContextTopicState;
}
} else {
// If the pipe proposal is "minimal", "fsharp", or "hack",
// or if no pipe proposal is active,
// then the callback result is returned
// without touching any extra parser state.
return callback();
} finally {
this.state.topicContext = outerContextTopicState;
}
}
@@ -2667,17 +2836,17 @@ export default class ExpressionParser extends LValParser {
return callback();
}
// Register the use of a primary topic reference (`#`) within the current
// topic context.
// Register the use of a topic reference within the current
// topic-binding context.
registerTopicReference(): void {
this.state.topicContext.maxTopicIndex = 0;
}
primaryTopicReferenceIsAllowedInCurrentTopicContext(): boolean {
topicReferenceIsAllowedInCurrentContext(): boolean {
return this.state.topicContext.maxNumOfResolvableTopics >= 1;
}
topicReferenceWasUsedInCurrentTopicContext(): boolean {
topicReferenceWasUsedInCurrentContext(): boolean {
return (
this.state.topicContext.maxTopicIndex != null &&
this.state.topicContext.maxTopicIndex >= 0

View File

@@ -528,11 +528,12 @@ export default class StatementParser extends ExpressionParser {
this.next();
this.state.labels.push(loopLabel);
// Parse the loop body's body.
node.body =
// For the smartPipelines plugin: Disable topic references from outer
// contexts within the loop body. They are permitted in test expressions,
// outside of the loop body.
this.withTopicForbiddingContext(() =>
this.withSmartMixTopicForbiddingContext(() =>
// Parse the loop body's body.
this.parseStatement("do"),
);
@@ -760,15 +761,16 @@ export default class StatementParser extends ExpressionParser {
this.scope.enter(SCOPE_OTHER);
}
// Parse the catch clause's body.
clause.body =
// For the smartPipelines plugin: Disable topic references from outer
// contexts within the catch clause's body.
this.withTopicForbiddingContext(() =>
this.withSmartMixTopicForbiddingContext(() =>
// Parse the catch clause's body.
this.parseBlock(false, false),
);
this.scope.exit();
this.scope.exit();
node.handler = this.finishNode(clause, "CatchClause");
}
@@ -796,11 +798,12 @@ export default class StatementParser extends ExpressionParser {
node.test = this.parseHeaderExpression();
this.state.labels.push(loopLabel);
// Parse the loop body.
node.body =
// For the smartPipelines plugin:
// Disable topic references from outer contexts within the loop body.
// They are permitted in test expressions, outside of the loop body.
this.withTopicForbiddingContext(() =>
this.withSmartMixTopicForbiddingContext(() =>
// Parse loop body.
this.parseStatement("while"),
);
@@ -817,12 +820,13 @@ export default class StatementParser extends ExpressionParser {
this.next();
node.object = this.parseHeaderExpression();
// Parse the statement body.
node.body =
// For the smartPipelines plugin:
// Disable topic references from outer contexts within the with statement's body.
// They are permitted in function default-parameter expressions, which are
// part of the outer context, outside of the with statement's body.
this.withTopicForbiddingContext(() =>
this.withSmartMixTopicForbiddingContext(() =>
// Parse the statement body.
this.parseStatement("with"),
);
@@ -1010,11 +1014,12 @@ export default class StatementParser extends ExpressionParser {
node.update = this.match(tt.parenR) ? null : this.parseExpression();
this.expect(tt.parenR);
// Parse the loop body.
node.body =
// For the smartPipelines plugin: Disable topic references from outer
// contexts within the loop body. They are permitted in test expressions,
// outside of the loop body.
this.withTopicForbiddingContext(() =>
this.withSmartMixTopicForbiddingContext(() =>
// Parse the loop body.
this.parseStatement("for"),
);
@@ -1065,11 +1070,12 @@ export default class StatementParser extends ExpressionParser {
: this.parseMaybeAssignAllowIn();
this.expect(tt.parenR);
// Parse the loop body.
node.body =
// For the smartPipelines plugin:
// Disable topic references from outer contexts within the loop body.
// They are permitted in test expressions, outside of the loop body.
this.withTopicForbiddingContext(() =>
this.withSmartMixTopicForbiddingContext(() =>
// Parse loop body.
this.parseStatement("for"),
);
@@ -1177,7 +1183,7 @@ export default class StatementParser extends ExpressionParser {
// For the smartPipelines plugin: Disable topic references from outer
// contexts within the function body. They are permitted in function
// default-parameter expressions, outside of the function body.
this.withTopicForbiddingContext(() => {
this.withSmartMixTopicForbiddingContext(() => {
// Parse the function body.
this.parseFunctionBodyAndFinish(
node,
@@ -1293,7 +1299,8 @@ export default class StatementParser extends ExpressionParser {
// For the smartPipelines plugin: Disable topic references from outer
// contexts within the class body.
this.withTopicForbiddingContext(() => {
this.withSmartMixTopicForbiddingContext(() => {
// Parse the contents within the braces.
while (!this.match(tt.braceR)) {
if (this.eat(tt.semi)) {
if (decorators.length > 0) {

View File

@@ -38,7 +38,8 @@ export function getPluginOption(
return null;
}
const PIPELINE_PROPOSALS = ["minimal", "smart", "fsharp"];
const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
const TOPIC_TOKENS = ["%", "#"];
const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
export function validatePlugins(plugins: PluginList) {
@@ -74,16 +75,45 @@ export function validatePlugins(plugins: PluginList) {
throw new Error("Cannot combine placeholders and v8intrinsic plugins.");
}
if (
hasPlugin(plugins, "pipelineOperator") &&
!PIPELINE_PROPOSALS.includes(
getPluginOption(plugins, "pipelineOperator", "proposal"),
)
) {
throw new Error(
"'pipelineOperator' requires 'proposal' option whose value should be one of: " +
PIPELINE_PROPOSALS.map(p => `'${p}'`).join(", "),
);
if (hasPlugin(plugins, "pipelineOperator")) {
const proposal = getPluginOption(plugins, "pipelineOperator", "proposal");
if (!PIPELINE_PROPOSALS.includes(proposal)) {
const proposalList = PIPELINE_PROPOSALS.map(p => `"${p}"`).join(", ");
throw new Error(
`"pipelineOperator" requires "proposal" option whose value must be one of: ${proposalList}.`,
);
}
const tupleSyntaxIsHash =
hasPlugin(plugins, "recordAndTuple") &&
getPluginOption(plugins, "recordAndTuple", "syntaxType") === "hash";
if (proposal === "hack") {
const topicToken = getPluginOption(
plugins,
"pipelineOperator",
"topicToken",
);
if (!TOPIC_TOKENS.includes(topicToken)) {
const tokenList = TOPIC_TOKENS.map(t => `"${t}"`).join(", ");
throw new Error(
`"pipelineOperator" in "proposal": "hack" mode also requires a "topicToken" option whose value must be one of: ${tokenList}.`,
);
}
if (topicToken === "#" && tupleSyntaxIsHash) {
throw new Error(
'Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.',
);
}
} else if (proposal === "smart" && tupleSyntaxIsHash) {
throw new Error(
'Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.',
);
}
}
if (hasPlugin(plugins, "moduleAttributes")) {

View File

@@ -64,7 +64,6 @@ export default class State {
// Flags to track
maybeInArrowParameters: boolean = false;
inPipeline: boolean = false;
inType: boolean = false;
noAnonFunctionType: boolean = false;
inPropertyName: boolean = false;
@@ -72,13 +71,13 @@ export default class State {
isAmbientContext: boolean = false;
inAbstractClass: boolean = false;
// For the smartPipelines plugin:
// For the Hack-style pipelines plugin
topicContext: TopicContextState = {
maxNumOfResolvableTopics: 0,
maxTopicIndex: null,
};
// For the F# plugin
// For the F#-style pipelines plugin
soloAwait: boolean = false;
inFSharpPipelineDirectBody: boolean = false;

View File

@@ -631,7 +631,13 @@ export type ParenthesizedExpression = NodeBase & {
expression: Expression,
};
// Pipelines
// Hack pipe operator
export type TopicReference = NodeBase & {
type: "TopicReference",
};
// Smart-mix pipe operator
export type PipelineBody = NodeBase & {
type: "PipelineBody",
@@ -663,6 +669,10 @@ export type PipelineStyle =
| "PipelineBareAwaitedFunction"
| "PipelineTopicExpression";
export type PipelinePrimaryTopicReference = NodeBase & {
type: "PipelinePrimaryTopicReference",
};
// Template Literals
export type TemplateLiteral = NodeBase & {