Skip to content

Commit

Permalink
Remove client import if clients are not created (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Jan 4, 2023
1 parent c63a781 commit 135b8ab
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-pots-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Remove client import if clients are not created
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import AWS_S3, { S3 } from "@aws-sdk/client-s3";
import AWS_S3 from "@aws-sdk/client-s3";

const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }];
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import AWS_S3, { S3 } from "@aws-sdk/client-s3";
import AWS_S3 from "@aws-sdk/client-s3";

const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }];
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import AWS_S3, { S3 } from "@aws-sdk/client-s3";
import AWS_S3 from "@aws-sdk/client-s3";

const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }];
11 changes: 9 additions & 2 deletions src/transforms/v2-to-v3/modules/addV3ClientImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { getV3ClientTypeNames } from "../ts-type";
import { addV3ClientDefaultImport } from "./addV3ClientDefaultImport";
import { addV3ClientImportEquals } from "./addV3ClientImportEquals";
import { addV3ClientNamedImport } from "./addV3ClientNamedImport";
import { getClientTSTypeRefCount } from "./getClientTSTypeRefCount";
import { getNewExpressionCount } from "./getNewExpressionCount";
import { hasImportEquals } from "./hasImportEquals";
import { V3ClientModulesOptions } from "./types";

Expand All @@ -17,8 +19,6 @@ export const addV3ClientImports = (
return;
}

addV3ClientNamedImport(j, source, options);

const { v2ClientLocalName, v2ClientName, v2GlobalName } = options;
const v3ClientTypeNames = getV3ClientTypeNames(j, source, {
v2ClientLocalName,
Expand All @@ -30,4 +30,11 @@ export const addV3ClientImports = (
if (v3ClientTypeNames.length > 0) {
addV3ClientDefaultImport(j, source, options);
}

if (
getNewExpressionCount(j, source, options) > 0 ||
getClientTSTypeRefCount(j, source, options) > 0
) {
addV3ClientNamedImport(j, source, options);
}
};
28 changes: 28 additions & 0 deletions src/transforms/v2-to-v3/modules/getClientTSTypeRefCount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getV2ClientTSTypeRef } from "../utils";
import { V3ClientModulesOptions } from "./types";

export const getClientTSTypeRefCount = (
j: JSCodeshift,
source: Collection<unknown>,
{ v2ClientName, v2ClientLocalName, v2GlobalName }: V3ClientModulesOptions
): number => {
let clientTSTypeRefCount = 0;

if (v2GlobalName) {
const clienTSTypeRefFromGlobalName = source.find(
j.TSTypeReference,
getV2ClientTSTypeRef({ v2ClientName, v2GlobalName })
);
clientTSTypeRefCount += clienTSTypeRefFromGlobalName.length;
}

const clienTSTypeRefFromClientLocalName = source.find(
j.TSTypeReference,
getV2ClientTSTypeRef({ v2ClientLocalName })
);
clientTSTypeRefCount += clienTSTypeRefFromClientLocalName.length;

return clientTSTypeRefCount;
};
4 changes: 2 additions & 2 deletions src/transforms/v2-to-v3/modules/getNewExpressionCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export const getNewExpressionCount = (
let newExpressionCount = 0;

if (v2GlobalName) {
const newExpressionsFromGlobal = source.find(
const newExpressionsFromGlobalName = source.find(
j.NewExpression,
getV2ClientNewExpression({ v2ClientName, v2GlobalName })
);
newExpressionCount += newExpressionsFromGlobal.length;
newExpressionCount += newExpressionsFromGlobalName.length;
}

const newExpressionsFromClientLocalName = source.find(
Expand Down

0 comments on commit 135b8ab

Please sign in to comment.