Skip to content

Commit

Permalink
[Babel 8]: rename typeParameters to typeArguments for `TSClassImp…
Browse files Browse the repository at this point in the history
…lements` and `TSInterfaceHeritage` (#17017)

* breaking: rename typeParameters to typeArguments for TSHeritage

* chore: add trailing linebreak to options.json

* update test fixtures

* add missing todo item

* breaking: remove TSExpressionWithTypeArguments generator

* typings: export deprecated AST types
  • Loading branch information
JLHwung authored Dec 11, 2024
1 parent ad0f525 commit 8b6fb94
Show file tree
Hide file tree
Showing 25 changed files with 106 additions and 62 deletions.
22 changes: 22 additions & 0 deletions packages/babel-generator/src/generators/deprecated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type Printer from "../printer";
import type * as t from "@babel/types";

export type DeprecatedBabel7ASTTypes = "Noop" | "TSExpressionWithTypeArguments";

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) {},
TSExpressionWithTypeArguments(
this: Printer,
// @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
node: t.TSExpressionWithTypeArguments,
) {
this.print(node.expression);
this.print(node.typeParameters);
},
};
Object.assign(PrinterClass.prototype, deprecatedBabel7Generators);
}
}
16 changes: 6 additions & 10 deletions packages/babel-generator/src/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,20 +505,16 @@ export function TSLiteralType(this: Printer, node: t.TSLiteralType) {
export function TSClassImplements(
this: Printer,
// TODO(Babel 8): Just use t.TSClassImplements
node: Extract<
t.Node,
{ type: "TSClassImplements" | "TSExpressionWithTypeArguments" }
>,
node: t.Node & {
expression: t.TSEntityName;
typeArguments?: t.TSTypeParameterInstantiation;
},
) {
this.print(node.expression);
this.print(node.typeParameters);
this.print(node.typeArguments);
}

export {
// TODO: Remove this in Babel 8
TSClassImplements as TSExpressionWithTypeArguments,
TSClassImplements as TSInterfaceHeritage,
};
export { TSClassImplements as TSInterfaceHeritage };

export function TSInterfaceDeclaration(
this: Printer,
Expand Down
10 changes: 6 additions & 4 deletions packages/babel-generator/src/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ 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 SourceMap from "./source-map.ts";
import type { TraceMap } from "@jridgewell/trace-mapping";
import type { Token } from "@babel/parser";
Expand Down Expand Up @@ -690,8 +694,7 @@ class Printer {
this[
nodeType as Exclude<
t.Node["type"],
// removed
| "Noop"
| DeprecatedBabel7ASTTypes
// renamed
| t.DeprecatedAliases["type"]
>
Expand Down Expand Up @@ -1407,8 +1410,7 @@ class Printer {
Object.assign(Printer.prototype, generatorFunctions);

if (!process.env.BABEL_8_BREAKING) {
// @ts-ignore(Babel 7 vs Babel 8) Babel 7 has Noop print method
Printer.prototype.Noop = function Noop(this: Printer) {};
addDeprecatedGenerators(Printer);
}

type GeneratorFunctions = typeof generatorFunctions;
Expand Down
11 changes: 8 additions & 3 deletions packages/babel-generator/test/printer.skip-bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,20 @@ describe("Printer", () => {

Object.keys(Printer.prototype).forEach(function (type) {
if (IS_BABEL_8()) {
if (type === "TSExpressionWithTypeArguments") return;
// Babel 7 AST
if (
type === "DecimalLiteral" ||
type === "TSExpressionWithTypeArguments"
) {
return;
}
} else {
// Babel 8 AST
if (type === "TSClassImplements" || type === "TSInterfaceHeritage") {
return;
}
}

if (type === "DecimalLiteral") return;

if (!/[A-Z]/.test(type[0])) return;

expect(t.VISITOR_KEYS).toHaveProperty(type);
Expand Down
31 changes: 19 additions & 12 deletions packages/babel-parser/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1708,19 +1708,26 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
N.TSClassImplements | N.TSInterfaceHeritage
>();
node.expression = this.tsParseEntityName();
if (this.match(tt.lt)) {
node.typeParameters = this.tsParseTypeArguments();
}
if (process.env.BABEL_8_BREAKING) {
if (this.match(tt.lt)) {
node.typeArguments = this.tsParseTypeArguments();
}
return this.finishNode(
node,
token === "extends" ? "TSInterfaceHeritage" : "TSClassImplements",
);
} else {
if (this.match(tt.lt)) {
// @ts-expect-error Babel 7 vs Babel 8
node.typeParameters = this.tsParseTypeArguments();
}

return this.finishNode(
node,
// @ts-expect-error Babel 7 vs Babel 8
process.env.BABEL_8_BREAKING
? token === "extends"
? "TSInterfaceHeritage"
: "TSClassImplements"
: "TSExpressionWithTypeArguments",
);
return this.finishNode(
node,
// @ts-expect-error Babel 7 vs Babel 8
"TSExpressionWithTypeArguments",
);
}
},
);

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1750,7 +1750,7 @@ export interface TSInterfaceBody extends NodeBase {

export interface TSHeritageBase extends NodeBase {
expression: TsEntityName;
typeParameters?: TsTypeParameterInstantiation;
typeArguments?: TsTypeParameterInstantiation;
}

export interface TSClassImplements extends TSHeritageBase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"BABEL_8_BREAKING": false
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"BABEL_8_BREAKING": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"name": "Y"
}
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":36,"end":39,"loc":{"start":{"line":1,"column":36,"index":36},"end":{"line":1,"column":39,"index":39}},
"params": [
Expand Down Expand Up @@ -139,7 +139,7 @@
"name": "Y"
}
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":83,"end":86,"loc":{"start":{"line":2,"column":38,"index":83},"end":{"line":2,"column":41,"index":86}},
"params": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"name": "Y"
}
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":21,"end":24,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":24,"index":24}},
"params": [
Expand Down Expand Up @@ -90,7 +90,7 @@
"name": "Y"
}
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":53,"end":56,"loc":{"start":{"line":2,"column":23,"index":53},"end":{"line":2,"column":26,"index":56}},
"params": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"BABEL_8_BREAKING": false
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"BABEL_8_BREAKING": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"name": "Y"
}
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":37,"end":40,"loc":{"start":{"line":1,"column":37,"index":37},"end":{"line":1,"column":40,"index":40}},
"params": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"name": "Y"
}
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":22,"end":25,"loc":{"start":{"line":1,"column":22,"index":22},"end":{"line":1,"column":25,"index":25}},
"params": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"name": "Y"
}
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":23,"end":26,"loc":{"start":{"line":1,"column":23,"index":23},"end":{"line":1,"column":26,"index":26}},
"params": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"start":20,"end":21,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":21,"index":21},"identifierName":"B"},
"name": "B"
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":21,"end":23,"loc":{"start":{"line":1,"column":21,"index":21},"end":{"line":1,"column":23,"index":23}},
"params": []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20},"identifierName":"B"},
"name": "B"
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":20,"end":22,"loc":{"start":{"line":1,"column":20,"index":20},"end":{"line":1,"column":22,"index":22}},
"params": []
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"BABEL_8_BREAKING": false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"jsx",
"typescript"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
"jsx",
"typescript"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2875,7 +2875,7 @@
"start":2348,"end":2354,"loc":{"start":{"line":129,"column":40,"index":2348},"end":{"line":129,"column":46,"index":2354},"identifierName":"Parent"},
"name": "Parent"
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":2354,"end":2357,"loc":{"start":{"line":129,"column":46,"index":2354},"end":{"line":129,"column":49,"index":2357}},
"params": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"BABEL_8_BREAKING": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2809,7 +2809,7 @@
"start":2313,"end":2319,"loc":{"start":{"line":126,"column":40,"index":2313},"end":{"line":126,"column":46,"index":2319},"identifierName":"Parent"},
"name": "Parent"
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":2319,"end":2322,"loc":{"start":{"line":126,"column":46,"index":2319},"end":{"line":126,"column":49,"index":2322}},
"params": [
Expand Down
36 changes: 24 additions & 12 deletions packages/babel-types/src/definitions/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,32 @@ defineType("TSLiteralType", {
},
});

