Skip to content

Commit 135b8ab

Browse files
authored
Remove client import if clients are not created (#301)
1 parent c63a781 commit 135b8ab

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

.changeset/many-pots-develop.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+
Remove client import if clients are not created
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import AWS_S3, { S3 } from "@aws-sdk/client-s3";
1+
import AWS_S3 from "@aws-sdk/client-s3";
22

33
const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import AWS_S3, { S3 } from "@aws-sdk/client-s3";
1+
import AWS_S3 from "@aws-sdk/client-s3";
22

33
const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import AWS_S3, { S3 } from "@aws-sdk/client-s3";
1+
import AWS_S3 from "@aws-sdk/client-s3";
22

33
const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }];

src/transforms/v2-to-v3/modules/addV3ClientImports.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { getV3ClientTypeNames } from "../ts-type";
44
import { addV3ClientDefaultImport } from "./addV3ClientDefaultImport";
55
import { addV3ClientImportEquals } from "./addV3ClientImportEquals";
66
import { addV3ClientNamedImport } from "./addV3ClientNamedImport";
7+
import { getClientTSTypeRefCount } from "./getClientTSTypeRefCount";
8+
import { getNewExpressionCount } from "./getNewExpressionCount";
79
import { hasImportEquals } from "./hasImportEquals";
810
import { V3ClientModulesOptions } from "./types";
911

@@ -17,8 +19,6 @@ export const addV3ClientImports = (
1719
return;
1820
}
1921

20-
addV3ClientNamedImport(j, source, options);
21-
2222
const { v2ClientLocalName, v2ClientName, v2GlobalName } = options;
2323
const v3ClientTypeNames = getV3ClientTypeNames(j, source, {
2424
v2ClientLocalName,
@@ -30,4 +30,11 @@ export const addV3ClientImports = (
3030
if (v3ClientTypeNames.length > 0) {
3131
addV3ClientDefaultImport(j, source, options);
3232
}
33+
34+
if (
35+
getNewExpressionCount(j, source, options) > 0 ||
36+
getClientTSTypeRefCount(j, source, options) > 0
37+
) {
38+
addV3ClientNamedImport(j, source, options);
39+
}
3340
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { getV2ClientTSTypeRef } from "../utils";
4+
import { V3ClientModulesOptions } from "./types";
5+
6+
export const getClientTSTypeRefCount = (
7+
j: JSCodeshift,
8+
source: Collection<unknown>,
9+
{ v2ClientName, v2ClientLocalName, v2GlobalName }: V3ClientModulesOptions
10+
): number => {
11+
let clientTSTypeRefCount = 0;
12+
13+
if (v2GlobalName) {
14+
const clienTSTypeRefFromGlobalName = source.find(
15+
j.TSTypeReference,
16+
getV2ClientTSTypeRef({ v2ClientName, v2GlobalName })
17+
);
18+
clientTSTypeRefCount += clienTSTypeRefFromGlobalName.length;
19+
}
20+
21+
const clienTSTypeRefFromClientLocalName = source.find(
22+
j.TSTypeReference,
23+
getV2ClientTSTypeRef({ v2ClientLocalName })
24+
);
25+
clientTSTypeRefCount += clienTSTypeRefFromClientLocalName.length;
26+
27+
return clientTSTypeRefCount;
28+
};

src/transforms/v2-to-v3/modules/getNewExpressionCount.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export const getNewExpressionCount = (
1111
let newExpressionCount = 0;
1212

1313
if (v2GlobalName) {
14-
const newExpressionsFromGlobal = source.find(
14+
const newExpressionsFromGlobalName = source.find(
1515
j.NewExpression,
1616
getV2ClientNewExpression({ v2ClientName, v2GlobalName })
1717
);
18-
newExpressionCount += newExpressionsFromGlobal.length;
18+
newExpressionCount += newExpressionsFromGlobalName.length;
1919
}
2020

2121
const newExpressionsFromClientLocalName = source.find(

0 commit comments

Comments
 (0)