Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to hash syntax for Record&Tuple #14744

Merged
merged 2 commits into from Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -187,12 +187,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