Skip to content

Commit d0d459a

Browse files
authored
Split getV2ClientModuleNames into import/require components (#72)
1 parent dd83c82 commit d0d459a

File tree

4 files changed

+50
-39
lines changed

4 files changed

+50
-39
lines changed

.changeset/nice-melons-admire.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+
Split getV2ClientModuleNames into import/require components
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { CLIENT_NAMES } from "./config";
4+
5+
export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>): string[] =>
6+
CLIENT_NAMES.map((clientName) => `aws-sdk/clients/${clientName.toLowerCase()}`)
7+
.map(
8+
(v2ClientImportSourceValue) =>
9+
source
10+
.find(j.ImportDeclaration, {
11+
source: { value: v2ClientImportSourceValue },
12+
})
13+
.nodes()[0]?.specifiers[0]?.local.name
14+
)
15+
.filter((v2ClientImportLocalName) => v2ClientImportLocalName !== undefined);
Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,10 @@
1-
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
1+
import { Collection, JSCodeshift } from "jscodeshift";
22

3-
import { CLIENT_NAMES } from "./config";
3+
import { containsRequire } from "./containsRequire";
4+
import { getV2ClientImportNames } from "./getV2ClientImportNames";
5+
import { getV2ClientRequireNames } from "./getV2ClientRequireNames";
46

5-
export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] => {
6-
const v2ClientModuleNames = [];
7-
8-
for (const clientName of CLIENT_NAMES) {
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-
v2ClientModuleNames.push(specifier.local.name);
21-
}
22-
});
23-
});
24-
25-
// Add specifier name to v2ClientImportNames if it is required in the source.
26-
source
27-
.find(j.VariableDeclarator, {
28-
id: { type: "Identifier" },
29-
init: {
30-
type: "CallExpression",
31-
callee: { type: "Identifier", name: "require" },
32-
arguments: [{ type: "Literal", value: `aws-sdk/clients/${clientName.toLowerCase()}` }],
33-
},
34-
})
35-
.forEach((declerationPath) => {
36-
v2ClientModuleNames.push((declerationPath.value.id as Identifier).name);
37-
});
38-
}
39-
40-
return v2ClientModuleNames;
41-
};
7+
export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] =>
8+
containsRequire(j, source)
9+
? getV2ClientRequireNames(j, source)
10+
: getV2ClientImportNames(j, source);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
2+
3+
import { CLIENT_NAMES } from "./config";
4+
5+
export const getV2ClientRequireNames = (j: JSCodeshift, source: Collection<any>): string[] =>
6+
CLIENT_NAMES.map((clientName) => `aws-sdk/clients/${clientName.toLowerCase()}`)
7+
.map(
8+
(v2ClientImportSourceValue) =>
9+
(
10+
source
11+
.find(j.VariableDeclarator, {
12+
id: { type: "Identifier" },
13+
init: {
14+
type: "CallExpression",
15+
callee: { type: "Identifier", name: "require" },
16+
arguments: [{ type: "Literal", value: v2ClientImportSourceValue }],
17+
},
18+
})
19+
.nodes()[0]?.id as Identifier
20+
)?.name
21+
)
22+
.filter((v2ClientImportLocalName) => v2ClientImportLocalName !== undefined);

0 commit comments

Comments
 (0)