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

Remove syntaxType option for record-and-tuple (parser&plugin) #16458

Merged
merged 10 commits into from
May 2, 2024
10 changes: 7 additions & 3 deletions eslint/babel-eslint-parser/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import escope from "eslint-scope";
import unpad from "dedent";
import { parseForESLint as parseForESLintOriginal } from "../lib/index.cjs";
import { ESLint } from "eslint";
import { itDummy, commonJS } from "$repo-utils";
import { itDummy, commonJS, IS_BABEL_8, itBabel7 } from "$repo-utils";

function parseForESLint(code, options) {
return parseForESLintOriginal(code, {
Expand Down Expand Up @@ -382,7 +382,11 @@ describe("Babel and Espree", () => {
babelOptions: {
filename: "test.js",
parserOpts: {
plugins: [["recordAndTuple", { syntaxType: "hash" }]],
plugins: [
IS_BABEL_8()
? "recordAndTuple"
: ["recordAndTuple", { syntaxType: "hash" }],
],
tokens: true,
},
},
Expand All @@ -395,7 +399,7 @@ describe("Babel and Espree", () => {
);
});

it("brace and bracket bar operator (token)", () => {
itBabel7("brace and bracket bar operator (token)", () => {
const code = "{||}; [||]";
const babylonAST = parseForESLint(code, {
eslintVisitorKeys: true,
Expand Down
50 changes: 30 additions & 20 deletions packages/babel-generator/src/generators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,26 @@ export function RecordExpression(this: Printer, node: t.RecordExpression) {

let startToken;
let endToken;
if (this.format.recordAndTupleSyntaxType === "bar") {
startToken = "{|";
endToken = "|}";
} 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 {
if (process.env.BABEL_8_BREAKING) {
startToken = "#{";
endToken = "}";
} else {
if (this.format.recordAndTupleSyntaxType === "bar") {
startToken = "{|";
endToken = "|}";
} 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 All @@ -146,16 +151,21 @@ export function TupleExpression(this: Printer, node: t.TupleExpression) {

let startToken;
let endToken;
if (this.format.recordAndTupleSyntaxType === "bar") {
startToken = "[|";
endToken = "|]";
} else if (this.format.recordAndTupleSyntaxType === "hash") {
if (process.env.BABEL_8_BREAKING) {
startToken = "#[";
endToken = "]";
} else {
throw new Error(
`${this.format.recordAndTupleSyntaxType} is not a valid recordAndTuple syntax type`,
);
if (this.format.recordAndTupleSyntaxType === "bar") {
startToken = "[|";
endToken = "|]";
} else if (this.format.recordAndTupleSyntaxType === "hash") {
startToken = "#[";
endToken = "]";
} else {
throw new Error(
`${this.format.recordAndTupleSyntaxType} is not a valid recordAndTuple syntax type`,
);
}
}

this.token(startToken);
Expand Down
3 changes: 2 additions & 1 deletion packages/babel-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ function normalizeOptions(
minimal: process.env.BABEL_8_BREAKING ? true : false,
...opts.jsescOption,
},
recordAndTupleSyntaxType: opts.recordAndTupleSyntaxType ?? "hash",
topicToken: opts.topicToken,
importAttributesKeyword: opts.importAttributesKeyword,
};

if (!process.env.BABEL_8_BREAKING) {
format.decoratorsBeforeExport = opts.decoratorsBeforeExport;
format.jsescOption.json = opts.jsonCompatibleStrings;
format.recordAndTupleSyntaxType = opts.recordAndTupleSyntaxType ?? "hash";
}

if (format.minified) {
Expand Down Expand Up @@ -190,6 +190,7 @@ export interface GeneratorOptions {

/**
* For use with the recordAndTuple token.
* @deprecated It will be removed in Babel 8.
*/
recordAndTupleSyntaxType?: RecordAndTuplePluginOptions["syntaxType"];

Expand Down
5 changes: 4 additions & 1 deletion packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export type Format = {
adjustMultilineComment: boolean;
style: string;
};
recordAndTupleSyntaxType: RecordAndTuplePluginOptions["syntaxType"];
/**
* @deprecated Removed in Babel 8, syntax type is always 'hash'
*/
recordAndTupleSyntaxType?: RecordAndTuplePluginOptions["syntaxType"];
jsescOption: jsescOptions;
/**
* @deprecated Removed in Babel 8, use `jsescOption` instead
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#[#{}]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"plugins": [["recordAndTuple"]]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#[#{}];
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"recordAndTupleSyntaxType": "bar",
"plugins": [["recordAndTuple", { "syntaxType": "bar" }]],
"sourceMaps": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"b"
],
"sources": [
"fixtures/sourcemaps/bar-record-tuple/input.js"
"fixtures/sourcemaps/bar-record-tuple-babel-7/input.js"
],
"sourcesContent": [
"let a = [| 1 |];\nlet b = {| a: 1 |};"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let a = #[1, 2, 3];
let b = #{a: 1, b: 2, c: 3};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"BABEL_8_BREAKING": false,
"recordAndTupleSyntaxType": "hash",
"plugins": [["recordAndTuple", { "syntaxType": "hash" }]],
"sourceMaps": true
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
let a = #[1, 2, 3];
let b = #{
a: 1,
b: 2,
c: 3
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": 3,
"names": [
"a",
"b",
"c"
],
"sources": [
"fixtures/sourcemaps/hash-record-tuple-babel-7/input.js"
],
"sourcesContent": [
"let a = #[1, 2, 3];\nlet b = #{a: 1, b: 2, c: 3};"
],
"mappings": "AAAA,IAAIA,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AAClB,IAAIC,CAAC,GAAG;EAAED,CAAC,EAAE,CAAC;EAAEC,CAAC,EAAE,CAAC;EAAEC,CAAC,EAAE;AAAC,CAAC",
"ignoreList": []
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"BABEL_8_BREAKING": true,
"recordAndTupleSyntaxType": "hash",
"plugins": [["recordAndTuple", { "syntaxType": "hash" }]],
"plugins": [["recordAndTuple"]],
"sourceMaps": true
}
50 changes: 32 additions & 18 deletions packages/babel-parser/src/plugin-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ export function getPluginOption<

const PIPELINE_PROPOSALS = ["minimal", "fsharp", "hack", "smart"];
const TOPIC_TOKENS = ["^^", "@@", "^", "%", "#"];
const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];

export function validatePlugins(plugins: PluginList) {
if (hasPlugin(plugins, "decorators")) {
Expand Down Expand Up @@ -127,10 +126,13 @@ export function validatePlugins(plugins: PluginList) {
);
}

const tupleSyntaxIsHash = hasPlugin(plugins, [
"recordAndTuple",
{ syntaxType: "hash" },
]);
const recordAndTupleConfigItem:
| "recordAndTuple"
| ["recordAndTuple", { syntaxType: "hash" }] = process.env
.BABEL_8_BREAKING
? "recordAndTuple"
: ["recordAndTuple", { syntaxType: "hash" }];
const tupleSyntaxIsHash = hasPlugin(plugins, recordAndTupleConfigItem);

if (proposal === "hack") {
if (hasPlugin(plugins, "placeholders")) {
Expand Down Expand Up @@ -161,12 +163,12 @@ export function validatePlugins(plugins: PluginList) {

if (topicToken === "#" && tupleSyntaxIsHash) {
throw new Error(
'Plugin conflict between `["pipelineOperator", { proposal: "hack", topicToken: "#" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.',
`Plugin conflict between \`["pipelineOperator", { proposal: "hack", topicToken: "#" }]\` and \`${JSON.stringify(recordAndTupleConfigItem)}\`.`,
);
}
} else if (proposal === "smart" && tupleSyntaxIsHash) {
throw new Error(
'Plugin conflict between `["pipelineOperator", { proposal: "smart" }]` and `["recordAndtuple", { syntaxType: "hash"}]`.',
`Plugin conflict between \`["pipelineOperator", { proposal: "smart" }]\` and \`${JSON.stringify(recordAndTupleConfigItem)}\`.`,
);
}
}
Expand Down Expand Up @@ -208,17 +210,29 @@ 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(
"The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " +
RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "),
);
if (hasPlugin(plugins, "recordAndTuple")) {
const syntaxType = getPluginOption(plugins, "recordAndTuple", "syntaxType");
if (syntaxType != null) {
if (process.env.BABEL_8_BREAKING) {
if (syntaxType === "hash") {
throw new Error(
'The syntaxType option is no longer required in Babel 8. You can safely remove { syntaxType: "hash" } from the recordAndTuple config.',
);
} else {
throw new Error(
'The syntaxType option is no longer required in Babel 8. Please remove { syntaxType: "bar" } from the recordAndTuple config and migrate to the hash syntax #{} and #[].',
);
}
} else {
const RECORD_AND_TUPLE_SYNTAX_TYPES = ["hash", "bar"];
if (!RECORD_AND_TUPLE_SYNTAX_TYPES.includes(syntaxType)) {
throw new Error(
"The 'syntaxType' option of the 'recordAndTuple' plugin must be one of: " +
RECORD_AND_TUPLE_SYNTAX_TYPES.map(p => `'${p}'`).join(", "),
);
}
}
}
}

if (
Expand Down
9 changes: 8 additions & 1 deletion packages/babel-parser/src/tokenizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,10 @@ 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") === "bar") {
if (
!process.env.BABEL_8_BREAKING &&
this.getPluginOption("recordAndTuple", "syntaxType") === "bar"
) {
throw this.raise(
next === charCodes.leftCurlyBrace
? Errors.RecordExpressionHashIncorrectStartSyntaxType
Expand Down Expand Up @@ -640,6 +643,7 @@ export default abstract class Tokenizer extends CommentsParser {
}
// '|}'
if (
!process.env.BABEL_8_BREAKING &&
this.hasPlugin("recordAndTuple") &&
next === charCodes.rightCurlyBrace
) {
Expand All @@ -656,6 +660,7 @@ export default abstract class Tokenizer extends CommentsParser {

// '|]'
if (
!process.env.BABEL_8_BREAKING &&
this.hasPlugin("recordAndTuple") &&
next === charCodes.rightSquareBracket
) {
Expand Down Expand Up @@ -872,6 +877,7 @@ export default abstract class Tokenizer extends CommentsParser {
return;
case charCodes.leftSquareBracket:
if (
!process.env.BABEL_8_BREAKING &&
this.hasPlugin("recordAndTuple") &&
this.input.charCodeAt(this.state.pos + 1) === charCodes.verticalBar
) {
Expand All @@ -896,6 +902,7 @@ export default abstract class Tokenizer extends CommentsParser {
return;
case charCodes.leftCurlyBrace:
if (
!process.env.BABEL_8_BREAKING &&
this.hasPlugin("recordAndTuple") &&
this.input.charCodeAt(this.state.pos + 1) === charCodes.verticalBar
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
"moduleBlocks",
[
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,18 @@
{
"BABEL_8_BREAKING": false,
"plugins": [
[
"pipelineOperator",
{
"proposal": "hack",
"topicToken": "^"
}
],
[
"recordAndTuple",
{
"syntaxType": "hash"
}
]
]
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
{
"BABEL_8_BREAKING": true,
"plugins": [
["pipelineOperator", { "proposal": "hack", "topicToken": "^" }],
["recordAndTuple", { "syntaxType": "hash" }]
[
"pipelineOperator",
{
"proposal": "hack",
"topicToken": "^"
}
],
[
"recordAndTuple"
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[0];
1 |> #[0, @@];