Skip to content

Commit

Permalink
[babel 8] Use an identifier for TSTypeParameter.name (#12829)
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Jul 2, 2021
1 parent 6df0b7c commit 9bad558
Show file tree
Hide file tree
Showing 177 changed files with 4,546 additions and 106 deletions.
12 changes: 10 additions & 2 deletions packages/babel-generator/src/generators/typescript.ts
Expand Up @@ -21,7 +21,11 @@ export function TSTypeParameterInstantiation(
export { TSTypeParameterInstantiation as TSTypeParameterDeclaration };

export function TSTypeParameter(this: Printer, node: t.TSTypeParameter) {
this.word(node.name);
this.word(
!process.env.BABEL_8_BREAKING
? (node.name as unknown as string)
: (node.name as unknown as t.Identifier).name,
);

if (node.constraint) {
this.space();
Expand Down Expand Up @@ -387,7 +391,11 @@ export function TSMappedType(this: Printer, node: t.TSMappedType) {
}

this.token("[");
this.word(typeParameter.name);
this.word(
!process.env.BABEL_8_BREAKING
? (typeParameter.name as unknown as string)
: (typeParameter.name as unknown as t.Identifier).name,
);
this.space();
this.word("in");
this.space();
Expand Down
8 changes: 7 additions & 1 deletion packages/babel-generator/test/index.js
Expand Up @@ -342,7 +342,13 @@ describe("generation", function () {

it("wraps around infer inside an array type", () => {
const type = t.tsArrayType(
t.tsInferType(t.tsTypeParameter(null, null, "T")),
t.tsInferType(
t.tsTypeParameter(
null,
null,
!process.env.BABEL_8_BREAKING ? "T" : t.identifier("T"),
),
),
);

const output = generate(type).code;
Expand Down
11 changes: 8 additions & 3 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -485,7 +485,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
tsParseTypeParameter(): N.TsTypeParameter {
const node: N.TsTypeParameter = this.startNode();
node.name = this.parseIdentifierName(node.start);
node.name = this.tsParseTypeParameterName();
node.constraint = this.tsEatThenParseType(tt._extends);
node.default = this.tsEatThenParseType(tt.eq);
return this.finishNode(node, "TSTypeParameter");
Expand Down Expand Up @@ -777,7 +777,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>

tsParseMappedTypeParameter(): N.TsTypeParameter {
const node: N.TsTypeParameter = this.startNode();
node.name = this.parseIdentifierName(node.start);
node.name = this.tsParseTypeParameterName();
node.constraint = this.tsExpectThenParseType(tt._in);
return this.finishNode(node, "TSTypeParameter");
}
Expand Down Expand Up @@ -1091,7 +1091,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
const node = this.startNode();
this.expectContextual("infer");
const typeParameter = this.startNode();
typeParameter.name = this.parseIdentifierName(typeParameter.start);
typeParameter.name = this.tsParseTypeParameterName();
node.typeParameter = this.finishNode(typeParameter, "TSTypeParameter");
return this.finishNode(node, "TSInferType");
}
Expand Down Expand Up @@ -3191,6 +3191,11 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return method;
}

tsParseTypeParameterName(): N.Identifier | string {
const typeName: N.Identifier = this.parseIdentifier();
return process.env.BABEL_8_BREAKING ? typeName : typeName.name;
}

shouldParseAsAmbientContext(): boolean {
return !!this.getPluginOption("typescript", "dts");
}
Expand Down
13 changes: 6 additions & 7 deletions packages/babel-parser/src/types.js
Expand Up @@ -960,7 +960,7 @@ export type TsTypeAnnotation = NodeBase & {
};

export type TypeParameterDeclarationBase = NodeBase & {
params: $ReadOnlyArray<TypeParameterBase>,
params: $ReadOnlyArray<TypeParameter | TsTypeParameter>,
};

export type TypeParameterDeclaration = TypeParameterDeclarationBase & {
Expand All @@ -973,17 +973,16 @@ export type TsTypeParameterDeclaration = TypeParameterDeclarationBase & {
params: $ReadOnlyArray<TsTypeParameter>,
};

export type TypeParameterBase = NodeBase & {
name: string,
};

export type TypeParameter = TypeParameterBase & {
export type TypeParameter = NodeBase & {
type: "TypeParameter",
name: string,
default?: TypeAnnotation,
};

export type TsTypeParameter = TypeParameterBase & {
export type TsTypeParameter = NodeBase & {
type: "TSTypeParameter",
// TODO(Babel-8): remove string type support
name: string | Identifier,
constraint?: TsType,
default?: TsType,
};
Expand Down
@@ -0,0 +1 @@
<div>() => {}
@@ -0,0 +1,4 @@
{
"plugins": ["typescript"],
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,42 @@
{
"type": "File",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"program": {
"type": "Program",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"expression": {
"type": "ArrowFunctionExpression",
"start":0,"end":13,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":13}},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":11,"end":13,"loc":{"start":{"line":1,"column":11},"end":{"line":1,"column":13}},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TSTypeParameterDeclaration",
"start":0,"end":5,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":5}},
"params": [
{
"type": "TSTypeParameter",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
}
]
}
}
}
],
"directives": []
}
}
@@ -1,3 +1,4 @@
{
"plugins": ["typescript"]
"plugins": ["typescript"],
"BABEL_8_BREAKING": true
}
Expand Up @@ -30,7 +30,11 @@
{
"type": "TSTypeParameter",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"name": "div"
"name": {
"type": "Identifier",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4},"identifierName":"div"},
"name": "div"
}
}
]
}
Expand Down
@@ -0,0 +1 @@
async <T>() => await null;
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,44 @@
{
"type": "File",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"program": {
"type": "Program",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":26,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"expression": {
"type": "ArrowFunctionExpression",
"start":0,"end":25,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":25}},
"typeParameters": {
"type": "TSTypeParameterDeclaration",
"start":6,"end":9,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":9}},
"params": [
{
"type": "TSTypeParameter",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8}},
"name": "T"
}
]
},
"params": [],
"id": null,
"generator": false,
"async": true,
"body": {
"type": "AwaitExpression",
"start":15,"end":25,"loc":{"start":{"line":1,"column":15},"end":{"line":1,"column":25}},
"argument": {
"type": "NullLiteral",
"start":21,"end":25,"loc":{"start":{"line":1,"column":21},"end":{"line":1,"column":25}}
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
Expand Up @@ -20,7 +20,11 @@
{
"type": "TSTypeParameter",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8}},
"name": "T"
"name": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7},"end":{"line":1,"column":8},"identifierName":"T"},
"name": "T"
}
}
]
},
Expand Down
@@ -0,0 +1,4 @@
async () => {
await null;
async <T>() => null;
};
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": false
}
@@ -0,0 +1,71 @@
{
"type": "File",
"start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"program": {
"type": "Program",
"start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":53,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":2}},
"expression": {
"type": "ArrowFunctionExpression",
"start":0,"end":52,"loc":{"start":{"line":1,"column":0},"end":{"line":4,"column":1}},
"id": null,
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start":12,"end":52,"loc":{"start":{"line":1,"column":12},"end":{"line":4,"column":1}},
"body": [
{
"type": "ExpressionStatement",
"start":16,"end":27,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":13}},
"expression": {
"type": "AwaitExpression",
"start":16,"end":26,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":12}},
"argument": {
"type": "NullLiteral",
"start":22,"end":26,"loc":{"start":{"line":2,"column":8},"end":{"line":2,"column":12}}
}
}
},
{
"type": "ExpressionStatement",
"start":30,"end":50,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":22}},
"expression": {
"type": "ArrowFunctionExpression",
"start":30,"end":49,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":21}},
"typeParameters": {
"type": "TSTypeParameterDeclaration",
"start":36,"end":39,"loc":{"start":{"line":3,"column":8},"end":{"line":3,"column":11}},
"params": [
{
"type": "TSTypeParameter",
"start":37,"end":38,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":10}},
"name": "T"
}
]
},
"params": [],
"id": null,
"generator": false,
"async": true,
"body": {
"type": "NullLiteral",
"start":45,"end":49,"loc":{"start":{"line":3,"column":17},"end":{"line":3,"column":21}}
}
}
}
],
"directives": []
}
}
}
],
"directives": []
}
}
@@ -0,0 +1,3 @@
{
"BABEL_8_BREAKING": true
}
Expand Up @@ -46,7 +46,11 @@
{
"type": "TSTypeParameter",
"start":37,"end":38,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":10}},
"name": "T"
"name": {
"type": "Identifier",
"start":37,"end":38,"loc":{"start":{"line":3,"column":9},"end":{"line":3,"column":10},"identifierName":"T"},
"name": "T"
}
}
]
},
Expand Down
@@ -0,0 +1 @@
async <T>(a: T): T => a;
@@ -0,0 +1,6 @@
{
"sourceType": "module",
"plugins": ["typescript"],
"tokens": true,
"BABEL_8_BREAKING": false
}

0 comments on commit 9bad558

Please sign in to comment.