Skip to content

Commit

Permalink
Rename typeParameters to typeArguments for call expression alike (#…
Browse files Browse the repository at this point in the history
…17020)

* breaking: rename typeParameters to typeArguments

For CallExpression, JSXOpeningElement, NewExpression, OptionalCallExpression and TSInstantiationExpression

* update test fixtures

* update Babel 7 fixtures

* supress Babel 8 typing errors

* Update packages/babel-generator/src/generators/expressions.ts

Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>

---------

Co-authored-by: Nicolò Ribaudo <hello@nicr.dev>
  • Loading branch information
JLHwung and nicolo-ribaudo authored Dec 16, 2024
1 parent 87a5e2e commit 3d36199
Show file tree
Hide file tree
Showing 169 changed files with 3,687 additions and 118 deletions.
21 changes: 15 additions & 6 deletions packages/babel-generator/src/generators/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,11 @@ export function NewExpression(
return;
}

this.print(node.typeArguments); // Flow
this.print(node.typeParameters); // TS
this.print(node.typeArguments);
if (!process.env.BABEL_8_BREAKING) {
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
this.print(node.typeParameters); // Legacy TS AST
}

// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
if (node.optional) {
Expand Down Expand Up @@ -186,13 +189,16 @@ export function OptionalCallExpression(
) {
this.print(node.callee);

this.print(node.typeParameters); // TS
if (!process.env.BABEL_8_BREAKING) {
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
this.print(node.typeParameters); // legacy TS AST
}

if (node.optional) {
this.token("?.");
}

this.print(node.typeArguments); // Flow
this.print(node.typeArguments);

this.token("(");
const exit = this.enterDelimited();
Expand All @@ -204,8 +210,11 @@ export function OptionalCallExpression(
export function CallExpression(this: Printer, node: t.CallExpression) {
this.print(node.callee);

this.print(node.typeArguments); // Flow
this.print(node.typeParameters); // TS
this.print(node.typeArguments);
if (!process.env.BABEL_8_BREAKING) {
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
this.print(node.typeParameters); // legacy TS AST
}
this.token("(");
const exit = this.enterDelimited();
this.printList(node.arguments, this.shouldPrintTrailingComma(")"));
Expand Down
9 changes: 8 additions & 1 deletion packages/babel-generator/src/generators/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@ function spaceSeparator(this: Printer) {
export function JSXOpeningElement(this: Printer, node: t.JSXOpeningElement) {
this.token("<");
this.print(node.name);
this.print(node.typeParameters); // TS
if (process.env.BABEL_8_BREAKING) {
//@ts-ignore(Babel 7 vs Babel 8) Babel 8 AST
this.print(node.typeArguments);
} else {
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
this.print(node.typeParameters); // Legacy TS AST
}

if (node.attributes.length > 0) {
this.space();
this.printJoin(node.attributes, undefined, undefined, spaceSeparator);
Expand Down
8 changes: 7 additions & 1 deletion packages/babel-generator/src/generators/template-literals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ export function TaggedTemplateExpression(
node: t.TaggedTemplateExpression,
) {
this.print(node.tag);
this.print(node.typeParameters); // TS
if (process.env.BABEL_8_BREAKING) {
// @ts-ignore(Babel 7 vs Babel 8) Babel 8 AST
this.print(node.typeArguments);
} else {
// @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
this.print(node.typeParameters);
}
this.print(node.quasi);
}

Expand Down
8 changes: 7 additions & 1 deletion packages/babel-generator/src/generators/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,13 @@ export function TSInstantiationExpression(
node: t.TSInstantiationExpression,
) {
this.print(node.expression);
this.print(node.typeParameters);
if (process.env.BABEL_8_BREAKING) {
// @ts-ignore(Babel 7 vs Babel 8) Babel 8 AST
this.print(node.typeArguments);
} else {
// @ts-ignore(Babel 7 vs Babel 8) Removed in Babel 8
this.print(node.typeParameters);
}
}

export function TSEnumDeclaration(this: Printer, node: t.TSEnumDeclaration) {
Expand Down
6 changes: 5 additions & 1 deletion packages/babel-generator/src/node/parentheses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ export function TSInstantiationExpression(
parentType === "OptionalCallExpression" ||
parentType === "NewExpression" ||
parentType === "TSInstantiationExpression") &&
!!parent.typeParameters
!!(process.env.BABEL_8_BREAKING
? // @ts-ignore(Babel 7 vs Babel 8) Babel 8 AST
parent.typeArguments
: // @ts-ignore(Babel 7 vs Babel 8) Babel 7 AST
parent.typeParameters)
);
}

Expand Down
40 changes: 34 additions & 6 deletions packages/babel-parser/src/plugins/typescript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2556,7 +2556,11 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
startLoc,
state,
);
result.typeParameters = typeArguments;
if (process.env.BABEL_8_BREAKING) {
result.typeArguments = typeArguments;
} else {
result.typeParameters = typeArguments;
}
return result;
}

Expand All @@ -2571,7 +2575,12 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
// Handles invalid case: `f<T>(a:b)`
this.tsCheckForInvalidTypeCasts(node.arguments);

node.typeParameters = typeArguments;
if (process.env.BABEL_8_BREAKING) {
node.typeArguments = typeArguments;
} else {
node.typeParameters = typeArguments;
}

if (state.optionalChainMember) {
(node as Undone<N.OptionalCallExpression>).optional =
isOptionalCall;
Expand All @@ -2597,7 +2606,11 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>

const node = this.startNodeAt<N.TsInstantiationExpression>(startLoc);
node.expression = base;
node.typeParameters = typeArguments;
if (process.env.BABEL_8_BREAKING) {
node.typeArguments = typeArguments;
} else {
node.typeParameters = typeArguments;
}
return this.finishNode(node, "TSInstantiationExpression");
});

Expand Down Expand Up @@ -2632,7 +2645,11 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
callee.type === "TSInstantiationExpression" &&
!callee.extra?.parenthesized
) {
node.typeParameters = callee.typeParameters;
if (process.env.BABEL_8_BREAKING) {
node.typeArguments = callee.typeArguments;
} else {
node.typeParameters = callee.typeParameters;
}
node.callee = callee.expression;
}
}
Expand Down Expand Up @@ -3776,7 +3793,12 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
expr,
startLoc,
) as N.CallExpression;
call.typeParameters = typeArguments;
if (process.env.BABEL_8_BREAKING) {
call.typeArguments = typeArguments;
} else {
call.typeParameters = typeArguments;
}

