Skip to content

Commit

Permalink
Enable solo await in F# pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoarrais authored and mAAdhaTTah committed Feb 24, 2019
1 parent da3515c commit b08f341
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 2 deletions.
19 changes: 17 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -410,7 +410,9 @@ export default class ExpressionParser extends LValParser {
);
});
case "fsharp":
return this.parseFSharpPipelineBody(op, prec, noIn);
return this.withSoloAwaitPermittingContext(() => {
return this.parseFSharpPipelineBody(op, prec, noIn);
});
}
// falls through

Expand Down Expand Up @@ -2074,7 +2076,9 @@ export default class ExpressionParser extends LValParser {
);
}

node.argument = this.parseMaybeUnary();
if (!this.state.soloAwait) {
node.argument = this.parseMaybeUnary();
}
return this.finishNode(node, "AwaitExpression");
}

Expand Down Expand Up @@ -2264,6 +2268,17 @@ export default class ExpressionParser extends LValParser {
}
}

withSoloAwaitPermittingContext<T>(callback: () => T): T {
const outerContextSoloAwaitState = this.state.soloAwait;
this.state.soloAwait = true;

try {
return callback();
} finally {
this.state.soloAwait = outerContextSoloAwaitState;
}
}

// Register the use of a primary topic reference (`#`) within the current
// topic context.
registerTopicReference(): void {
Expand Down
3 changes: 3 additions & 0 deletions packages/babel-parser/src/tokenizer/state.js
Expand Up @@ -87,6 +87,9 @@ export default class State {
maxTopicIndex: null,
};

// For the F# plugin
soloAwait: boolean = false;

// Check whether we are in a (nested) class or not.
classLevel: number = 0;

Expand Down
@@ -0,0 +1,3 @@
async function f () {
return x |> await;
}
@@ -0,0 +1,3 @@
{
"plugins": [["pipelineOperator", { "proposal": "fsharp" }]]
}
@@ -0,0 +1,181 @@
{
"type": "File",
"start": 0,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 44,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 44,
"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": 44,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ReturnStatement",
"start": 24,
"end": 42,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 20
}
},
"argument": {
"type": "BinaryExpression",
"start": 31,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 19
}
},
"left": {
"type": "PipelineHead",
"start": 33,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 11
},
"end": {
"line": 2,
"column": 10
}
},
"head": {
"type": "Identifier",
"start": 31,
"end": 32,
"loc": {
"start": {
"line": 2,
"column": 9
},
"end": {
"line": 2,
"column": 10
},
"identifierName": "x"
},
"name": "x"
}
},
"operator": "|>",
"right": {
"type": "PipelineBody",
"start": 36,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 19
}
},
"body": {
"type": "AwaitExpression",
"start": 36,
"end": 41,
"loc": {
"start": {
"line": 2,
"column": 14
},
"end": {
"line": 2,
"column": 19
}
}
}
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}

0 comments on commit b08f341

Please sign in to comment.