Skip to content

Commit

Permalink
parser: Check conflict between hash topic tokens and tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
js-choi committed Jun 1, 2021
1 parent 62c52eb commit 7cdf3ac
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 1 deletion.
1 change: 0 additions & 1 deletion packages/babel-parser/src/parser/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export const ErrorMessages = makeErrorTemplates(
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:
Expand Down
14 changes: 14 additions & 0 deletions packages/babel-parser/src/plugin-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ export function validatePlugins(plugins: PluginList) {
);
}

const tupleSyntaxIsHash =
hasPlugin(plugins, "recordAndTuple") &&
getPluginOption(plugins, "recordAndTuple", "syntaxType") === "hash";

if (proposal === "hack") {
const topicToken = getPluginOption(
plugins,
Expand All @@ -99,6 +103,16 @@ export function validatePlugins(plugins: PluginList) {
`"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"}]`.',
);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[0];
1 |> #[0];
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "#" }],
["recordAndTuple", { "syntaxType": "hash" }]
],
"throws": "Plugin conflict between `[\"pipelineOperator\", { proposal: \"hack\", topicToken: \"#\" }]` and `[\"recordAndtuple\", { syntaxType: \"hash\"}]`."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[0];
1 |> #[0];
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
["pipelineOperator", { "proposal": "smart" }],
["recordAndTuple", { "syntaxType": "hash" }]
],
"throws": "Plugin conflict between `[\"pipelineOperator\", { proposal: \"smart\" }]` and `[\"recordAndtuple\", { syntaxType: \"hash\"}]`."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function *myGenerator(n) {
return n
|> (yield #)
|> Math.abs(#);
}

const myIterator = myGenerator(15);

const yieldedValue = myIterator.next().value;
const returnedValue = myIterator.next(-30).value;

expect(yieldedValue).toBe(15);
expect(returnedValue).toBe(30);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
["proposal-pipeline-operator", { "proposal": "hack", "topicToken": "#" }],
["proposal-record-and-tuple", { "syntaxType": "hash" }]
],
"throws": "Plugin conflict between `[\"pipelineOperator\", { proposal: \"hack\", topicToken: \"#\" }]` and `[\"recordAndtuple\", { syntaxType: \"hash\"}]`."
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function *myGenerator(n) {
return n
|> (yield #)
|> Math.abs(#);
}

const myIterator = myGenerator(15);

const yieldedValue = myIterator.next().value;
const returnedValue = myIterator.next(-30).value;

expect(yieldedValue).toBe(15);
expect(returnedValue).toBe(30);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"plugins": [
["proposal-pipeline-operator", { "proposal": "smart" }],
["proposal-record-and-tuple", { "syntaxType": "hash" }]
],
"throws": "Plugin conflict between `[\"pipelineOperator\", { proposal: \"smart\" }]` and `[\"recordAndtuple\", { syntaxType: \"hash\"}]`."
}

0 comments on commit 7cdf3ac

Please sign in to comment.