Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
176 changes: 80 additions & 96 deletions packages/babel-generator/src/generators/deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,112 +8,96 @@ export type DeprecatedBabel7ASTTypes =
| "RecordExpression"
| "TupleExpression";

export function addDeprecatedGenerators(PrinterClass: typeof Printer) {
// Add Babel 7 generator methods that is removed in Babel 8
if (!process.env.BABEL_8_BREAKING) {
const deprecatedBabel7Generators = {
Noop(this: Printer) {},
export function Noop(this: Printer) {}

TSExpressionWithTypeArguments(
this: Printer,
// @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
node: t.TSExpressionWithTypeArguments,
) {
this.print(node.expression);
this.print(node.typeParameters);
},

DecimalLiteral(this: Printer, node: any) {
const raw = this.getPossibleRaw(node);
if (!this.format.minified && raw !== undefined) {
this.word(raw);
return;
}
this.word(node.value + "m");
},
export function TSExpressionWithTypeArguments(
this: Printer,
// @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
node: t.TSExpressionWithTypeArguments,
) {
this.print(node.expression);
this.print(node.typeParameters);
}

// @ts-ignore(Babel 7 vs Babel 8) - t.RecordExpression only exists in Babel 7
RecordExpression(this: Printer, node: t.RecordExpression) {
const props = node.properties;
export function DecimalLiteral(this: Printer, node: any) {
const raw = this.getPossibleRaw(node);
if (!this.format.minified && raw !== undefined) {
this.word(raw);
return;
}
this.word(node.value + "m");
}

let startToken;
let endToken;
// @ts-ignore(Babel 7 vs Babel 8) - t.RecordExpression only exists in Babel 7
export function RecordExpression(this: Printer, node: t.RecordExpression) {
const props = node.properties;

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 = "}";
}
let startToken;
let endToken;

this.token(startToken);
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 = "}";
}

if (props.length) {
this.space();
this.printList(
props,
this.shouldPrintTrailingComma(endToken),
true,
true,
);
this.space();
}
this.token(endToken);
},
this.token(startToken);

// @ts-ignore(Babel 7 vs Babel 8) - t.TupleExpression only exists in Babel 7
TupleExpression(this: Printer, node: t.TupleExpression) {
const elems = node.elements;
const len = elems.length;
if (props.length) {
this.space();
this.printList(props, this.shouldPrintTrailingComma(endToken), true, true);
this.space();
}
this.token(endToken);
}

let startToken;
let endToken;
if (process.env.BABEL_8_BREAKING) {
startToken = "#[";
endToken = "]";
} else {
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`,
);
}
}
// @ts-ignore(Babel 7 vs Babel 8) - t.TupleExpression only exists in Babel 7
export function TupleExpression(this: Printer, node: t.TupleExpression) {
const elems = node.elements;
const len = elems.length;

this.token(startToken);
let startToken;
let endToken;
if (process.env.BABEL_8_BREAKING) {
startToken = "#[";
endToken = "]";
} else {
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`,
);
}
}

for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
if (elem) {
if (i > 0) this.space();
this.print(elem);
if (i < len - 1 || this.shouldPrintTrailingComma(endToken)) {
this.token(",", false, i);
}
}
}
this.token(startToken);

this.token(endToken);
},
} satisfies Record<
DeprecatedBabel7ASTTypes,
(this: Printer, node: any) => void
>;
Object.assign(PrinterClass.prototype, deprecatedBabel7Generators);
for (let i = 0; i < elems.length; i++) {
const elem = elems[i];
if (elem) {
if (i > 0) this.space();
this.print(elem);
if (i < len - 1 || this.shouldPrintTrailingComma(endToken)) {
this.token(",", false, i);
}
}
}

this.token(endToken);
}
8 changes: 3 additions & 5 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import type { Opts as jsescOptions } from "jsesc";
import { TokenMap } from "./token-map.ts";
import type { GeneratorOptions } from "./index.ts";
import * as generatorFunctions from "./generators/index.ts";
import {
addDeprecatedGenerators,
type DeprecatedBabel7ASTTypes,
} from "./generators/deprecated.ts";
import type { DeprecatedBabel7ASTTypes } from "./generators/deprecated.ts";
import * as deprecatedGeneratorFunctions from "./generators/deprecated.ts" with { if: "!process.env.BABEL_8_BREAKING" };
import type SourceMap from "./source-map.ts";
import type { TraceMap } from "@jridgewell/trace-mapping";
import type { Token } from "@babel/parser";
Expand Down Expand Up @@ -1420,7 +1418,7 @@ class Printer {
Object.assign(Printer.prototype, generatorFunctions);

if (!process.env.BABEL_8_BREAKING) {
addDeprecatedGenerators(Printer);
Object.assign(Printer.prototype, deprecatedGeneratorFunctions);
}

type GeneratorFunctions = typeof generatorFunctions;
Expand Down
10 changes: 0 additions & 10 deletions packages/babel-runtime-corejs3/helpers/esm/regeneratorDefineIM.js

This file was deleted.

14 changes: 0 additions & 14 deletions packages/babel-runtime-corejs3/helpers/esm/tryCatch.js

This file was deleted.

Loading
Loading