-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Retain type annotation when transforming redundant types (#555)
- Loading branch information
Showing
7 changed files
with
158 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aws-sdk-js-codemod": patch | ||
--- | ||
|
||
Retain type annotation when transforming redundant types |
52 changes: 26 additions & 26 deletions
52
src/transforms/v2-to-v3/__fixtures__/api-redundant-type/global-require.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,46 @@ | ||
const AWS = require("aws-sdk"); | ||
|
||
// Native types | ||
const stringType: AWS.S3.AccountId = "string"; | ||
const booleanType: AWS.S3.BucketKeyEnabled = true; | ||
const numberType: AWS.S3.ContentLength = 123; | ||
const stringType: typeof AWS.S3.AccountId = "string"; | ||
const booleanType: typeof AWS.S3.BucketKeyEnabled = true; | ||
const numberType: typeof AWS.S3.ContentLength = 123; | ||
|
||
// Date | ||
const dateType: AWS.S3.CreationDate = new Date(); | ||
const dateType: typeof AWS.S3.CreationDate = new Date(); | ||
|
||
// Uint8Array | ||
const blobType: AWS.RDSDataService._Blob = new Uint8Array(); | ||
const blobType: typeof AWS.RDSDataService._Blob = new Uint8Array(); | ||
|
||
// Arrays | ||
const stringArray: AWS.S3.AllowedHeaders = ["string1", "string2"]; | ||
const booleanArray: AWS.RDSDataService.BooleanArray = [true, false]; | ||
const numberArray: AWS.RDSDataService.LongArray = [123, 456]; | ||
const blobArray: AWS.IoTFleetWise.NetworkFilesList = [new Uint8Array()]; | ||
const enumArray: AWS.S3.ChecksumAlgorithmList = ["CRC32"]; | ||
const structureArray: AWS.S3.Buckets = [{ Name: "bucketName" }]; | ||
const stringArray: typeof AWS.S3.AllowedHeaders = ["string1", "string2"]; | ||
const booleanArray: typeof AWS.RDSDataService.BooleanArray = [true, false]; | ||
const numberArray: typeof AWS.RDSDataService.LongArray = [123, 456]; | ||
const blobArray: typeof AWS.IoTFleetWise.NetworkFilesList = [new Uint8Array()]; | ||
const enumArray: typeof AWS.S3.ChecksumAlgorithmList = ["CRC32"]; | ||
const structureArray: typeof AWS.S3.Buckets = [{ Name: "bucketName" }]; | ||
|
||
// Maps | ||
const stringMap: AWS.S3.Metadata = { key: "value" }; | ||
const booleanMap: AWS.APIGateway.MapOfStringToBoolean = { key: true }; | ||
const numberMap: AWS.SSM.AssociationStatusAggregatedCount = { key: 123 }; | ||
const structureMap: AWS.APIGateway.MapOfMethodSnapshot = { key: { apiKeyRequired: true } }; | ||
const stringMap: typeof AWS.S3.Metadata = { key: "value" }; | ||
const booleanMap: typeof AWS.APIGateway.MapOfStringToBoolean = { key: true }; | ||
const numberMap: typeof AWS.SSM.AssociationStatusAggregatedCount = { key: 123 }; | ||
const structureMap: typeof AWS.APIGateway.MapOfMethodSnapshot = { key: { apiKeyRequired: true } }; | ||
|
||
// Nested arrays | ||
const arrayNestedTwice: AWS.SageMakerGeospatial.LinearRing = [[1, 2], [3, 4]]; | ||
const arrayNestedThrice: AWS.SageMakerGeospatial.LinearRings = [[[1, 2], [3, 4]], [[4, 5], [6, 7]]]; | ||
const arrayNestedFour: AWS.SageMakerGeospatial.LinearRingsList = [ | ||
const arrayNestedTwice: typeof AWS.SageMakerGeospatial.LinearRing = [[1, 2], [3, 4]]; | ||
const arrayNestedThrice: typeof AWS.SageMakerGeospatial.LinearRings = [[[1, 2], [3, 4]], [[4, 5], [6, 7]]]; | ||
const arrayNestedFour: typeof AWS.SageMakerGeospatial.LinearRingsList = [ | ||
[[[1], [2]], [[3], [4]]], | ||
[[[5], [6]], [[7], [8]]] | ||
]; | ||
|
||
// Nested maps | ||
const mapNestedTwice: AWS.LexModelsV2.ConditionMap = { key: stringMap }; | ||
const mapNestedTwiceStruct: AWS.APIGateway.PathToMapOfMethodSnapshot = { key: structureMap }; | ||
const mapNestedTwice: typeof AWS.LexModelsV2.ConditionMap = { key: stringMap }; | ||
const mapNestedTwiceStruct: typeof AWS.APIGateway.PathToMapOfMethodSnapshot = { key: structureMap }; | ||
|
||
// Nested arrays and maps | ||
const mapOfArrays: AWS.NetworkManager.FilterMap = { key: ["value"] }; | ||
const mapOfMapOfArrays: AWS.AppIntegrations.ObjectConfiguration = { key: mapOfArrays }; | ||
const mapOfArrayOfMaps: AWS.DynamoDB.BatchGetResponseMap = { key: [{ key: { S:"A" }}] }; | ||
const mapOfArrayOfArrays: AWS.APIGateway.MapOfKeyUsages = { key: [[1], [2]] }; | ||
const arrayOfMaps: AWS.SSM.InventoryItemEntryList = [stringMap]; | ||
const arrayOfMapOfArrays: AWS.SSM.TargetMaps = [mapOfArrays]; | ||
const mapOfArrays: typeof AWS.NetworkManager.FilterMap = { key: ["value"] }; | ||
const mapOfMapOfArrays: typeof AWS.AppIntegrations.ObjectConfiguration = { key: mapOfArrays }; | ||
const mapOfArrayOfMaps: typeof AWS.DynamoDB.BatchGetResponseMap = { key: [{ key: { S:"A" }}] }; | ||
const mapOfArrayOfArrays: typeof AWS.APIGateway.MapOfKeyUsages = { key: [[1], [2]] }; | ||
const arrayOfMaps: typeof AWS.SSM.InventoryItemEntryList = [stringMap]; | ||
const arrayOfMapOfArrays: typeof AWS.SSM.TargetMaps = [mapOfArrays]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/transforms/v2-to-v3/ts-type/getTSQualifiedNameFromClientName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { TSQualifiedName } from "jscodeshift"; | ||
|
||
export const getTSQualifiedNameFromClientName = ( | ||
v2GlobalName: string, | ||
clientName: string | ||
): TSQualifiedName => { | ||
// Support for DynamoDB.DocumentClient | ||
const [clientNamePrefix, clientNameSuffix] = clientName.split("."); | ||
|
||
if (clientNameSuffix) { | ||
return { | ||
left: { | ||
left: { type: "Identifier", name: v2GlobalName }, | ||
right: { type: "Identifier", name: clientNamePrefix }, | ||
}, | ||
right: { type: "Identifier", name: clientNameSuffix }, | ||
} as TSQualifiedName; | ||
} | ||
|
||
return { | ||
left: { type: "Identifier", name: v2GlobalName }, | ||
right: { type: "Identifier", name: clientNamePrefix }, | ||
} as TSQualifiedName; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ASTPath, Identifier, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift"; | ||
import { getV3ClientTypeReference } from "./getV3ClientTypeReference"; | ||
|
||
const nativeTsRefTypes = ["TSAnyKeyword", "TSStringKeyword", "TSNumberKeyword", "TSBooleanKeyword"]; | ||
const nativeTsIdentifierTypes = ["Date", "Uint8Array", "Array", "Record"]; | ||
|
||
interface UpdateV2ClientTypeRefOptions { | ||
v2ClientName: string; | ||
v2ClientTypeName: string; | ||
v2ClientLocalName: string; | ||
} | ||
|
||
export const updateV2ClientTypeRef = ( | ||
j: JSCodeshift, | ||
v2ClientType: ASTPath<TSQualifiedName>, | ||
{ v2ClientName, v2ClientTypeName, v2ClientLocalName }: UpdateV2ClientTypeRefOptions | ||
) => { | ||
const v3ClientTypeRef = getV3ClientTypeReference(j, { | ||
v2ClientName, | ||
v2ClientTypeName, | ||
v2ClientLocalName, | ||
}); | ||
|
||
if ( | ||
(v2ClientType.parentPath?.value.type === "TSTypeQuery" && | ||
nativeTsRefTypes.includes(v3ClientTypeRef.type)) || | ||
(v3ClientTypeRef.type === "TSTypeReference" && | ||
(v3ClientTypeRef as TSTypeReference).typeName.type === "Identifier" && | ||
nativeTsIdentifierTypes.includes( | ||
((v3ClientTypeRef as TSTypeReference).typeName as Identifier).name | ||
)) | ||
) { | ||
v2ClientType.parentPath?.replace(v3ClientTypeRef); | ||
} else { | ||
v2ClientType.replace(v3ClientTypeRef); | ||
} | ||
}; |