Skip to content

Commit 3411083

Browse files
authored
Remove type assertions while generating client types map (#913)
1 parent 3cbcf56 commit 3411083

File tree

3 files changed

+75
-81
lines changed

3 files changed

+75
-81
lines changed

.changeset/clean-bikes-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": minor
3+
---
4+
5+
Remove type assertions while generating client types map

scripts/generateClientTypesMap/getClientReqRespTypesMap.ts

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

83
import { getTypesSource } from "./getTypesSource";
94

@@ -23,58 +18,58 @@ export const getClientReqRespTypesMap = async (
2318
if (classMethod.key.name === "constructor") return;
2419
if (classMethod.key.name.startsWith("waitFor")) return;
2520

26-
const classMethodKeyName = (classMethod.key as Identifier).name;
21+
const classMethodKeyName = classMethod.key.name;
2722
const commandName = classMethodKeyName.charAt(0).toUpperCase() + classMethodKeyName.slice(1);
2823

2924
if (classMethod.params.length !== 2) return;
3025
if (classMethod.params[0].type !== "Identifier") return;
3126
if (classMethod.params[0].name !== "params") return;
3227

33-
const params = classMethod.params[0] as Identifier;
28+
const params = classMethod.params[0];
3429

3530
if (!params.typeAnnotation) return;
3631
if (!params.typeAnnotation.typeAnnotation) return;
3732
if (params.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return;
38-
const paramsTypeRef = params.typeAnnotation.typeAnnotation as TSTypeReference;
33+
const paramsTypeRef = params.typeAnnotation.typeAnnotation;
3934

4035
if (!paramsTypeRef.typeName) return;
4136
if (paramsTypeRef.typeName.type !== "TSQualifiedName") return;
42-
const paramsTypeRefName = paramsTypeRef.typeName as TSQualifiedName;
37+
const paramsTypeRefName = paramsTypeRef.typeName;
4338

4439
if (!paramsTypeRefName.right) return;
4540
if (paramsTypeRefName.right.type !== "Identifier") return;
46-
const paramsTypeName = paramsTypeRefName.right as Identifier;
41+
const paramsTypeName = paramsTypeRefName.right;
4742
const requestTypeName = paramsTypeName.name;
4843

4944
clientTypesMap[requestTypeName] = `${commandName}CommandInput`;
5045

5146
if (classMethod.params[1].type !== "Identifier") return;
5247
if (classMethod.params[1].name !== "callback") return;
53-
const callback = classMethod.params[1] as Identifier;
48+
const callback = classMethod.params[1];
5449

5550
if (!callback.typeAnnotation) return;
5651
if (!callback.typeAnnotation.typeAnnotation) return;
5752
if (callback.typeAnnotation.typeAnnotation.type !== "TSFunctionType") return;
58-
const callbackTypeRef = callback.typeAnnotation.typeAnnotation as TSFunctionType;
53+
const callbackTypeRef = callback.typeAnnotation.typeAnnotation;
5954

6055
if (!callbackTypeRef.parameters) return;
6156
if (callbackTypeRef.parameters.length !== 2) return;
6257
if (callbackTypeRef.parameters[1].type !== "Identifier") return;
63-
const responseType = callbackTypeRef.parameters[1] as Identifier;
58+
const responseType = callbackTypeRef.parameters[1];
6459

6560
if (!responseType.typeAnnotation) return;
6661
if (responseType.typeAnnotation.type !== "TSTypeAnnotation") return;
6762
if (!responseType.typeAnnotation.typeAnnotation) return;
6863
if (responseType.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return;
69-
const responseTypeRef = responseType.typeAnnotation.typeAnnotation as TSTypeReference;
64+
const responseTypeRef = responseType.typeAnnotation.typeAnnotation;
7065

7166
if (!responseTypeRef.typeName) return;
7267
if (responseTypeRef.typeName.type !== "TSQualifiedName") return;
73-
const responseTypeRefName = responseTypeRef.typeName as TSQualifiedName;
68+
const responseTypeRefName = responseTypeRef.typeName;
7469

7570
if (!responseTypeRefName.right) return;
7671
if (responseTypeRefName.right.type !== "Identifier") return;
77-
const responseTypeName = (responseTypeRefName.right as Identifier).name;
72+
const responseTypeName = responseTypeRefName.right.name;
7873

7974
clientTypesMap[responseTypeName] = `${commandName}CommandOutput`;
8075
});

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)