const expressionWithTypeArguments = {
aliases: ["TSType"],
visitor: ["expression", "typeParameters"],
fields: {
expression: validateType("TSEntityName"),
typeParameters: validateOptionalType("TSTypeParameterInstantiation"),
},
};

if (process.env.BABEL_8_BREAKING) {
defineType("TSClassImplements", expressionWithTypeArguments);
defineType("TSInterfaceHeritage", expressionWithTypeArguments);
defineType("TSClassImplements", {
aliases: ["TSType"],
visitor: ["expression", "typeArguments"],
fields: {
expression: validateType("TSEntityName"),
typeArguments: validateOptionalType("TSTypeParameterInstantiation"),
},
});
defineType("TSInterfaceHeritage", {
aliases: ["TSType"],
visitor: ["expression", "typeArguments"],
fields: {
expression: validateType("TSEntityName"),
typeArguments: validateOptionalType("TSTypeParameterInstantiation"),
},
});
} else {
defineType("TSExpressionWithTypeArguments", expressionWithTypeArguments);
defineType("TSExpressionWithTypeArguments", {
aliases: ["TSType"],
visitor: ["expression", "typeParameters"],
fields: {
expression: validateType("TSEntityName"),
typeParameters: validateOptionalType("TSTypeParameterInstantiation"),
},
});
}

defineType("TSInterfaceDeclaration", {
Expand Down
2 changes: 1 addition & 1 deletion scripts/generate-babel-7-tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function assignJSONFile(jsonFile, inputProperties) {
}
}
try {
writeFileSync(jsonFile, JSON.stringify(options, null, 2), {
writeFileSync(jsonFile, JSON.stringify(options, null, 2) + "\n", {
recursive: true,
});
console.log(`${jsonFileExists ? "Updated" : "Created"} ${jsonFile}`);
Expand Down

0 comments on commit 8b6fb94

Please sign in to comment.