Skip to content

Commit 339e74a

Browse files
authored
Handle v2 client imports in case of ImportNamespaceSpecifier (#57)
1 parent 3f0278e commit 339e74a

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

.changeset/little-yaks-heal.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+
Handle v2 client imports in case of ImportNamespaceSpecifier
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as DynamoDB from "aws-sdk/clients/dynamodb";
2+
3+
const client = new DynamoDB();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const client = new DynamoDB();

src/transforms/v2-to-v3/utils/getV2ClientImportNames.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
2-
import findImports from "jscodeshift-find-imports";
32

43
import { CLIENT_NAMES } from "./config";
54

65
export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>): string[] => {
76
const v2ClientImportNames = [];
8-
const { statement } = j.template;
97

108
for (const clientName of CLIENT_NAMES) {
11-
const importStatement = `import ${clientName} from 'aws-sdk/clients/${clientName.toLowerCase()}'`;
12-
const imports = findImports(source, statement([importStatement]));
13-
14-
for (const importObj of Object.values(imports)) {
15-
if (importObj.type === "Identifier") v2ClientImportNames.push(importObj.name);
16-
}
9+
// Add specifier name to v2ClientImportNames if it is imported in the source.
10+
source
11+
.find(j.ImportDeclaration, {
12+
source: { value: `aws-sdk/clients/${clientName.toLowerCase()}` },
13+
})
14+
.forEach((declerationPath) => {
15+
declerationPath.value.specifiers.forEach((specifier) => {
16+
if (
17+
specifier.type === "ImportDefaultSpecifier" ||
18+
specifier.type === "ImportNamespaceSpecifier"
19+
) {
20+
v2ClientImportNames.push(specifier.local.name);
21+
}
22+
});
23+
});
1724
}
1825

1926
return v2ClientImportNames;

src/transforms/v2-to-v3/utils/removeV2ClientImport.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@ export const removeV2ClientImport = (
1010
.find(j.ImportDeclaration, {
1111
specifiers: [
1212
{
13-
type: "ImportDefaultSpecifier",
1413
local: { name: v2ClientName },
1514
},
1615
],
1716
source: { value: importSourceName },
1817
})
1918
.forEach((declerationPath) => {
20-
// Remove default import from ImportDeclaration.
19+
// Remove import from ImportDeclaration.
2120
declerationPath.value.specifiers = declerationPath.value.specifiers.filter(
22-
(specifier) =>
23-
specifier.type !== "ImportDefaultSpecifier" && specifier.local.name !== v2ClientName
21+
(specifier) => specifier.local.name !== v2ClientName
2422
);
2523
// Remove ImportDeclaration if there are no other imports.
2624
if (declerationPath.value.specifiers.length === 0) {

0 commit comments

Comments
 (0)