Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f8f3a8

Browse files
committed
feature: TSTypeOperator: keyof (#16799)
1 parent 55f9fb5 commit 0f8f3a8

5 files changed

Lines changed: 40 additions & 5 deletions

File tree

packages/babel-types/scripts/generators/validators.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ type Opts<Obj> = Partial<{
9292
`;
9393

9494
Object.keys(VISITOR_KEYS).forEach(type => {
95+
if (type === "TSClassImplements") console.log(">>>");
9596
output += addIsHelper(type);
9697
});
9798

packages/babel-types/src/ast-types/generated/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,7 @@ export interface TSParenthesizedType extends BaseNode {
19251925
export interface TSTypeOperator extends BaseNode {
19261926
type: "TSTypeOperator";
19271927
typeAnnotation: TSType;
1928-
operator: string;
1928+
operator: "keyof" | "readonly" | "unique";
19291929
}
19301930

19311931
export interface TSIndexedAccessType extends BaseNode {

packages/babel-types/src/builders/generated/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3021,14 +3021,18 @@ export function tsParenthesizedType(
30213021
return node;
30223022
}
30233023
export { tsParenthesizedType as tSParenthesizedType };
3024-
export function tsTypeOperator(typeAnnotation: t.TSType): t.TSTypeOperator {
3024+
export function tsTypeOperator(
3025+
typeAnnotation: t.TSType,
3026+
operator: "keyof" | "readonly" | "unique",
3027+
): t.TSTypeOperator {
30253028
const node: t.TSTypeOperator = {
30263029
type: "TSTypeOperator",
30273030
typeAnnotation,
3028-
operator: null,
3031+
operator,
30293032
};
30303033
const defs = NODE_FIELDS.TSTypeOperator;
30313034
validate(defs.typeAnnotation, node, "typeAnnotation", typeAnnotation, 1);
3035+
validate(defs.operator, node, "operator", operator);
30323036
return node;
30333037
}
30343038
export { tsTypeOperator as tSTypeOperator };

packages/babel-types/src/definitions/typescript.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,11 @@ defineType("TSParenthesizedType", {
337337

338338
defineType("TSTypeOperator", {
339339
aliases: ["TSType"],
340-
visitor: ["typeAnnotation"],
340+
visitor: ["typeAnnotation", "operator"],
341341
fields: {
342-
operator: validate(assertValueType("string")),
342+
operator: {
343+
validate: assertOneOf("keyof", "readonly", "unique"),
344+
},
343345
typeAnnotation: validateType("TSType"),
344346
},
345347
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as t from "../../../lib/index.js";
2+
3+
describe("builders", function () {
4+
describe("typescript", function () {
5+
describe("tsTypeOperator", function () {
6+
it("accept operator as argument for tsTypeOperator", function () {
7+
const tsTypeOperator = t.TSTypeOperator(
8+
t.TSTypeQuery(t.Identifier("x")),
9+
"keyof",
10+
);
11+
expect(tsTypeOperator).toMatchInlineSnapshot(`
12+
Object {
13+
"operator": "keyof",
14+
"type": "TSTypeOperator",
15+
"typeAnnotation": Object {
16+
"exprName": Object {
17+
"name": "x",
18+
"type": "Identifier",
19+
},
20+
"type": "TSTypeQuery",
21+
"typeParameters": null,
22+
},
23+
}
24+
`);
25+
});
26+
});
27+
});
28+
});

0 commit comments

Comments
 (0)