Skip to content

Commit

Permalink
Split getV2ClientModuleNames into import/require components (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Mar 8, 2022
1 parent dd83c82 commit d0d459a
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 39 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-melons-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Split getV2ClientModuleNames into import/require components
15 changes: 15 additions & 0 deletions src/transforms/v2-to-v3/utils/getV2ClientImportNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { CLIENT_NAMES } from "./config";

export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>): string[] =>
CLIENT_NAMES.map((clientName) => `aws-sdk/clients/${clientName.toLowerCase()}`)
.map(
(v2ClientImportSourceValue) =>
source
.find(j.ImportDeclaration, {
source: { value: v2ClientImportSourceValue },
})
.nodes()[0]?.specifiers[0]?.local.name
)
.filter((v2ClientImportLocalName) => v2ClientImportLocalName !== undefined);
47 changes: 8 additions & 39 deletions src/transforms/v2-to-v3/utils/getV2ClientModuleNames.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,10 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
import { Collection, JSCodeshift } from "jscodeshift";

import { CLIENT_NAMES } from "./config";
import { containsRequire } from "./containsRequire";
import { getV2ClientImportNames } from "./getV2ClientImportNames";
import { getV2ClientRequireNames } from "./getV2ClientRequireNames";

export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] => {
const v2ClientModuleNames = [];

for (const clientName of CLIENT_NAMES) {
// Add specifier name to v2ClientImportNames if it is imported in the source.
source
.find(j.ImportDeclaration, {
source: { value: `aws-sdk/clients/${clientName.toLowerCase()}` },
})
.forEach((declerationPath) => {
declerationPath.value.specifiers.forEach((specifier) => {
if (
specifier.type === "ImportDefaultSpecifier" ||
specifier.type === "ImportNamespaceSpecifier"
) {
v2ClientModuleNames.push(specifier.local.name);
}
});
});

// Add specifier name to v2ClientImportNames if it is required in the source.
source
.find(j.VariableDeclarator, {
id: { type: "Identifier" },
init: {
type: "CallExpression",
callee: { type: "Identifier", name: "require" },
arguments: [{ type: "Literal", value: `aws-sdk/clients/${clientName.toLowerCase()}` }],
},
})
.forEach((declerationPath) => {
v2ClientModuleNames.push((declerationPath.value.id as Identifier).name);
});
}

return v2ClientModuleNames;
};
export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] =>
containsRequire(j, source)
? getV2ClientRequireNames(j, source)
: getV2ClientImportNames(j, source);
22 changes: 22 additions & 0 deletions src/transforms/v2-to-v3/utils/getV2ClientRequireNames.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { CLIENT_NAMES } from "./config";

export const getV2ClientRequireNames = (j: JSCodeshift, source: Collection<any>): string[] =>
CLIENT_NAMES.map((clientName) => `aws-sdk/clients/${clientName.toLowerCase()}`)
.map(
(v2ClientImportSourceValue) =>
(
source
.find(j.VariableDeclarator, {
id: { type: "Identifier" },
init: {
type: "CallExpression",
callee: { type: "Identifier", name: "require" },
arguments: [{ type: "Literal", value: v2ClientImportSourceValue }],
},
})
.nodes()[0]?.id as Identifier
)?.name
)
.filter((v2ClientImportLocalName) => v2ClientImportLocalName !== undefined);

0 comments on commit d0d459a

Please sign in to comment.