Skip to content

Commit 725034e

Browse files
authored
Add type annotation for union of required types (#556)
1 parent 0180775 commit 725034e

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

.changeset/dry-shoes-check.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": patch
3+
---
4+
5+
Add type annotation for union of required types

src/transforms/v2-to-v3/__fixtures__/api-redundant-type/global-require.output.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ const stringArray: Array<string> = ["string1", "string2"];
1616
const booleanArray: Array<boolean> = [true, false];
1717
const numberArray: Array<number> = [123, 456];
1818
const blobArray: Array<Uint8Array> = [new Uint8Array()];
19-
const enumArray: Array<AWS_S3.ChecksumAlgorithm> = ["CRC32"];
20-
const structureArray: Array<AWS_S3.Bucket> = [{ Name: "bucketName" }];
19+
const enumArray: Array<typeof AWS_S3.ChecksumAlgorithm> = ["CRC32"];
20+
const structureArray: Array<typeof AWS_S3.Bucket> = [{ Name: "bucketName" }];
2121

2222
// Maps
2323
const stringMap: Record<string, string> = { key: "value" };
2424
const booleanMap: Record<string, boolean> = { key: true };
2525
const numberMap: Record<string, number> = { key: 123 };
26-
const structureMap: Record<string, AWS_APIGateway.MethodSnapshot> = { key: { apiKeyRequired: true } };
26+
const structureMap: Record<string, typeof AWS_APIGateway.MethodSnapshot> = { key: { apiKeyRequired: true } };
2727

2828
// Nested arrays
2929
const arrayNestedTwice: Array<Array<number>> = [[1, 2], [3, 4]];
@@ -35,12 +35,12 @@ const arrayNestedFour: Array<Array<Array<Array<number>>>> = [
3535

3636
// Nested maps
3737
const mapNestedTwice: Record<string, Record<string, string>> = { key: stringMap };
38-
const mapNestedTwiceStruct: Record<string, Record<string, AWS_APIGateway.MethodSnapshot>> = { key: structureMap };
38+
const mapNestedTwiceStruct: Record<string, Record<string, typeof AWS_APIGateway.MethodSnapshot>> = { key: structureMap };
3939

4040
// Nested arrays and maps
4141
const mapOfArrays: Record<string, Array<string>> = { key: ["value"] };
4242
const mapOfMapOfArrays: Record<string, Record<string, Array<string>>> = { key: mapOfArrays };
43-
const mapOfArrayOfMaps: Record<string, Array<Record<string, AWS_DynamoDB.AttributeValue>>> = { key: [{ key: { S:"A" }}] };
43+
const mapOfArrayOfMaps: Record<string, Array<Record<string, typeof AWS_DynamoDB.AttributeValue>>> = { key: [{ key: { S:"A" }}] };
4444
const mapOfArrayOfArrays: Record<string, Array<Array<number>>> = { key: [[1], [2]] };
4545
const arrayOfMaps: Array<Record<string, string>> = [stringMap];
4646
const arrayOfMapOfArrays: Array<Record<string, Array<string>>> = [mapOfArrays];

src/transforms/v2-to-v3/__fixtures__/api-redundant-type/service-require-deep.output.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ const stringArray: Array<string> = ["string1", "string2"];
1818
const booleanArray: Array<boolean> = [true, false];
1919
const numberArray: Array<number> = [123, 456];
2020
const blobArray: Array<Uint8Array> = [new Uint8Array()];
21-
const enumArray: Array<AWS_S3.ChecksumAlgorithm> = ["CRC32"];
22-
const structureArray: Array<AWS_S3.Bucket> = [{ Name: "bucketName" }];
21+
const enumArray: Array<typeof AWS_S3.ChecksumAlgorithm> = ["CRC32"];
22+
const structureArray: Array<typeof AWS_S3.Bucket> = [{ Name: "bucketName" }];
2323

2424
// Maps
2525
const stringMap: Record<string, string> = { key: "value" };
2626
const booleanMap: Record<string, boolean> = { key: true };
2727
const numberMap: Record<string, number> = { key: 123 };
28-
const structureMap: Record<string, AWS_APIGateway.MethodSnapshot> = { key: { apiKeyRequired: true } };
28+
const structureMap: Record<string, typeof AWS_APIGateway.MethodSnapshot> = { key: { apiKeyRequired: true } };
2929

3030
// Nested arrays
3131
const arrayNestedTwice: Array<Array<number>> = [[1, 2], [3, 4]];
@@ -37,12 +37,12 @@ const arrayNestedFour: Array<Array<Array<Array<number>>>> = [
3737

3838
// Nested maps
3939
const mapNestedTwice: Record<string, Record<string, string>> = { key: stringMap };
40-
const mapNestedTwiceStruct: Record<string, Record<string, AWS_APIGateway.MethodSnapshot>> = { key: structureMap };
40+
const mapNestedTwiceStruct: Record<string, Record<string, typeof AWS_APIGateway.MethodSnapshot>> = { key: structureMap };
4141

4242
// Nested arrays and maps
4343
const mapOfArrays: Record<string, Array<string>> = { key: ["value"] };
4444
const mapOfMapOfArrays: Record<string, Record<string, Array<string>>> = { key: mapOfArrays };
45-
const mapOfArrayOfMaps: Record<string, Array<Record<string, AWS_DynamoDB.AttributeValue>>> = { key: [{ key: { S:"A" }}] };
45+
const mapOfArrayOfMaps: Record<string, Array<Record<string, typeof AWS_DynamoDB.AttributeValue>>> = { key: [{ key: { S:"A" }}] };
4646
const mapOfArrayOfArrays: Record<string, Array<Array<number>>> = { key: [[1], [2]] };
4747
const arrayOfMaps: Array<Record<string, string>> = [stringMap];
4848
const arrayOfMapOfArrays: Array<Record<string, Array<string>>> = [mapOfArrays];

src/transforms/v2-to-v3/__fixtures__/api-redundant-type/service-require.output.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ const stringArray: Array<string> = ["string1", "string2"];
1818
const booleanArray: Array<boolean> = [true, false];
1919
const numberArray: Array<number> = [123, 456];
2020
const blobArray: Array<Uint8Array> = [new Uint8Array()];
21-
const enumArray: Array<AWS_S3.ChecksumAlgorithm> = ["CRC32"];
22-
const structureArray: Array<AWS_S3.Bucket> = [{ Name: "bucketName" }];
21+
const enumArray: Array<typeof AWS_S3.ChecksumAlgorithm> = ["CRC32"];
22+
const structureArray: Array<typeof AWS_S3.Bucket> = [{ Name: "bucketName" }];
2323

2424
// Maps
2525
const stringMap: Record<string, string> = { key: "value" };
2626
const booleanMap: Record<string, boolean> = { key: true };
2727
const numberMap: Record<string, number> = { key: 123 };
28-
const structureMap: Record<string, AWS_APIGateway.MethodSnapshot> = { key: { apiKeyRequired: true } };
28+
const structureMap: Record<string, typeof AWS_APIGateway.MethodSnapshot> = { key: { apiKeyRequired: true } };
2929

3030
// Nested arrays
3131
const arrayNestedTwice: Array<Array<number>> = [[1, 2], [3, 4]];
@@ -37,12 +37,12 @@ const arrayNestedFour: Array<Array<Array<Array<number>>>> = [
3737

3838
// Nested maps
3939
const mapNestedTwice: Record<string, Record<string, string>> = { key: stringMap };
40-
const mapNestedTwiceStruct: Record<string, Record<string, AWS_APIGateway.MethodSnapshot>> = { key: structureMap };
40+
const mapNestedTwiceStruct: Record<string, Record<string, typeof AWS_APIGateway.MethodSnapshot>> = { key: structureMap };
4141

4242
// Nested arrays and maps
4343
const mapOfArrays: Record<string, Array<string>> = { key: ["value"] };
4444
const mapOfMapOfArrays: Record<string, Record<string, Array<string>>> = { key: mapOfArrays };
45-
const mapOfArrayOfMaps: Record<string, Array<Record<string, AWS_DynamoDB.AttributeValue>>> = { key: [{ key: { S:"A" }}] };
45+
const mapOfArrayOfMaps: Record<string, Array<Record<string, typeof AWS_DynamoDB.AttributeValue>>> = { key: [{ key: { S:"A" }}] };
4646
const mapOfArrayOfArrays: Record<string, Array<Array<number>>> = { key: [[1], [2]] };
4747
const arrayOfMaps: Array<Record<string, string>> = [stringMap];
4848
const arrayOfMapOfArrays: Array<Record<string, Array<string>>> = [mapOfArrays];
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Identifier, JSCodeshift, TSTypeReference } from "jscodeshift";
2+
import { getDefaultLocalName } from "../utils";
3+
4+
const nativeTsUnionTypes = ["Array", "Record"];
5+
6+
export const addTsTypeQueryToRefType = (j: JSCodeshift, v3ClientType: TSTypeReference) => {
7+
const { typeParameters } = v3ClientType;
8+
if (typeParameters?.type === "TSTypeParameterInstantiation") {
9+
for (const type of typeParameters.params) {
10+
if (type.type === "TSTypeReference") {
11+
const typeRef = type as TSTypeReference;
12+
if (typeRef.typeName.type === "Identifier") {
13+
const typeRefIdentifier = typeRef.typeName as Identifier;
14+
if (typeRefIdentifier.name.startsWith(getDefaultLocalName(""))) {
15+
// @ts-expect-error Type 'TSTypeQuery' is not assignable
16+
typeRef.typeName = j.tsTypeQuery(j.identifier(typeRefIdentifier.name));
17+
} else if (nativeTsUnionTypes.includes(typeRefIdentifier.name)) {
18+
addTsTypeQueryToRefType(j, typeRef);
19+
}
20+
}
21+
}
22+
}
23+
}
24+
};
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { ASTPath, Identifier, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift";
2+
import { addTsTypeQueryToRefType } from "./addTsTypeQueryToRefType";
23
import { getV3ClientType } from "./getV3ClientType";
34

45
const nativeTsRefTypes = ["TSAnyKeyword", "TSStringKeyword", "TSNumberKeyword", "TSBooleanKeyword"];
5-
const nativeTsIdentifierTypes = ["Date", "Uint8Array", "Array", "Record"];
6+
const nativeTsUnionTypes = ["Array", "Record"];
7+
const nativeTsIdentifierTypes = ["Date", "Uint8Array", ...nativeTsUnionTypes];
68

79
interface UpdateV2ClientTypeOptions {
810
v2ClientName: string;
@@ -17,17 +19,26 @@ export const updateV2ClientType = (
1719
) => {
1820
const v3ClientType = getV3ClientType(j, { v2ClientName, v2ClientTypeName, v2ClientLocalName });
1921

20-
if (
21-
(v2ClientType.parentPath?.value.type === "TSTypeQuery" &&
22-
nativeTsRefTypes.includes(v3ClientType.type)) ||
23-
(v3ClientType.type === "TSTypeReference" &&
24-
(v3ClientType as TSTypeReference).typeName.type === "Identifier" &&
25-
nativeTsIdentifierTypes.includes(
26-
((v3ClientType as TSTypeReference).typeName as Identifier).name
27-
))
28-
) {
29-
v2ClientType.parentPath?.replace(v3ClientType);
30-
} else {
31-
v2ClientType.replace(v3ClientType);
22+
if (v2ClientType.parentPath?.value.type === "TSTypeQuery") {
23+
if (nativeTsRefTypes.includes(v3ClientType.type)) {
24+
v2ClientType.parentPath?.replace(v3ClientType);
25+
return;
26+
}
27+
28+
if (v3ClientType.type === "TSTypeReference") {
29+
const v3ClientTypeRef = v3ClientType as TSTypeReference;
30+
if (v3ClientTypeRef.typeName.type === "Identifier") {
31+
const v3ClientTypeRefIdentifier = v3ClientTypeRef.typeName as Identifier;
32+
if (nativeTsIdentifierTypes.includes(v3ClientTypeRefIdentifier.name)) {
33+
if (nativeTsUnionTypes.includes(v3ClientTypeRefIdentifier.name)) {
34+
addTsTypeQueryToRefType(j, v3ClientTypeRef);
35+
}
36+
v2ClientType.parentPath?.replace(v3ClientType);
37+
return;
38+
}
39+
}
40+
}
3241
}
42+
43+
v2ClientType.replace(v3ClientType);
3344
};

0 commit comments

Comments
 (0)