From e1c4980e426d37711b64d24838423e529608df50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 8 Jul 2022 12:15:36 +0200 Subject: [PATCH] Default to hash syntax for Record&Tuple --- packages/babel-generator/src/generators/types.ts | 11 +++++++---- .../recordAndTupleSyntaxType/missing-option/input.js | 1 - .../missing-option/options.json | 4 ---- packages/babel-parser/src/plugin-utils.js | 3 ++- packages/babel-parser/src/tokenizer/index.js | 2 +- .../invalid-type-bar-record/options.json | 2 +- .../invalid-type-bar-tuple/options.json | 2 +- .../invalid-type-hash-record/options.json | 2 +- .../invalid-type-hash-tuple/options.json | 2 +- 9 files changed, 14 insertions(+), 15 deletions(-) delete mode 100644 packages/babel-generator/test/fixtures/recordAndTupleSyntaxType/missing-option/input.js delete mode 100644 packages/babel-generator/test/fixtures/recordAndTupleSyntaxType/missing-option/options.json diff --git a/packages/babel-generator/src/generators/types.ts b/packages/babel-generator/src/generators/types.ts index 552b81faade6..0ff05db52a9d 100644 --- a/packages/babel-generator/src/generators/types.ts +++ b/packages/babel-generator/src/generators/types.ts @@ -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); diff --git a/packages/babel-generator/test/fixtures/recordAndTupleSyntaxType/missing-option/input.js b/packages/babel-generator/test/fixtures/recordAndTupleSyntaxType/missing-option/input.js deleted file mode 100644 index 6412030c4216..000000000000 --- a/packages/babel-generator/test/fixtures/recordAndTupleSyntaxType/missing-option/input.js +++ /dev/null @@ -1 +0,0 @@ -#{ a: 1 } diff --git a/packages/babel-generator/test/fixtures/recordAndTupleSyntaxType/missing-option/options.json b/packages/babel-generator/test/fixtures/recordAndTupleSyntaxType/missing-option/options.json deleted file mode 100644 index 09a736a1a286..000000000000 --- a/packages/babel-generator/test/fixtures/recordAndTupleSyntaxType/missing-option/options.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "plugins": [["recordAndTuple", { "syntaxType": "hash" }]], - "throws": "The \"recordAndTupleSyntaxType\" generator option must be \"bar\" or \"hash\" (undefined received)" -} diff --git a/packages/babel-parser/src/plugin-utils.js b/packages/babel-parser/src/plugin-utils.js index f663d5552e2d..9691d5f4387b 100644 --- a/packages/babel-parser/src/plugin-utils.js +++ b/packages/babel-parser/src/plugin-utils.js @@ -185,12 +185,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(", "), ); } diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 7c24f2e148a5..8a3fad235bb5 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -519,7 +519,7 @@ export default 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 diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-bar-record/options.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-bar-record/options.json index f266a14d42eb..da006416f7ce 100644 --- a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-bar-record/options.json +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-bar-record/options.json @@ -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'" } diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-bar-tuple/options.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-bar-tuple/options.json index f266a14d42eb..da006416f7ce 100644 --- a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-bar-tuple/options.json +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-bar-tuple/options.json @@ -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'" } diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-hash-record/options.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-hash-record/options.json index f266a14d42eb..da006416f7ce 100644 --- a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-hash-record/options.json +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-hash-record/options.json @@ -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'" } diff --git a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-hash-tuple/options.json b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-hash-tuple/options.json index f266a14d42eb..da006416f7ce 100644 --- a/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-hash-tuple/options.json +++ b/packages/babel-parser/test/fixtures/experimental/record-and-tuple/invalid-type-hash-tuple/options.json @@ -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'" }