Skip to content

Commit

Permalink
Update operator precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 4, 2021
1 parent 083e4b6 commit 9310eec
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 6 deletions.
5 changes: 3 additions & 2 deletions packages/babel-parser/src/parser/error-message.js
Expand Up @@ -134,6 +134,7 @@ export const ErrorMessages = makeErrorTemplates(
ParamDupe: "Argument name clash.",
PatternHasAccessor: "Object pattern can't contain getter or setter.",
PatternHasMethod: "Object pattern can't contain methods.",
// This error is only used by the smart-mix proposal
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:
Expand All @@ -144,8 +145,8 @@ export const ErrorMessages = makeErrorTemplates(
'Invalid topic token %0. In order to use %0 as a topic reference, the pipelineOperator plugin must be configured with { "proposal": "hack", "topicToken": "%0" }.',
PipeTopicUnused:
"Hack-style pipe body does not contain a topic reference; Hack-style pipes must use topic at least once.",
PipeUnparenthesizedYield:
"Hack-style pipe body cannot be an unparenthesized yield expression; please wrap it in parentheses: x |> (yield y) |> z.",
PipeUnparenthesizedBody:
"Hack-style pipe body cannot be an unparenthesized %0 expression; please wrap it in parentheses.",

// Messages whose codes start with “Pipeline” or “PrimaryTopic”
// are retained for backwards compatibility
Expand Down
16 changes: 14 additions & 2 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -61,6 +61,7 @@ import { cloneIdentifier } from "./node";
import type { SourceType } from "../options";
*/

const hasOwn = Function.call.bind(Object.prototype.hasOwnProperty);
export default class ExpressionParser extends LValParser {
// Forward-declaration: defined in statement.js
/*::
Expand Down Expand Up @@ -526,8 +527,19 @@ export default class ExpressionParser extends LValParser {

const body = this.parseMaybeAssign();

if (body.type === "YieldExpression" && !body.extra?.parenthesized) {
this.raise(start, Errors.PipeUnparenthesizedYield);
const invalidBodies = {
ArrowFunctionExpression: "arrow function",
AssignmentExpression: "assignment",
ConditionalExpression: "conditional",
YieldExpression: "yield",
};

if (hasOwn(invalidBodies, body.type) && !body.extra?.parenthesized) {
this.raise(
start,
Errors.PipeUnparenthesizedBody,
invalidBodies[body.type],
);
}
if (!this.topicReferenceWasUsedInCurrentContext()) {
// A Hack pipe body must use the topic reference at least once.
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized arrow function expression; please wrap it in parentheses. (1:6)"
],
"program": {
"type": "Program",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized assignment expression; please wrap it in parentheses. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized assignment expression; please wrap it in parentheses. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized assignment expression; please wrap it in parentheses. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
Expand Down
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized yield expression; please wrap it in parentheses: x |> (yield y) |> z. (2:14)"
"SyntaxError: Hack-style pipe body cannot be an unparenthesized yield expression; please wrap it in parentheses. (2:14)"
],
"program": {
"type": "Program",
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized arrow function expression; please wrap it in parentheses. (1:6)"
],
"program": {
"type": "Program",
"start":0,"end":17,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":17}},
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized assignment expression; please wrap it in parentheses. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":16,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":16}},
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized assignment expression; please wrap it in parentheses. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized assignment expression; please wrap it in parentheses. (1:9)"
],
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":15}},
Expand Down
Expand Up @@ -2,7 +2,7 @@
"type": "File",
"start":0,"end":43,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: Hack-style pipe body cannot be an unparenthesized yield expression; please wrap it in parentheses: x |> (yield y) |> z. (2:14)"
"SyntaxError: Hack-style pipe body cannot be an unparenthesized yield expression; please wrap it in parentheses. (2:14)"
],
"program": {
"type": "Program",
Expand Down

0 comments on commit 9310eec

Please sign in to comment.