Skip to content

Commit aa735f7

Browse files
authored
Transform types imported from Types (#726)
1 parent 5c4c903 commit aa735f7

14 files changed

+66
-1
lines changed

.changeset/two-cups-fry.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+
Transform types imported from redundant Types
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import AWS = require("aws-sdk");
2+
3+
const testTags: AWS.S3.Types.Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import AWS_client_s3 = require("@aws-sdk/client-s3");
2+
import Tag = AWS_client_s3.Tag;
3+
4+
const testTags: Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import AWS from "aws-sdk";
2+
3+
const testTags: AWS.S3.Types.Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Tag } from "@aws-sdk/client-s3";
2+
3+
const testTags: Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import S3 from "aws-sdk/clients/s3";
2+
3+
const testTags: S3.Types.Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Tag } from "@aws-sdk/client-s3";
2+
3+
const testTags: Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import S3 = require("aws-sdk/clients/s3");
2+
3+
const testTags: S3.Types.Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import AWS_client_s3 = require("@aws-sdk/client-s3");
2+
import Tag = AWS_client_s3.Tag;
3+
4+
const testTags: Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { S3 } from "aws-sdk";
2+
3+
const testTags: S3.Types.Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Tag } from "@aws-sdk/client-s3";
2+
3+
const testTags: Tag[] = [{ Key: "key", Value: "value" }];

src/transforms/v2-to-v3/transformer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
removeClientModule,
3131
removeGlobalModule,
3232
} from "./modules";
33-
import { replaceTSTypeReference } from "./ts-type";
33+
import { removeTypesFromTSQualifiedName, replaceTSTypeReference } from "./ts-type";
3434
import {
3535
IndentationType,
3636
getFormattedSourceString,
@@ -91,6 +91,8 @@ const transformer = async (file: FileInfo, api: API) => {
9191
const v2Options = { v2ClientName, v2ClientLocalName, v2GlobalName };
9292
const v3Options = { v3ClientName, v3ClientPackageName };
9393

94+
// Remove redundant `Types` from Client Types.
95+
removeTypesFromTSQualifiedName(j, source, v2ClientName);
9496
addClientModules(j, source, { ...v2Options, ...v3Options, clientIdentifiers, importType });
9597
replaceTSTypeReference(j, source, { ...v2Options, v3ClientName });
9698
removeClientModule(j, source, { ...v2Options, importType });
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./getClientTypeNames";
22
export * from "./getV3ClientType";
33
export * from "./getV3ClientTypes";
4+
export * from "./removeTypesFromTSQualifiedName";
45
export * from "./replaceTSTypeReference";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
export const removeTypesFromTSQualifiedName = (
4+
j: JSCodeshift,
5+
source: Collection<unknown>,
6+
v2ClientName: string
7+
) => {
8+
// Support for DynamoDB.DocumentClient
9+
const [, clientNameSuffix] = v2ClientName.split(".");
10+
const clientName = clientNameSuffix ? clientNameSuffix : v2ClientName;
11+
12+
source
13+
.find(j.TSQualifiedName, {
14+
left: { type: "Identifier", name: clientName },
15+
right: { type: "Identifier", name: "Types" },
16+
})
17+
.replaceWith((nodePath) => nodePath.node.left);
18+
19+
source
20+
.find(j.TSQualifiedName, {
21+
left: { type: "TSQualifiedName", right: { type: "Identifier", name: clientName } },
22+
right: { type: "Identifier", name: "Types" },
23+
})
24+
.replaceWith((nodePath) => nodePath.node.left);
25+
};

0 commit comments

Comments
 (0)