Skip to content

Commit 53b1787

Browse files
committed
chore(scripts): remove type assertions in getClientTypesMap
1 parent eecba04 commit 53b1787

File tree

1 file changed

+58
-64
lines changed

1 file changed

+58
-64
lines changed

scripts/generateClientTypesMap/getClientTypesMap.ts

Lines changed: 58 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import jscodeshift, {
2-
type Identifier,
3-
type TSArrayType,
4-
type TSTypeLiteral,
5-
type TSTypeReference,
6-
} from "jscodeshift";
1+
import jscodeshift from "jscodeshift";
72

83
import { CLIENT_NAMES_MAP, DOCUMENT_CLIENT } from "../../src/transforms/v2-to-v3/config";
94
import { getClientTypesMapWithKeysRemovedFromValues } from "./getClientTypesMapWithKeysRemovedFromValues";
@@ -39,20 +34,21 @@ export const getClientTypesMap = async (clientName: string): Promise<Record<stri
3934
});
4035
}
4136

42-
tsTypes
43-
.filter((tsType) => tsType.typeAnnotation.type === "TSTypeReference")
44-
.forEach((tsType) => {
45-
const name = tsType.id.name;
46-
const typeName = ((tsType.typeAnnotation as TSTypeReference).typeName as Identifier).name;
47-
if (typeName === "Date") {
48-
clientTypesMap[name] = typeName;
49-
} else if (typeName === "EventStream") {
50-
// Exception for SelectObjectContentEventStream
51-
clientTypesMap[name] = "AsyncIterable<KEY>";
52-
} else {
53-
console.log("TSTypeReference with unsupported type:", name, typeName);
54-
}
55-
});
37+
tsTypes.forEach((tsType) => {
38+
if (tsType.typeAnnotation.type !== "TSTypeReference") return;
39+
const typeAnnotationName = tsType.typeAnnotation.typeName;
40+
if (typeAnnotationName.type !== "Identifier") return;
41+
const name = tsType.id.name;
42+
const typeName = typeAnnotationName.name;
43+
if (typeName === "Date") {
44+
clientTypesMap[name] = typeName;
45+
} else if (typeName === "EventStream") {
46+
// Exception for SelectObjectContentEventStream
47+
clientTypesMap[name] = "AsyncIterable<KEY>";
48+
} else {
49+
console.log("TSTypeReference with unsupported type:", name, typeName);
50+
}
51+
});
5652

5753
tsTypes
5854
.filter((tsType) => tsType.typeAnnotation.type === "TSUnionType")
@@ -63,62 +59,60 @@ export const getClientTypesMap = async (clientName: string): Promise<Record<stri
6359
}
6460
});
6561

