Skip to content

Commit

Permalink
Rename TSTypeReference.typeParameters to typeArguments (#17008)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung authored Dec 6, 2024
1 parent 5511172 commit c7f26c8
Show file tree
Hide file tree
Showing 89 changed files with 1,960 additions and 137 deletions.
9 changes: 7 additions & 2 deletions packages/babel-generator/src/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,13 @@ export function tsPrintFunctionOrConstructorType(
}

export function TSTypeReference(this: Printer, node: t.TSTypeReference) {
this.print(node.typeName, !!node.typeParameters);
this.print(node.typeParameters);
const typeArguments = process.env.BABEL_8_BREAKING
? // @ts-ignore(Babel 7 vs Babel 8) Babel 8 AST shape
node.typeArguments
: // @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST shape
node.typeParameters;
this.print(node.typeName, !!typeArguments);
this.print(typeArguments);
}

export function TSTypePredicate(this: Printer, node: t.TSTypePredicate) {
Expand Down
6 changes: 5 additions & 1 deletion packages/babel-parser/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,11 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
const node = this.startNode<N.TsTypeReference>();
node.typeName = this.tsParseEntityName();
if (!this.hasPrecedingLineBreak() && this.match(tt.lt)) {
node.typeParameters = this.tsParseTypeArguments();
if (process.env.BABEL_8_BREAKING) {
node.typeArguments = this.tsParseTypeArguments();
} else {
node.typeParameters = this.tsParseTypeArguments();
}
}
return this.finishNode(node, "TSTypeReference");
}
Expand Down
4 changes: 4 additions & 0 deletions packages/babel-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,10 @@ export interface TsConstructorType
export interface TsTypeReference extends TsTypeBase {
type: "TSTypeReference";
typeName: TsEntityName;
typeArguments?: TsTypeParameterInstantiation;
/**
* @deprecated
*/
typeParameters?: TsTypeParameterInstantiation;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class A {
#foo = "bar";
static #bar = foo;
declare #qux: Array<string>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"plugins": [
"typescript",
[
"estree",
{
"classFeatures": true
}
]
],
"BABEL_8_BREAKING": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"type": "File",
"start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
"program": {
"type": "Program",
"start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":79,"loc":{"start":{"line":1,"column":0},"end":{"line":5,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"A"},
"name": "A"
},
"superClass": null,
"body": {
"type": "ClassBody",
"start":8,"end":79,"loc":{"start":{"line":1,"column":8},"end":{"line":5,"column":1}},
"body": [
{
"type": "PropertyDefinition",
"start":12,"end":25,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":15}},
"static": false,
"key": {
"type": "PrivateIdentifier",
"start":12,"end":16,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6}},
"name": "foo"
},
"value": {
"type": "Literal",
"start":19,"end":24,"loc":{"start":{"line":2,"column":9},"end":{"line":2,"column":14}},
"value": "bar",
"raw": "\"bar\""
},
"computed": false
},
{
"type": "PropertyDefinition",
"start":28,"end":46,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":20}},
"static": true,
"key": {
"type": "PrivateIdentifier",
"start":35,"end":39,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":13}},
"name": "bar"
},
"value": {
"type": "Identifier",
"start":42,"end":45,"loc":{"start":{"line":3,"column":16},"end":{"line":3,"column":19},"identifierName":"foo"},
"name": "foo"
},
"computed": false
},
{
"type": "PropertyDefinition",
"start":49,"end":77,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":30}},
"declare": true,
"static": false,
"key": {
"type": "PrivateIdentifier",
"start":57,"end":61,"loc":{"start":{"line":4,"column":10},"end":{"line":4,"column":14}},
"name": "qux"
},
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":61,"end":76,"loc":{"start":{"line":4,"column":14},"end":{"line":4,"column":29}},
"typeAnnotation": {
"type": "TSTypeReference",
"start":63,"end":76,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":29}},
"typeName": {
"type": "Identifier",
"start":63,"end":68,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":21},"identifierName":"Array"},
"name": "Array"
},
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":68,"end":76,"loc":{"start":{"line":4,"column":21},"end":{"line":4,"column":29}},
"params": [
{
"type": "TSStringKeyword",
"start":69,"end":75,"loc":{"start":{"line":4,"column":22},"end":{"line":4,"column":28}}
}
]
}
}
},
"value": null,
"computed": false
}
]
}
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"plugins": ["typescript", ["estree", { "classFeatures": true }]]
}
"plugins": [
"typescript",
[
"estree",
{
"classFeatures": true
}
]
],
"BABEL_8_BREAKING": true
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"start":63,"end":68,"loc":{"start":{"line":4,"column":16},"end":{"line":4,"column":21},"identifierName":"Array"},
"name": "Array"
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":68,"end":76,"loc":{"start":{"line":4,"column":21},"end":{"line":4,"column":29}},
"params": [
Expand All @@ -94,4 +94,4 @@
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class A extends B {
async method?(val: string): Promise<void>;
}
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,90 @@
{
"type": "File",
"start":0,"end":66,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":66}},
"program": {
"type": "Program",
"start":0,"end":66,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":66}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ClassDeclaration",
"start":0,"end":66,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":3,"column":1,"index":66}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":7,"index":7},"identifierName":"A"},
"name": "A"
},
"superClass": {
"type": "Identifier",
"start":16,"end":17,"loc":{"start":{"line":1,"column":16,"index":16},"end":{"line":1,"column":17,"index":17},"identifierName":"B"},
"name": "B"
},
"body": {
"type": "ClassBody",
"start":18,"end":66,"loc":{"start":{"line":1,"column":18,"index":18},"end":{"line":3,"column":1,"index":66}},
"body": [
{
"type": "TSDeclareMethod",
"start":22,"end":64,"loc":{"start":{"line":2,"column":2,"index":22},"end":{"line":2,"column":44,"index":64}},
"static": false,
"key": {
"type": "Identifier",
"start":28,"end":34,"loc":{"start":{"line":2,"column":8,"index":28},"end":{"line":2,"column":14,"index":34},"identifierName":"method"},
"name": "method"
},
"computed": false,
"kind": "method",
"optional": true,
"id": null,
"generator": false,
"async": true,
"params": [
{
"type": "Identifier",
"start":36,"end":47,"loc":{"start":{"line":2,"column":16,"index":36},"end":{"line":2,"column":27,"index":47},"identifierName":"val"},
"name": "val",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":39,"end":47,"loc":{"start":{"line":2,"column":19,"index":39},"end":{"line":2,"column":27,"index":47}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":41,"end":47,"loc":{"start":{"line":2,"column":21,"index":41},"end":{"line":2,"column":27,"index":47}}
}
}
}
],
"returnType": {
"type": "TSTypeAnnotation",
"start":48,"end":63,"loc":{"start":{"line":2,"column":28,"index":48},"end":{"line":2,"column":43,"index":63}},
"typeAnnotation": {
"type": "TSTypeReference",
"start":50,"end":63,"loc":{"start":{"line":2,"column":30,"index":50},"end":{"line":2,"column":43,"index":63}},
"typeName": {
"type": "Identifier",
"start":50,"end":57,"loc":{"start":{"line":2,"column":30,"index":50},"end":{"line":2,"column":37,"index":57},"identifierName":"Promise"},
"name": "Promise"
},
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":57,"end":63,"loc":{"start":{"line":2,"column":37,"index":57},"end":{"line":2,"column":43,"index":63}},
"params": [
{
"type": "TSVoidKeyword",
"start":58,"end":62,"loc":{"start":{"line":2,"column":38,"index":58},"end":{"line":2,"column":42,"index":62}}
}
]
}
}
}
}
]
}
}
],
"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 @@ -65,7 +65,7 @@
"start":50,"end":57,"loc":{"start":{"line":2,"column":30,"index":50},"end":{"line":2,"column":37,"index":57},"identifierName":"Promise"},
"name": "Promise"
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":57,"end":63,"loc":{"start":{"line":2,"column":37,"index":57},"end":{"line":2,"column":43,"index":63}},
"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 @@ -71,7 +71,7 @@
"start":36,"end":45,"loc":{"start":{"line":1,"column":36,"index":36},"end":{"line":1,"column":45,"index":45},"identifierName":"Generator"},
"name": "Generator"
},
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":45,"end":53,"loc":{"start":{"line":1,"column":45,"index":45},"end":{"line":1,"column":53,"index":53}},
"params": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React, { Context } from 'react';
import { a } from 'react';
import { a as b } from 'react';

let Context: Context<{}> = React.createContext({});
let a: a = 1;
let b: b = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
Loading

0 comments on commit c7f26c8

Please sign in to comment.