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

@ -237,6 +237,23 @@ export function DecimalLiteral(this: Printer, node: t.DecimalLiteral) {
this.word(node.value + "m");
}
// Hack pipe operator
export function TopicReference(this: Printer) {
const { topicToken } = this.format;
switch (topicToken) {
case "#":
this.token("#");
break;
default: {
const givenTopicTokenJSON = JSON.stringify(topicToken);
const message = `The "topicToken" generator option must be "#" (${givenTopicTokenJSON} received instead).`;
throw new Error(message);
}
}
}
// Smart-mix pipe operator
export function PipelineTopicExpression(
this: Printer,
node: t.PipelineTopicExpression,

View File

@ -62,6 +62,7 @@ function normalizeOptions(code, opts): Format {
...opts.jsescOption,
},
recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType,
topicToken: opts.topicToken,
};
if (!process.env.BABEL_8_BREAKING) {
@ -197,6 +198,12 @@ export interface GeneratorOptions {
*/
wrap?: boolean;
};
/**
* For use with the Hack-style pipe operator.
* Changes what token is used for pipe bodies topic references.
*/
topicToken?: "#";
}
export interface GeneratorResult {

View File

@ -33,6 +33,11 @@ export type Format = {
recordAndTupleSyntaxType: "bar" | "hash";
jsescOption;
jsonCompatibleStrings?;
/**
* For use with the Hack-style pipe operator.
* Changes what token is used for pipe bodies topic references.
*/
topicToken?: "#";
};
class Printer {

View File

@ -1,3 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "smart" }], "doExpressions"]
"plugins": [["pipelineOperator", { "proposal": "smart" }]]
}

View File

@ -0,0 +1 @@
2 + 3 |> #.toString(16);

View File

@ -0,0 +1 @@
2 + 3 |> #.toString(16);

View File

@ -0,0 +1 @@
2 + 3 |> #.toString(16);

View File

@ -0,0 +1,4 @@
{
"plugins": [["pipelineOperator", { "proposal": "hack", "topicToken": "#" }]],
"topicToken": "#"
}

View File

@ -0,0 +1 @@
2 + 3 |> #.toString(16);

View File

@ -0,0 +1 @@
2 + 3 |> #.toString(16);

View File

@ -0,0 +1,5 @@
{
"plugins": [["pipelineOperator", { "proposal": "hack", "topicToken": "#" }]],
"topicToken": "invalid",
"throws": "The \"topicToken\" generator option must be \"#\" (\"invalid\" received instead)."
}

View File

@ -0,0 +1 @@
2 + 3 |> #.toString(16);

View File

@ -0,0 +1,4 @@
{
"plugins": [["pipelineOperator", { "proposal": "hack", "topicToken": "#" }]],
"throws": "The \"topicToken\" generator option must be \"#\" (undefined received instead)."
}

View File