66-
tsTypes
67-
.filter((tsType) => tsType.typeAnnotation.type === "TSArrayType")
68-
.forEach((tsType) => {
69-
const name = tsType.id.name;
70-
const elementType = (tsType.typeAnnotation as TSArrayType).elementType;
71-
if (elementType.type === "TSTypeReference") {
72-
const typeName = elementType.typeName;
73-
if (typeName.type === "Identifier") {
74-
if (clientTypesMap[typeName.name]) {
75-
clientTypesMap[name] = `Array<${clientTypesMap[typeName.name]}>`;
76-
} else {
77-
// Assume it's an interface which would be available in v3.
78-
clientTypesMap[name] = `Array<${typeName.name}>`;
79-
}
62+
tsTypes.forEach((tsType) => {
63+
if (tsType.typeAnnotation.type !== "TSArrayType") return;
64+
const name = tsType.id.name;
65+
const elementType = tsType.typeAnnotation.elementType;
66+
if (elementType.type === "TSTypeReference") {
67+
const typeName = elementType.typeName;
68+
if (typeName.type === "Identifier") {
69+
if (clientTypesMap[typeName.name]) {
70+
clientTypesMap[name] = `Array<${clientTypesMap[typeName.name]}>`;
8071
} else {
81-
console.log("TSArrayType TSTypeReference without Identifier type:", name);
72+
// Assume it's an interface which would be available in v3.
73+
clientTypesMap[name] = `Array<${typeName.name}>`;
8274
}
83-
} else if (Object.keys(ElementTypeToNativeTypeMap).includes(elementType.type)) {
84-
clientTypesMap[name] = `Array<${ElementTypeToNativeTypeMap[elementType.type]}>`;
8575
} else {
86-
console.log("TSArrayType with unsupported elemental type:", name);
76+
console.log("TSArrayType TSTypeReference without Identifier type:", name);
8777
}
88-
});
78+
} else if (Object.keys(ElementTypeToNativeTypeMap).includes(elementType.type)) {
79+
clientTypesMap[name] = `Array<${ElementTypeToNativeTypeMap[elementType.type]}>`;
80+
} else {
81+
console.log("TSArrayType with unsupported elemental type:", name);
82+
}
83+
});
8984

90-
tsTypes
91-
.filter((tsType) => tsType.typeAnnotation.type === "TSTypeLiteral")
92-
.forEach((tsType) => {
93-
const name = tsType.id.name;
94-
const member = (tsType.typeAnnotation as TSTypeLiteral).members[0];
95-
if (member.type === "TSIndexSignature") {
96-
if (member.typeAnnotation) {
97-
if (member.typeAnnotation.typeAnnotation) {
98-
const typeAnnotation = member.typeAnnotation.typeAnnotation;
99-
if (typeAnnotation.type === "TSTypeReference") {
100-
const typeName = typeAnnotation.typeName;
101-
if (typeName.type === "Identifier") {
102-
if (clientTypesMap[typeName.name]) {
103-
clientTypesMap[name] = `Record<string, ${clientTypesMap[typeName.name]}>`;
104-
} else {
105-
// Assume it's an interface which would be available in v3.
106-
clientTypesMap[name] = `Record<string, ${typeName.name}>`;
107-
}
85+
tsTypes.forEach((tsType) => {
86+
if (tsType.typeAnnotation.type !== "TSTypeLiteral") return;
87+
const name = tsType.id.name;
88+
const member = tsType.typeAnnotation.members[0];
89+
if (member.type === "TSIndexSignature") {
90+
if (member.typeAnnotation) {
91+
if (member.typeAnnotation.typeAnnotation) {
92+
const typeAnnotation = member.typeAnnotation.typeAnnotation;
93+
if (typeAnnotation.type === "TSTypeReference") {
94+
const typeName = typeAnnotation.typeName;
95+
if (typeName.type === "Identifier") {
96+
if (clientTypesMap[typeName.name]) {
97+
clientTypesMap[name] = `Record<string, ${clientTypesMap[typeName.name]}>`;
10898
} else {
109-
console.log("TSTypeLiteral TSTypeReference without Identifier type:", name);
99+
// Assume it's an interface which would be available in v3.
100+
clientTypesMap[name] = `Record<string, ${typeName.name}>`;
110101
}
111-
} else if (Object.keys(ElementTypeToNativeTypeMap).includes(typeAnnotation.type)) {
112-
clientTypesMap[name] = `Record<string, ${
113-
ElementTypeToNativeTypeMap[typeAnnotation.type]
114-
}>`;
115102
} else {
116-
console.log("TSTypeLiteral with unsupported typeAnnotation type:", name);
103+
console.log("TSTypeLiteral TSTypeReference without Identifier type:", name);
117104
}
105+
} else if (Object.keys(ElementTypeToNativeTypeMap).includes(typeAnnotation.type)) {
106+
clientTypesMap[name] = `Record<string, ${
107+
ElementTypeToNativeTypeMap[typeAnnotation.type]
108+
}>`;
109+
} else {
110+
console.log("TSTypeLiteral with unsupported typeAnnotation type:", name);
118111
}
119112
}
120113
}
121-
});
114+
}
115+
});
122116

123117
tsTypes.forEach((tsType) => {
124118
const name = tsType.id.name;

0 commit comments

Comments
 (0)