return call;
}

Expand Down Expand Up @@ -3915,7 +3937,13 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
const typeArguments = this.tsTryParseAndCatch(() =>
this.tsParseTypeArgumentsInExpression(),
);
if (typeArguments) node.typeParameters = typeArguments;
if (typeArguments) {
if (process.env.BABEL_8_BREAKING) {
node.typeArguments = typeArguments;
} else {
node.typeParameters = typeArguments;
}
}
}
return super.jsxParseOpeningElementAfterName(node);
}
Expand Down
17 changes: 16 additions & 1 deletion packages/babel-parser/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,9 @@ export interface CallOrNewBase extends NodeBase {
callee: Expression | Super | Import;
arguments: Array<Expression | SpreadElement>; // TODO: $ReadOnlyArray,
typeArguments: TypeParameterInstantiationBase | undefined | null;
/**
* @deprecated
*/
typeParameters?: TypeParameterInstantiationBase | null; // TODO: Not in spec
}

Expand Down Expand Up @@ -799,6 +802,10 @@ export interface TaggedTemplateExpression extends NodeBase {
type: "TaggedTemplateExpression";
tag: Expression;
quasi: TemplateLiteral;
typeArguments?: TypeParameterInstantiationBase | null; // TODO: Not in spec
/**
* @deprecated
*/
typeParameters?: TypeParameterInstantiationBase | null; // TODO: Not in spec
}

Expand Down Expand Up @@ -1127,6 +1134,10 @@ export type JSXSpreadAttribute = NodeAny<"JSXSpreadAttribute">;
export interface JSXOpeningElement extends NodeBase {
type: "JSXOpeningElement";
name: JSXNamespacedName | JSXMemberExpression;
typeArguments?: TypeParameterInstantiationBase | null; // TODO: Not in spec,
/**
* @deprecated
*/
typeParameters?: TypeParameterInstantiationBase | null; // TODO: Not in spec,
attributes: (JSXAttribute | JSXSpreadAttribute)[];
selfClosing: boolean;
Expand Down Expand Up @@ -1886,7 +1897,11 @@ export interface TsNonNullExpression extends NodeBase {
export interface TsInstantiationExpression extends NodeBase {
type: "TSInstantiationExpression";
expression: Expression;
typeParameters: TsTypeParameterInstantiation;
typeArguments?: TsTypeParameterInstantiation;
/**
* @deprecated
*/
typeParameters?: TsTypeParameterInstantiation;
}

// ================
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
foo?.foo<T>();
foo?.foo?.<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,102 @@
{
"type": "File",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"program": {
"type": "Program",
"start":0,"end":31,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":16}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":14,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":14}},
"expression": {
"type": "ChainExpression",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"expression": {
"type": "CallExpression",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"callee": {
"type": "MemberExpression",
"start":0,"end":8,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":8}},
"object": {
"type": "Identifier",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":5,"end":8,"loc":{"start":{"line":1,"column":5},"end":{"line":1,"column":8},"identifierName":"foo"},
"name": "foo"
},
"optional": true
},
"arguments": [],
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11}},
"params": [
{
"type": "TSTypeReference",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10}},
"typeName": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9},"end":{"line":1,"column":10},"identifierName":"T"},
"name": "T"
}
}
]
},
"optional": false
}
}
},
{
"type": "ExpressionStatement",
"start":15,"end":31,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":16}},
"expression": {
"type": "ChainExpression",
"start":15,"end":30,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":15}},
"expression": {
"type": "CallExpression",
"start":15,"end":30,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":15}},
"callee": {
"type": "MemberExpression",
"start":15,"end":23,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":8}},
"object": {
"type": "Identifier",
"start":15,"end":18,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":3},"identifierName":"foo"},
"name": "foo"
},
"computed": false,
"property": {
"type": "Identifier",
"start":20,"end":23,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":8},"identifierName":"foo"},
"name": "foo"
},
"optional": true
},
"arguments": [],
"typeParameters": {
"type": "TSTypeParameterInstantiation",
"start":25,"end":28,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13}},
"params": [
{
"type": "TSTypeReference",
"start":26,"end":27,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12}},
"typeName": {
"type": "Identifier",
"start":26,"end":27,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":12},"identifierName":"T"},
"name": "T"
}
}
]
},
"optional": true
}
}
}
]
}
}
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 @@ -33,7 +33,7 @@
"optional": true
},
"arguments": [],
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":8,"end":11,"loc":{"start":{"line":1,"column":8},"end":{"line":1,"column":11}},
"params": [
Expand Down Expand Up @@ -78,7 +78,7 @@
"optional": true
},
"arguments": [],
"typeParameters": {
"typeArguments": {
"type": "TSTypeParameterInstantiation",
"start":25,"end":28,"loc":{"start":{"line":2,"column":10},"end":{"line":2,"column":13}},
"params": [
Expand All @@ -99,4 +99,4 @@
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
async < 1;
async<T>() == 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
Loading

0 comments on commit 3d36199

Please sign in to comment.