Skip to content

Commit

Permalink
[babel 8] Create TSClassImplements|TSInterfaceHeritage nodes (babel…
Browse files Browse the repository at this point in the history
…#16731)

Co-authored-by: Nicolò Ribaudo <[email protected]>
Co-authored-by: Nicolò Ribaudo <[email protected]>
  • Loading branch information
3 people authored Sep 17, 2024
1 parent 60b9670 commit 611d958
Show file tree
Hide file tree
Showing 56 changed files with 991 additions and 39 deletions.
14 changes: 12 additions & 2 deletions packages/babel-generator/src/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,14 +475,24 @@ export function TSLiteralType(this: Printer, node: t.TSLiteralType) {
this.print(node.literal);
}

export function TSExpressionWithTypeArguments(
export function TSClassImplements(
this: Printer,
node: t.TSExpressionWithTypeArguments,
// TODO(Babel 8): Just use t.TSClassImplements
node: Extract<
t.Node,
{ type: "TSClassImplements" | "TSExpressionWithTypeArguments" }
>,
) {
this.print(node.expression);
this.print(node.typeParameters);
}

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

export function TSInterfaceDeclaration(
this: Printer,
node: t.TSInterfaceDeclaration,
Expand Down
14 changes: 12 additions & 2 deletions packages/babel-generator/test/printer.skip-bundled.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import * as t from "@babel/types";
import { IS_BABEL_8 } from "$repo-utils";

import _Printer from "../lib/printer.js";
const Printer = _Printer.default || _Printer;

Expand All @@ -9,11 +11,19 @@ describe("Printer", () => {
});

Object.keys(Printer.prototype).forEach(function (type) {
if (!/[A-Z]/.test(type[0])) return;
if (IS_BABEL_8()) {
if (type === "TSExpressionWithTypeArguments") return;
} else {
if (type === "TSClassImplements" || type === "TSInterfaceHeritage") {
return;
}
}

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

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

expect(t.VISITOR_KEYS).toHaveProperty(type);
});
});
});
22 changes: 18 additions & 4 deletions packages/babel-parser/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1698,21 +1698,33 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
return this.finishNode(node, "TSTypeAssertion");
}

tsParseHeritageClause(token: "extends"): Array<N.TSInterfaceHeritage>;
tsParseHeritageClause(token: "implements"): Array<N.TSClassImplements>;
tsParseHeritageClause(
token: "extends" | "implements",
): Array<N.TsExpressionWithTypeArguments> {
): Array<N.TSClassImplements> | Array<N.TSInterfaceHeritage> {
const originalStartLoc = this.state.startLoc;

const delimitedList = this.tsParseDelimitedList(
"HeritageClauseElement",
() => {
const node = this.startNode<N.TsExpressionWithTypeArguments>();
const node = this.startNode<
N.TSClassImplements | N.TSInterfaceHeritage
>();
node.expression = this.tsParseEntityName();
if (this.match(tt.lt)) {
node.typeParameters = this.tsParseTypeArguments();
}

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

Expand All @@ -1722,7 +1734,9 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
});
}

return delimitedList;
return delimitedList as
| Array<N.TSClassImplements>
| Array<N.TSInterfaceHeritage>;
}

