Skip to content

Commit

Permalink
Default to hash syntax for Record&Tuple (#14744)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Sep 2, 2022
1 parent 74853c7 commit 14ea25f
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 13 deletions.
11 changes: 7 additions & 4 deletions packages/babel-generator/src/generators/types.ts
Expand Up @@ -117,15 +117,18 @@ export function RecordExpression(this: Printer, node: t.RecordExpression) {
if (this.format.recordAndTupleSyntaxType === "bar") {
startToken = "{|";
endToken = "|}";
} else if (this.format.recordAndTupleSyntaxType === "hash") {
startToken = "#{";
endToken = "}";
} else {
} else if (
this.format.recordAndTupleSyntaxType !== "hash" &&
this.format.recordAndTupleSyntaxType != null
) {
throw new Error(
`The "recordAndTupleSyntaxType" generator option must be "bar" or "hash" (${JSON.stringify(
this.format.recordAndTupleSyntaxType,
)} received).`,
);
} else {
startToken = "#{";
endToken = "}";
}

this.token(startToken);
Expand Down
@@ -1,4 +1,3 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "hash" }]],
"throws": "The \"recordAndTupleSyntaxType\" generator option must be \"bar\" or \"hash\" (undefined received)"
"plugins": ["recordAndTuple"]
}
@@ -0,0 +1,3 @@
#{
a: 1
};
3 changes: 2 additions & 1 deletion packages/babel-parser/src/plugin-utils.ts
Expand Up @@ -189,12 +189,13 @@ export function validatePlugins(plugins: PluginList) {

if (
hasPlugin(plugins, "recordAndTuple") &&
getPluginOption(plugins, "recordAndTuple", "syntaxType") != null &&
!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(
getPluginOption(plugins, "recordAndTuple", "syntaxType"),
)
) {
throw new Error(
"'recordAndTuple' requires 'syntaxType' option whose value should be one of: " +
"The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " +
RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "),
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/tokenizer/index.ts
Expand Up @@ -493,7 +493,7 @@ export default abstract class Tokenizer extends CommentsParser {
// which is not allowed in the spec. Throwing expecting recordAndTuple is
// misleading
this.expectPlugin("recordAndTuple");
if (this.getPluginOption("recordAndTuple", "syntaxType") !== "hash") {
if (this.getPluginOption("recordAndTuple", "syntaxType") === "bar") {
throw this.raise(
next === charCodes.leftCurlyBrace
? Errors.RecordExpressionHashIncorrectStartSyntaxType
Expand Down
@@ -0,0 +1 @@
#{ a: 1 }
@@ -0,0 +1,3 @@
{
"plugins": ["recordAndTuple"]
}
@@ -0,0 +1,44 @@
{
"type": "File",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}},
"program": {
"type": "Program",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}},
"expression": {
"type": "RecordExpression",
"start":0,"end":9,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":9,"index":9}},
"properties": [
{
"type": "ObjectProperty",
"start":3,"end":7,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":7,"index":7}},
"method": false,
"key": {
"type": "Identifier",
"start":3,"end":4,"loc":{"start":{"line":1,"column":3,"index":3},"end":{"line":1,"column":4,"index":4},"identifierName":"a"},
"name": "a"
},
"computed": false,
"shorthand": false,
"value": {
"type": "NumericLiteral",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
]
}
}
],
"directives": []
}
}
@@ -1,4 +1,4 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "invalid" }]],
"throws": "'recordAndTuple' requires 'syntaxType' option whose value should be one of: 'hash', 'bar'"
"throws": "The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: 'hash', 'bar'"
}
@@ -1,4 +1,4 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "invalid" }]],
"throws": "'recordAndTuple' requires 'syntaxType' option whose value should be one of: 'hash', 'bar'"
"throws": "The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: 'hash', 'bar'"
}
@@ -1,4 +1,4 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "invalid" }]],
"throws": "'recordAndTuple' requires 'syntaxType' option whose value should be one of: 'hash', 'bar'"
"throws": "The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: 'hash', 'bar'"
}
@@ -1,4 +1,4 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "invalid" }]],
"throws": "'recordAndTuple' requires 'syntaxType' option whose value should be one of: 'hash', 'bar'"
"throws": "The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: 'hash', 'bar'"
}
3 changes: 2 additions & 1 deletion packages/babel-parser/typings/babel-parser.d.ts
Expand Up @@ -162,6 +162,7 @@ export type ParserPlugin =
| "pipelineOperator"
| "placeholders"
| "privateIn" // Enabled by default
| "recordAndTuple"
| "regexpUnicodeSets"
| "throwExpressions"
| "topLevelAwait"
Expand All @@ -186,7 +187,7 @@ export interface PipelineOperatorPluginOptions {
}

export interface RecordAndTuplePluginOptions {
syntaxType: "bar" | "hash";
syntaxType?: "bar" | "hash";
}

export interface FlowPluginOptions {
Expand Down

0 comments on commit 14ea25f

Please sign in to comment.