Skip to content

Commit

Permalink
fix: Keep type annotations in syntacticPlaceholders mode (babel#16905)
Browse files Browse the repository at this point in the history
* fix

* review
  • Loading branch information
liuxingbaoyu authored Oct 19, 2024
1 parent f594ee4 commit 831396d
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sourceType": "module",
"plugins": [
"typescript",
"placeholders"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const %%x%%: string = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"type": "File",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"program": {
"type": "Program",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":23,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":23,"index":23}},
"id": {
"type": "Placeholder",
"start":6,"end":19,"loc":{"start":{"line":1,"column":6,"index":6},"end":{"line":1,"column":19,"index":19}},
"name": {
"type": "Identifier",
"start":8,"end":9,"loc":{"start":{"line":1,"column":8,"index":8},"end":{"line":1,"column":9,"index":9},"identifierName":"x"},
"name": "x"
},
"expectedNode": "Pattern",
"typeAnnotation": {
"type": "TSTypeAnnotation",
"start":11,"end":19,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":19,"index":19}},
"typeAnnotation": {
"type": "TSStringKeyword",
"start":13,"end":19,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":19,"index":19}}
}
}
},
"init": {
"type": "NumericLiteral",
"start":22,"end":23,"loc":{"start":{"line":1,"column":22,"index":22},"end":{"line":1,"column":23,"index":23}},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
}
}
],
"kind": "const"
}
],
"directives": [],
"extra": {
"topLevelAwait": false
}
}
}
2 changes: 1 addition & 1 deletion packages/babel-template/src/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function applyReplacement(
function set(parent: any, key: any, value: any) {
const node = parent[key] as t.Node;
parent[key] = value;
if (node.type === "Identifier") {
if (node.type === "Identifier" || node.type === "Placeholder") {
if (node.typeAnnotation) {
value.typeAnnotation = node.typeAnnotation;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/babel-template/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -543,5 +543,25 @@ describe("@babel/template", function () {
]
`);
});

it("should keep node props with syntacticPlaceholders", () => {
const outputs = [
template({ plugins: ["typescript"] })(`const %%x%%: string = 'Hello'`)({
x: t.identifier("x"),
}),
template({ plugins: ["typescript"] })(`
var %%x%%: string = x;
`)({
x: t.objectPattern([]),
}),
];

expect(outputs.map(ast => generator(ast).code)).toMatchInlineSnapshot(`
Array [
"const x: string = 'Hello';",
"var {}: string = x;",
]
`);
});
});
});
7 changes: 7 additions & 0 deletions packages/babel-types/src/ast-types/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1598,6 +1598,9 @@ export interface Placeholder extends BaseNode {
| "ClassBody"
| "Pattern";
name: Identifier;
decorators?: Array<Decorator> | null;
optional?: boolean | null;
typeAnnotation?: TypeAnnotation | TSTypeAnnotation | Noop | null;
}

export interface V8IntrinsicIdentifier extends BaseNode {
Expand Down Expand Up @@ -4041,6 +4044,7 @@ export interface ParentMaps {
| ObjectMethod
| ObjectPattern
| ObjectProperty
| Placeholder
| RestElement
| TSDeclareMethod
| TSParameterProperty;
Expand Down Expand Up @@ -5429,6 +5433,7 @@ export interface ParentMaps {
| Identifier
| ObjectMethod
| ObjectPattern
| Placeholder
| RestElement
| TSDeclareFunction
| TSDeclareMethod;
Expand Down Expand Up @@ -7725,6 +7730,7 @@ export interface ParentMaps {
| Identifier
| ObjectMethod
| ObjectPattern
| Placeholder
| RestElement
| TSCallSignatureDeclaration
| TSConstructSignatureDeclaration
Expand Down Expand Up @@ -8469,6 +8475,7 @@ export interface ParentMaps {
| Identifier
| ObjectMethod
| ObjectPattern
| Placeholder
| RestElement
| TypeCastExpression
| TypeParameter;
Expand Down
2 changes: 2 additions & 0 deletions packages/babel-types/src/definitions/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
assertValueType,
} from "./utils.ts";
import { PLACEHOLDERS } from "./placeholders.ts";
import { patternLikeCommon } from "./core.ts";

const defineType = defineAliasedType("Miscellaneous");

Expand All @@ -25,6 +26,7 @@ defineType("Placeholder", {
expectedNode: {
validate: assertOneOf(...PLACEHOLDERS),
},
...patternLikeCommon(),
},
});

Expand Down

0 comments on commit 831396d

Please sign in to comment.