tsParseInterfaceDeclaration(
Expand Down
23 changes: 13 additions & 10 deletions packages/babel-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -867,11 +867,7 @@ export interface ClassBase extends HasDecorators {
typeParameters?: TypeParameterDeclarationBase | null;
superTypeParameters?: TypeParameterInstantiationBase | null;
abstract?: boolean;
implements?:
| TsExpressionWithTypeArguments[]
| undefined
| null
| FlowClassImplements[];
implements?: TSClassImplements[] | undefined | null | FlowClassImplements[];
}

export interface ClassBody extends NodeBase {
Expand Down Expand Up @@ -1734,8 +1730,7 @@ export interface TsInterfaceDeclaration extends DeclarationBase {
type: "TSInterfaceDeclaration";
id: Identifier | undefined | null;
typeParameters: TsTypeParameterDeclaration | undefined | null;
// TS uses "heritageClauses", but want this to resemble ClassBase.
extends?: TsExpressionWithTypeArguments[];
extends?: TSInterfaceHeritage[];
body: TSInterfaceBody;
}

Expand All @@ -1744,12 +1739,19 @@ export interface TSInterfaceBody extends NodeBase {
body: TsTypeElement[];
}

export interface TsExpressionWithTypeArguments extends TsTypeBase {
type: "TSExpressionWithTypeArguments";
export interface TSHeritageBase extends NodeBase {
expression: TsEntityName;
typeParameters?: TsTypeParameterInstantiation;
}

export interface TSClassImplements extends TSHeritageBase {
type: "TSClassImplements";
}

export interface TSInterfaceHeritage extends TSHeritageBase {
type: "TSInterfaceHeritage";
}

export interface TsTypeAliasDeclaration extends DeclarationBase {
type: "TSTypeAliasDeclaration";
id: Identifier;
Expand Down Expand Up @@ -2055,7 +2057,8 @@ export type Node =
| TsEnumDeclaration
| TsEnumMember
| TsExportAssignment
| TsExpressionWithTypeArguments
| TSClassImplements
| TSInterfaceHeritage
| TsExternalModuleReference
| TsFunctionType
| TsImportEqualsDeclaration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(class extends f()<T> implements X.Y<T> {});
(class C extends f()<T> implements X.Y<T> {});
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
@@ -0,0 +1,176 @@
{
"type": "File",
"start":0,"end":91,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":46,"index":91}},
"program": {
"type": "Program",
"start":0,"end":91,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":2,"column":46,"index":91}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":44,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":44,"index":44}},
"expression": {
"type": "ClassExpression",
"start":1,"end":42,"loc":{"start":{"line":1,"column":1,"index":1},"end":{"line":1,"column":42,"index":42}},
"id": null,
"superClass": {
"type": "CallExpression",
"start":15,"end":18,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":18,"index":18}},
"callee": {
"type": "Identifier",
"start":15,"end":16,"loc":{"start":{"line":1,"column":15,"index":15},"end":{"line":1,"column":16,"index":16},"identifierName":"f"},
"name": "f"
},
"arguments": []
},
"superTypeParameters": {
"type": "TSTypeParameterInstantiation",
"start":18,"end":21,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":1,"column":21,"index":21}},
"params": [
{
"type": "TSTypeReference",
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20}},
"typeName": {
"type": "Identifier",
"start":19,"end":20,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":20,"index":20},"identifierName":"T"},
"name": "T"
}
}
]
},
"implements": [
{
"type": "TSExpressionWithTypeArguments",
"start":33,"end":39,"loc":{"start":{"line":1,"column":33,"index":33},"end":{"line":1,"column":39,"index":39}},
"expression": {
"type": "TSQualifiedName",
"start":33,"end":36,"loc":{"start":{"line":1,"column":33,"index":33},"end":{"line":1,"column":36,"index":36}},
"left": {
"type": "Identifier",
"start":33,"end":34,"loc":{"start":{"line":1,"column":33,"index":33},"end":{"line":1,"column":34,"index":34},"identifierName":"X"},
"name": "X"
},
"right": {
"type": "Identifier",
"start":35,"end":36,"loc":{"start":{"line":1,"column":35,"index":35},"end":{"line":1,"column":36,"index":36},"identifierName":"Y"},
"name": "Y"
}
},
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":36,"end":39,"loc":{"start":{"line":1,"column":36,"index":36},"end":{"line":1,"column":39,"index":39}},
"params": [
{
"type": "TSTypeReference",
"start":37,"end":38,"loc":{"start":{"line":1,"column":37,"index":37},"end":{"line":1,"column":38,"index":38}},
"typeName": {
"type": "Identifier",
"start":37,"end":38,"loc":{"start":{"line":1,"column":37,"index":37},"end":{"line":1,"column":38,"index":38},"identifierName":"T"},
"name": "T"
}
}
]
}
}
],
"body": {
"type": "ClassBody",
"start":40,"end":42,"loc":{"start":{"line":1,"column":40,"index":40},"end":{"line":1,"column":42,"index":42}},
"body": []
},
"extra": {
"parenthesized": true,
"parenStart": 0
}
}
},
{
"type": "ExpressionStatement",
"start":45,"end":91,"loc":{"start":{"line":2,"column":0,"index":45},"end":{"line":2,"column":46,"index":91}},
"expression": {
"type": "ClassExpression",
"start":46,"end":89,"loc":{"start":{"line":2,"column":1,"index":46},"end":{"line":2,"column":44,"index":89}},
"id": {
"type": "Identifier",
"start":52,"end":53,"loc":{"start":{"line":2,"column":7,"index":52},"end":{"line":2,"column":8,"index":53},"identifierName":"C"},
"name": "C"
},
"superClass": {
"type": "CallExpression",
"start":62,"end":65,"loc":{"start":{"line":2,"column":17,"index":62},"end":{"line":2,"column":20,"index":65}},
"callee": {
"type": "Identifier",
"start":62,"end":63,"loc":{"start":{"line":2,"column":17,"index":62},"end":{"line":2,"column":18,"index":63},"identifierName":"f"},
"name": "f"
},
"arguments": []
},
"superTypeParameters": {
"type": "TSTypeParameterInstantiation",
"start":65,"end":68,"loc":{"start":{"line":2,"column":20,"index":65},"end":{"line":2,"column":23,"index":68}},
"params": [
{
"type": "TSTypeReference",
"start":66,"end":67,"loc":{"start":{"line":2,"column":21,"index":66},"end":{"line":2,"column":22,"index":67}},
"typeName": {
"type": "Identifier",
"start":66,"end":67,"loc":{"start":{"line":2,"column":21,"index":66},"end":{"line":2,"column":22,"index":67},"identifierName":"T"},
"name": "T"
}
}
]
},
"implements": [
{
"type": "TSExpressionWithTypeArguments",
"start":80,"end":86,"loc":{"start":{"line":2,"column":35,"index":80},"end":{"line":2,"column":41,"index":86}},
"expression": {
"type": "TSQualifiedName",
"start":80,"end":83,"loc":{"start":{"line":2,"column":35,"index":80},"end":{"line":2,"column":38,"index":83}},
"left": {
"type": "Identifier",
"start":80,"end":81,"loc":{"start":{"line":2,"column":35,"index":80},"end":{"line":2,"column":36,"index":81},"identifierName":"X"},
"name": "X"
},
"right": {
"type": "Identifier",
"start":82,"end":83,"loc":{"start":{"line":2,"column":37,"index":82},"end":{"line":2,"column":38,"index":83},"identifierName":"Y"},
"name": "Y"
}
},
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":83,"end":86,"loc":{"start":{"line":2,"column":38,"index":83},"end":{"line":2,"column":41,"index":86}},
"params": [
{
"type": "TSTypeReference",
"start":84,"end":85,"loc":{"start":{"line":2,"column":39,"index":84},"end":{"line":2,"column":40,"index":85}},
"typeName": {
"type": "Identifier",
"start":84,"end":85,"loc":{"start":{"line":2,"column":39,"index":84},"end":{"line":2,"column":40,"index":85},"identifierName":"T"},
"name": "T"
}
}
]
}
}
],
"body": {
"type": "ClassBody",
"start":87,"end":89,"loc":{"start":{"line":2,"column":42,"index":87},"end":{"line":2,"column":44,"index":89}},
"body": []
},
"extra": {
"parenthesized": true,
"parenStart": 45
}
}
}
],
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
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
Expand Up @@ -41,7 +41,7 @@
},
"implements": [
{
"type": "TSExpressionWithTypeArguments",
"type": "TSClassImplements",
"start":33,"end":39,"loc":{"start":{"line":1,"column":33,"index":33},"end":{"line":1,"column":39,"index":39}},
"expression": {
"type": "TSQualifiedName",
Expand Down Expand Up @@ -123,7 +123,7 @@
},
"implements": [
{
"type": "TSExpressionWithTypeArguments",
"type": "TSClassImplements",
"start":80,"end":86,"loc":{"start":{"line":2,"column":35,"index":80},"end":{"line":2,"column":41,"index":86}},
"expression": {
"type": "TSQualifiedName",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(class implements X.Y<T> {});
(class C implements X.Y<T> {});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
Loading

0 comments on commit 611d958

Please sign in to comment.