Skip to content

Commit

Permalink
Add utility getV2ClientNewExpression (#224)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Dec 27, 2022
1 parent 9d2c537 commit 27a2f24
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 31 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-hats-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add utility getV2ClientNewExpression
20 changes: 5 additions & 15 deletions src/transforms/v2-to-v3/utils/get/getV2ClientIdNamesFromNewExpr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Collection, Identifier, JSCodeshift, NewExpression } from "jscodeshift";

import { getMergedArrayWithoutDuplicates } from "./getMergedArrayWithoutDuplicates";
import { getV2ClientNewExpression } from "./getV2ClientNewExpression";

export interface GetV2ClientIdNamesFromNewExprOptions {
v2ClientName: string;
Expand Down Expand Up @@ -38,25 +39,14 @@ export const getV2ClientIdNamesFromNewExpr = (
source: Collection<unknown>,
{ v2DefaultModuleName, v2ClientName }: GetV2ClientIdNamesFromNewExprOptions
): string[] => {
const defaultNewExpr = {
type: "NewExpression",
callee: {
object: { type: "Identifier", name: v2DefaultModuleName },
property: { type: "Identifier", name: v2ClientName },
},
} as NewExpression;

const clientNewExpr = {
type: "NewExpression",
callee: { type: "Identifier", name: v2ClientName },
} as NewExpression;

const namesFromDefaultModule = [];
const namesFromServiceModule = [];

for (const getNames of [getNamesFromVariableDeclarator, getNamesFromAssignmentPattern]) {
namesFromDefaultModule.push(...getNames(j, source, defaultNewExpr));
namesFromServiceModule.push(...getNames(j, source, clientNewExpr));
namesFromDefaultModule.push(
...getNames(j, source, getV2ClientNewExpression({ v2DefaultModuleName, v2ClientName }))
);
namesFromServiceModule.push(...getNames(j, source, getV2ClientNewExpression({ v2ClientName })));
}

return getMergedArrayWithoutDuplicates(namesFromDefaultModule, namesFromServiceModule);
Expand Down
10 changes: 3 additions & 7 deletions src/transforms/v2-to-v3/utils/get/getV2ClientNamesFromNewExpr.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { Collection, Identifier, JSCodeshift, MemberExpression } from "jscodeshift";

import { getV2ClientNewExpression } from "../get";

export const getV2ClientNamesFromNewExpr = (
j: JSCodeshift,
source: Collection<unknown>,
v2DefaultModuleName: string
): string[] =>
source
.find(j.NewExpression, {
callee: {
type: "MemberExpression",
object: { type: "Identifier", name: v2DefaultModuleName },
property: { type: "Identifier" },
},
})
.find(j.NewExpression, getV2ClientNewExpression({ v2DefaultModuleName }))
.nodes()
.map(
(newExpression) => ((newExpression.callee as MemberExpression).property as Identifier).name
Expand Down
33 changes: 33 additions & 0 deletions src/transforms/v2-to-v3/utils/get/getV2ClientNewExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { NewExpression } from "jscodeshift";

export interface ClientNewExpressionOptions {
v2DefaultModuleName?: string;
v2ClientName?: string;
isDocumentClient?: boolean;
}

export const getV2ClientNewExpression = ({
v2DefaultModuleName,
v2ClientName,
}: ClientNewExpressionOptions): NewExpression => {
if (!v2DefaultModuleName && !v2ClientName) {
throw new Error(
`At least one of the following options must be provided: v2DefaultModuleName, v2ClientName`
);
}

if (v2DefaultModuleName) {
return {
type: "NewExpression",
callee: {
object: { type: "Identifier", name: v2DefaultModuleName },
property: { type: "Identifier", ...(v2ClientName && { name: v2ClientName }) },
},
} as NewExpression;
}

return {
type: "NewExpression",
callee: { type: "Identifier", name: v2ClientName },
} as NewExpression;
};
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/utils/get/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from "./getV2ClientIdentifiers";
export * from "./getV2ClientIdThisExpressions";
export * from "./getV2ClientNames";
export * from "./getV2ClientNamesFromDefault";
export * from "./getV2ClientNewExpression";
export * from "./getV2ClientTypeNames";
export * from "./getV2DefaultModuleName";
export * from "./getV2ServiceModulePath";
Expand Down
13 changes: 4 additions & 9 deletions src/transforms/v2-to-v3/utils/replace/replaceClientCreation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getV2ClientNewExpression } from "../get";

export interface ReplaceClientCreationOptions {
v2ClientName: string;
v3ClientName: string;
Expand All @@ -14,12 +16,7 @@ export const replaceClientCreation = (
): void => {
// Replace clients created with default module.
source
.find(j.NewExpression, {
callee: {
object: { type: "Identifier", name: v2DefaultModuleName },
property: { type: "Identifier", name: v2ClientName },
},
})
.find(j.NewExpression, getV2ClientNewExpression({ v2DefaultModuleName, v2ClientName }))
.replaceWith((nodePath) => {
const { node } = nodePath;
node.callee = j.identifier(v3ClientName);
Expand All @@ -28,9 +25,7 @@ export const replaceClientCreation = (

// Replace clients created with client module.
source
.find(j.NewExpression, {
callee: { type: "Identifier", name: v2ClientName },
})
.find(j.NewExpression, getV2ClientNewExpression({ v2ClientName }))
.replaceWith((nodePath) => {
const { node } = nodePath;
node.callee = j.identifier(v3ClientName);
Expand Down

0 comments on commit 27a2f24

Please sign in to comment.