From eecba04b7b6e6e2fe562b04dd0f052fc468d1f78 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Sat, 27 Jul 2024 18:51:26 -0700 Subject: [PATCH 1/3] chore(scripts): remove type assertions from getClientReqRespTypesMap --- .../getClientReqRespTypesMap.ts | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/scripts/generateClientTypesMap/getClientReqRespTypesMap.ts b/scripts/generateClientTypesMap/getClientReqRespTypesMap.ts index b9e140163..2fd644270 100644 --- a/scripts/generateClientTypesMap/getClientReqRespTypesMap.ts +++ b/scripts/generateClientTypesMap/getClientReqRespTypesMap.ts @@ -1,9 +1,4 @@ -import jscodeshift, { - type Identifier, - type TSFunctionType, - type TSQualifiedName, - type TSTypeReference, -} from "jscodeshift"; +import jscodeshift from "jscodeshift"; import { getTypesSource } from "./getTypesSource"; @@ -23,58 +18,58 @@ export const getClientReqRespTypesMap = async ( if (classMethod.key.name === "constructor") return; if (classMethod.key.name.startsWith("waitFor")) return; - const classMethodKeyName = (classMethod.key as Identifier).name; + const classMethodKeyName = classMethod.key.name; const commandName = classMethodKeyName.charAt(0).toUpperCase() + classMethodKeyName.slice(1); if (classMethod.params.length !== 2) return; if (classMethod.params[0].type !== "Identifier") return; if (classMethod.params[0].name !== "params") return; - const params = classMethod.params[0] as Identifier; + const params = classMethod.params[0]; if (!params.typeAnnotation) return; if (!params.typeAnnotation.typeAnnotation) return; if (params.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return; - const paramsTypeRef = params.typeAnnotation.typeAnnotation as TSTypeReference; + const paramsTypeRef = params.typeAnnotation.typeAnnotation; if (!paramsTypeRef.typeName) return; if (paramsTypeRef.typeName.type !== "TSQualifiedName") return; - const paramsTypeRefName = paramsTypeRef.typeName as TSQualifiedName; + const paramsTypeRefName = paramsTypeRef.typeName; if (!paramsTypeRefName.right) return; if (paramsTypeRefName.right.type !== "Identifier") return; - const paramsTypeName = paramsTypeRefName.right as Identifier; + const paramsTypeName = paramsTypeRefName.right; const requestTypeName = paramsTypeName.name; clientTypesMap[requestTypeName] = `${commandName}CommandInput`; if (classMethod.params[1].type !== "Identifier") return; if (classMethod.params[1].name !== "callback") return; - const callback = classMethod.params[1] as Identifier; + const callback = classMethod.params[1]; if (!callback.typeAnnotation) return; if (!callback.typeAnnotation.typeAnnotation) return; if (callback.typeAnnotation.typeAnnotation.type !== "TSFunctionType") return; - const callbackTypeRef = callback.typeAnnotation.typeAnnotation as TSFunctionType; + const callbackTypeRef = callback.typeAnnotation.typeAnnotation; if (!callbackTypeRef.parameters) return; if (callbackTypeRef.parameters.length !== 2) return; if (callbackTypeRef.parameters[1].type !== "Identifier") return; - const responseType = callbackTypeRef.parameters[1] as Identifier; + const responseType = callbackTypeRef.parameters[1]; if (!responseType.typeAnnotation) return; if (responseType.typeAnnotation.type !== "TSTypeAnnotation") return; if (!responseType.typeAnnotation.typeAnnotation) return; if (responseType.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return; - const responseTypeRef = responseType.typeAnnotation.typeAnnotation as TSTypeReference; + const responseTypeRef = responseType.typeAnnotation.typeAnnotation; if (!responseTypeRef.typeName) return; if (responseTypeRef.typeName.type !== "TSQualifiedName") return; - const responseTypeRefName = responseTypeRef.typeName as TSQualifiedName; + const responseTypeRefName = responseTypeRef.typeName; if (!responseTypeRefName.right) return; if (responseTypeRefName.right.type !== "Identifier") return; - const responseTypeName = (responseTypeRefName.right as Identifier).name; + const responseTypeName = responseTypeRefName.right.name; clientTypesMap[responseTypeName] = `${commandName}CommandOutput`; }); From 53b17871eaaf3c02579259283919d135b6575f7c Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Sat, 27 Jul 2024 19:00:17 -0700 Subject: [PATCH 2/3] chore(scripts): remove type assertions in getClientTypesMap --- .../getClientTypesMap.ts | 122 +++++++++--------- 1 file changed, 58 insertions(+), 64 deletions(-) diff --git a/scripts/generateClientTypesMap/getClientTypesMap.ts b/scripts/generateClientTypesMap/getClientTypesMap.ts index f5e46f5f3..2ceafa173 100644 --- a/scripts/generateClientTypesMap/getClientTypesMap.ts +++ b/scripts/generateClientTypesMap/getClientTypesMap.ts @@ -1,9 +1,4 @@ -import jscodeshift, { - type Identifier, - type TSArrayType, - type TSTypeLiteral, - type TSTypeReference, -} from "jscodeshift"; +import jscodeshift from "jscodeshift"; import { CLIENT_NAMES_MAP, DOCUMENT_CLIENT } from "../../src/transforms/v2-to-v3/config"; import { getClientTypesMapWithKeysRemovedFromValues } from "./getClientTypesMapWithKeysRemovedFromValues"; @@ -39,20 +34,21 @@ export const getClientTypesMap = async (clientName: string): Promise tsType.typeAnnotation.type === "TSTypeReference") - .forEach((tsType) => { - const name = tsType.id.name; - const typeName = ((tsType.typeAnnotation as TSTypeReference).typeName as Identifier).name; - if (typeName === "Date") { - clientTypesMap[name] = typeName; - } else if (typeName === "EventStream") { - // Exception for SelectObjectContentEventStream - clientTypesMap[name] = "AsyncIterable"; - } else { - console.log("TSTypeReference with unsupported type:", name, typeName); - } - }); + tsTypes.forEach((tsType) => { + if (tsType.typeAnnotation.type !== "TSTypeReference") return; + const typeAnnotationName = tsType.typeAnnotation.typeName; + if (typeAnnotationName.type !== "Identifier") return; + const name = tsType.id.name; + const typeName = typeAnnotationName.name; + if (typeName === "Date") { + clientTypesMap[name] = typeName; + } else if (typeName === "EventStream") { + // Exception for SelectObjectContentEventStream + clientTypesMap[name] = "AsyncIterable"; + } else { + console.log("TSTypeReference with unsupported type:", name, typeName); + } + }); tsTypes .filter((tsType) => tsType.typeAnnotation.type === "TSUnionType") @@ -63,62 +59,60 @@ export const getClientTypesMap = async (clientName: string): Promise tsType.typeAnnotation.type === "TSArrayType") - .forEach((tsType) => { - const name = tsType.id.name; - const elementType = (tsType.typeAnnotation as TSArrayType).elementType; - if (elementType.type === "TSTypeReference") { - const typeName = elementType.typeName; - if (typeName.type === "Identifier") { - if (clientTypesMap[typeName.name]) { - clientTypesMap[name] = `Array<${clientTypesMap[typeName.name]}>`; - } else { - // Assume it's an interface which would be available in v3. - clientTypesMap[name] = `Array<${typeName.name}>`; - } + tsTypes.forEach((tsType) => { + if (tsType.typeAnnotation.type !== "TSArrayType") return; + const name = tsType.id.name; + const elementType = tsType.typeAnnotation.elementType; + if (elementType.type === "TSTypeReference") { + const typeName = elementType.typeName; + if (typeName.type === "Identifier") { + if (clientTypesMap[typeName.name]) { + clientTypesMap[name] = `Array<${clientTypesMap[typeName.name]}>`; } else { - console.log("TSArrayType TSTypeReference without Identifier type:", name); + // Assume it's an interface which would be available in v3. + clientTypesMap[name] = `Array<${typeName.name}>`; } - } else if (Object.keys(ElementTypeToNativeTypeMap).includes(elementType.type)) { - clientTypesMap[name] = `Array<${ElementTypeToNativeTypeMap[elementType.type]}>`; } else { - console.log("TSArrayType with unsupported elemental type:", name); + console.log("TSArrayType TSTypeReference without Identifier type:", name); } - }); + } else if (Object.keys(ElementTypeToNativeTypeMap).includes(elementType.type)) { + clientTypesMap[name] = `Array<${ElementTypeToNativeTypeMap[elementType.type]}>`; + } else { + console.log("TSArrayType with unsupported elemental type:", name); + } + }); - tsTypes - .filter((tsType) => tsType.typeAnnotation.type === "TSTypeLiteral") - .forEach((tsType) => { - const name = tsType.id.name; - const member = (tsType.typeAnnotation as TSTypeLiteral).members[0]; - if (member.type === "TSIndexSignature") { - if (member.typeAnnotation) { - if (member.typeAnnotation.typeAnnotation) { - const typeAnnotation = member.typeAnnotation.typeAnnotation; - if (typeAnnotation.type === "TSTypeReference") { - const typeName = typeAnnotation.typeName; - if (typeName.type === "Identifier") { - if (clientTypesMap[typeName.name]) { - clientTypesMap[name] = `Record`; - } else { - // Assume it's an interface which would be available in v3. - clientTypesMap[name] = `Record`; - } + tsTypes.forEach((tsType) => { + if (tsType.typeAnnotation.type !== "TSTypeLiteral") return; + const name = tsType.id.name; + const member = tsType.typeAnnotation.members[0]; + if (member.type === "TSIndexSignature") { + if (member.typeAnnotation) { + if (member.typeAnnotation.typeAnnotation) { + const typeAnnotation = member.typeAnnotation.typeAnnotation; + if (typeAnnotation.type === "TSTypeReference") { + const typeName = typeAnnotation.typeName; + if (typeName.type === "Identifier") { + if (clientTypesMap[typeName.name]) { + clientTypesMap[name] = `Record`; } else { - console.log("TSTypeLiteral TSTypeReference without Identifier type:", name); + // Assume it's an interface which would be available in v3. + clientTypesMap[name] = `Record`; } - } else if (Object.keys(ElementTypeToNativeTypeMap).includes(typeAnnotation.type)) { - clientTypesMap[name] = `Record`; } else { - console.log("TSTypeLiteral with unsupported typeAnnotation type:", name); + console.log("TSTypeLiteral TSTypeReference without Identifier type:", name); } + } else if (Object.keys(ElementTypeToNativeTypeMap).includes(typeAnnotation.type)) { + clientTypesMap[name] = `Record`; + } else { + console.log("TSTypeLiteral with unsupported typeAnnotation type:", name); } } } - }); + } + }); tsTypes.forEach((tsType) => { const name = tsType.id.name; From eca4db232832c49d2afb51ecd36b4f0581d406d8 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Sun, 28 Jul 2024 09:45:24 -0700 Subject: [PATCH 3/3] chore: yarn changeset --- .changeset/clean-bikes-end.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/clean-bikes-end.md diff --git a/.changeset/clean-bikes-end.md b/.changeset/clean-bikes-end.md new file mode 100644 index 000000000..589d97850 --- /dev/null +++ b/.changeset/clean-bikes-end.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": minor +--- + +Remove type assertions while generating client types map