@ -960,54 +960,6 @@ interface BindExpression <: Expression {
If `object` is `null`, then `callee` should be a `MemberExpression`.
### Pipeline
These nodes are used by the Smart Pipeline to determine the type of the expression in a Pipeline Operator Expression. The F# Pipeline uses simple `BinaryExpression`s.
#### PipelineBody
```js
interface PipelineBody <: NodeBase {
type: "PipelineBody";
}
```
#### PipelineBareFunctionBody
```js
interface PipelineBody <: NodeBase {
type: "PipelineBareFunctionBody";
callee: Expression;
}
```
#### PipelineBareConstructorBody
```js
interface PipelineBareConstructorBody <: NodeBase {
type: "PipelineBareConstructorBody";
callee: Expression;
}
```
#### PipelineBareAwaitedFunctionBody
```js
interface PipelineBareConstructorBody <: NodeBase {
type: "PipelineTopicBody";
expression: Expression;
}
```
#### PipelineTopicBody
```js
interface PipelineBareConstructorBody <: NodeBase {
type: "PipelineBareAwaitedFunctionBody";
callee: Expression;
}
```
## ConditionalExpression
```js
@ -1099,6 +1051,17 @@ interface ModuleExpression <: Expression {
A inline module expression proposed in https://github.com/tc39/proposal-js-module-blocks.
## TopicReference
```js
interface TopicReference <: Expression {
type: "TopicReference";
}
```
A topic reference to be used inside the body of
a [Hack-style pipe expression](https://github.com/js-choi/proposal-hack-pipes).
# Template Literals
## TemplateLiteral
@ -1431,3 +1394,53 @@ interface ExportAllDeclaration <: ModuleDeclaration {
```
An export batch declaration, e.g., `export * from "mod";`.
### Smart-mix pipelines
These types are **deprecated**.
They are used by the deprecated smart-mix pipe operator to determine
the type of a pipe expression's the body expression.
The Hack and F# pipe operators use simple `BinaryExpression`s.
#### PipelineBody
```js
interface PipelineBody <: NodeBase {
type: "PipelineBody";
}
```
#### PipelineBareFunctionBody
```js
interface PipelineBody <: NodeBase {
type: "PipelineBareFunctionBody";
callee: Expression;
}
```
#### PipelineBareConstructorBody
```js
interface PipelineBareConstructorBody <: NodeBase {
type: "PipelineBareConstructorBody";
callee: Expression;
}
```
#### PipelineBareAwaitedFunctionBody
```js
interface PipelineBareConstructorBody <: NodeBase {
type: "PipelineTopicBody";
expression: Expression;
}
```
#### PipelineTopicBody
```js
interface PipelineBareConstructorBody <: NodeBase {
type: "PipelineBareAwaitedFunctionBody";
callee: Expression;
}

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 & {

View File

@ -0,0 +1,4 @@
{
"plugins": [["pipelineOperator", { "proposal": "fsharp" }]],
"throws": "Topic reference is used, but the pipelineOperator plugin was not passed a \"proposal\": \"hack\" or \"smart\" option. (1:5)"
}

View File

@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "hack", "topicToken": "#" }]]
}

View File

@ -0,0 +1,45 @@
{
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"program": {
"type": "Program",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"left": {
"type": "Identifier",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5},"identifierName":"value"},
"name": "value"
},
"operator": "|>",
"right": {
"type": "BinaryExpression",
"start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14}},
"left": {
"type": "TopicReference",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}}
},
"operator": "+",
"right": {
"type": "NumericLiteral",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "hack", "topicToken": "#" }]]
}

View File

@ -0,0 +1,45 @@
{
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"program": {
"type": "Program",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"left": {
"type": "Identifier",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5},"identifierName":"value"},
"name": "value"
},
"operator": "|>",
"right": {
"type": "BinaryExpression",
"start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14}},
"left": {
"type": "NumericLiteral",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"operator": "+",
"right": {
"type": "TopicReference",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14}}
}
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "hack", "topicToken": "#" }]]
}

View File

@ -0,0 +1,45 @@
{
"type": "File",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"errors": [
"SyntaxError: Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"left": {
"type": "Identifier",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5},"identifierName":"value"},
"name": "value"
},
"operator": "|>",
"right": {
"type": "BinaryExpression",
"start":9,"end":14,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":14}},
"left": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"a"},
"name": "a"
},
"operator": "+",
"right": {
"type": "Identifier",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13},"end":{"line":1,"column":14},"identifierName":"b"},
"name": "b"
}
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "hack", "topicToken": "#" }]]
}

View File

@ -0,0 +1,57 @@
{
"type": "File",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"program": {
"type": "Program",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":22,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":22}},
"left": {
"type": "Identifier",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5},"identifierName":"value"},
"name": "value"
},
"operator": "|>",
"right": {
"type": "ArrowFunctionExpression",
"start":10,"end":21,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":21}},
"extra": {
"parenthesized": true,
"parenStart": 9
},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BinaryExpression",
"start":16,"end":21,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":21}},
"left": {
"type": "TopicReference",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":17}}
},
"operator": "+",
"right": {
"type": "NumericLiteral",
"start":20,"end":21,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":21}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
}
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,12 @@
{
"plugins": [
[
"pipelineOperator",
{
"proposal": "hack",
"topicToken": "#"
}
]
],
"throws": "Unexpected => after pipeline body; any => expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence. (1:8)"
}

View File

@ -0,0 +1,12 @@
{
"plugins": [
[
"pipelineOperator",
{
"proposal": "hack",
"topicToken": "#"
}
]
],
"throws": "Unexpected &&= after pipeline body; any &&= expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence. (1:11)"
}

View File

@ -0,0 +1,12 @@
{
"plugins": [
[
"pipelineOperator",
{
"proposal": "hack",
"topicToken": "#"
}
]
],
"throws": "Unexpected = after pipeline body; any = expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence. (1:16)"
}

View File

@ -0,0 +1,12 @@
{
"plugins": [
[
"pipelineOperator",
{
"proposal": "hack",
"topicToken": "#"
}
]
],
"throws": "Unexpected += after pipeline body; any += expression acting as Hack-style pipe body must be parenthesized due to its loose operator precedence. (1:11)"
}

View File

@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "hack", "topicToken": "#" }]]
}

View File

@ -0,0 +1,54 @@
{
"type": "File",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"program": {
"type": "Program",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start":0,"end":46,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"id": {
"type": "Identifier",
"start":15,"end":16,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":16},"identifierName":"f"},
"name": "f"
},
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start":20,"end":46,"loc":{"start":{"line":1,"column":20},"end":{"line":3,"column":1}},
"body": [
{
"type": "ReturnStatement",
"start":24,"end":44,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":22}},
"argument": {
"type": "BinaryExpression",
"start":31,"end":43,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":21}},
"left": {
"type": "Identifier",
"start":31,"end":32,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":10},"identifierName":"x"},
"name": "x"
},
"operator": "|>",
"right": {
"type": "AwaitExpression",
"start":36,"end":43,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":21}},
"argument": {
"type": "TopicReference",
"start":42,"end":43,"loc":{"start":{"line":2,"column":20},"end":{"line":2,"column":21}}
}
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}

View File

@ -0,0 +1,9 @@
value |> new (
@classDecorator
class Thing {
@methodDecorator
method () {
return # + this.property;
}
}
);

View File

@ -0,0 +1,6 @@
{
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "#" }],
["decorators", {"decoratorsBeforeExport": true}]
]
}

View File

@ -0,0 +1,124 @@
{
"type": "File",
"start":0,"end":130,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":2}},
"program": {
"type": "Program",
"start":0,"end":130,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":2}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":130,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":2}},
"expression": {
"type": "BinaryExpression",
"start":0,"end":129,"loc":{"start":{"line":1,"column":0},"end":{"line":9,"column":1}},
"left": {
"type": "Identifier",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5},"identifierName":"value"},
"name": "value"
},
"operator": "|>",
"right": {
"type": "NewExpression",
"start":9,"end":129,"loc":{"start":{"line":1,"column":9},"end":{"line":9,"column":1}},
"callee": {
"type": "ClassExpression",
"start":17,"end":127,"loc":{"start":{"line":2,"column":2},"end":{"line":8,"column":3}},
"extra": {
"parenthesized": true,
"parenStart": 13
},
"decorators": [
{
"type": "Decorator",
"start":17,"end":32,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":17}},
"expression": {
"type": "Identifier",
"start":18,"end":32,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":17},"identifierName":"classDecorator"},
"name": "classDecorator"
}
}
],
"id": {
"type": "Identifier",
"start":41,"end":46,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":13},"identifierName":"Thing"},
"name": "Thing"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":47,"end":127,"loc":{"start":{"line":3,"column":14},"end":{"line":8,"column":3}},
"body": [
{
"type": "ClassMethod",
"start":53,"end":123,"loc":{"start":{"line":4,"column":4},"end":{"line":7,"column":5}},
"decorators": [
{
"type": "Decorator",
"start":53,"end":69,"loc":{"start":{"line":4,"column":4},"end":{"line":4,"column":20}},
"expression": {
"type": "Identifier",
"start":54,"end":69,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":20},"identifierName":"methodDecorator"},
"name": "methodDecorator"
}
}
],
"static": false,
"key": {
"type": "Identifier",
"start":74,"end":80,"loc":{"start":{"line":5,"column":4},"end":{"line":5,"column":10},"identifierName":"method"},
"name": "method"
},
"computed": false,
"kind": "method",
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":84,"end":123,"loc":{"start":{"line":5,"column":14},"end":{"line":7,"column":5}},
"body": [
{
"type": "ReturnStatement",
"start":92,"end":117,"loc":{"start":{"line":6,"column":6},"end":{"line":6,"column":31}},
"argument": {
"type": "BinaryExpression",
"start":99,"end":116,"loc":{"start":{"line":6,"column":13},"end":{"line":6,"column":30}},
"left": {
"type": "TopicReference",
"start":99,"end":100,"loc":{"start":{"line":6,"column":13},"end":{"line":6,"column":14}}
},
"operator": "+",
"right": {
"type": "MemberExpression",
"start":103,"end":116,"loc":{"start":{"line":6,"column":17},"end":{"line":6,"column":30}},
"object": {
"type": "ThisExpression",
"start":103,"end":107,"loc":{"start":{"line":6,"column":17},"end":{"line":6,"column":21}}
},
"computed": false,
"property": {
"type": "Identifier",
"start":108,"end":116,"loc":{"start":{"line":6,"column":22},"end":{"line":6,"column":30},"identifierName":"property"},
"name": "property"
}
}
}
}
],
"directives": []
}
}
]
}
},
"arguments": []
}
}
}
],
"directives": []
}
}

Some files were not shown because too many files have changed in this diff Show More