Skip to content

Commit

Permalink
Split getClientNewExpression into global/local name (#899)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jul 12, 2024
1 parent 01efdc8 commit a301bf9
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 99 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-grapes-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Split getClientNewExpression into global/local name
13 changes: 8 additions & 5 deletions src/transforms/v2-to-v3/apis/getClientIdNamesFromNewExpr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import type {
} from "jscodeshift";

import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
import { getClientNewExpression } from "../utils";
import {
getClientNewExpressionFromGlobalName,
getClientNewExpressionFromLocalName,
} from "../utils";

export interface GetClientIdNamesFromNewExprOptions {
v2ClientName: string;
Expand Down Expand Up @@ -70,27 +73,27 @@ export const getClientIdNamesFromNewExpr = (
]) {
if (v2GlobalName) {
namesFromGlobalModule.push(
...getNames(j, source, getClientNewExpression({ v2GlobalName, v2ClientName }))
...getNames(j, source, getClientNewExpressionFromGlobalName(v2GlobalName, v2ClientName))
);
if (v2ClientName === DYNAMODB) {
namesFromGlobalModule.push(
...getNames(
j,
source,
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
getClientNewExpressionFromGlobalName(v2GlobalName, DYNAMODB_DOCUMENT_CLIENT)
)
);
}
}
namesFromServiceModule.push(
...getNames(j, source, getClientNewExpression({ v2ClientLocalName }))
...getNames(j, source, getClientNewExpressionFromLocalName(v2ClientLocalName))
);
if (v2ClientName === DYNAMODB) {
namesFromServiceModule.push(
...getNames(
j,
source,
getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` })
getClientNewExpressionFromLocalName(`${v2ClientLocalName}.${DOCUMENT_CLIENT}`)
)
);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Collection, JSCodeshift, ObjectExpression } from "jscodeshift";
import { getClientNewExpression } from "../utils";
import {
getClientNewExpressionFromGlobalName,
getClientNewExpressionFromLocalName,
} from "../utils";
import { getNewClientExpression } from "./getNewClientExpression";

export interface ReplaceClientCreationOptions {
Expand All @@ -25,14 +28,14 @@ export const replaceClientCreation = (
const clientName = v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName;

source
.find(j.NewExpression, getClientNewExpression({ v2ClientName, v2ClientLocalName }))
.find(j.NewExpression, getClientNewExpressionFromLocalName(v2ClientLocalName))
.replaceWith((v2ClientNewExpression) =>
getNewClientExpression(j, clientName, { v2ClientNewExpression, awsGlobalConfig })
);

if (v2GlobalName) {
source
.find(j.NewExpression, getClientNewExpression({ v2GlobalName, v2ClientName }))
.find(j.NewExpression, getClientNewExpressionFromGlobalName(v2GlobalName, v2ClientName))
.replaceWith((v2ClientNewExpression) =>
getNewClientExpression(j, clientName, { v2ClientNewExpression, awsGlobalConfig })
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Collection, JSCodeshift } from "jscodeshift";

import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT, DYNAMODB_DOCUMENT_CLIENT } from "../config";
import { getClientNewExpression } from "../utils";
import {
getClientNewExpressionFromGlobalName,
getClientNewExpressionFromLocalName,
} from "../utils";
import { getDynamoDBDocClientArgs } from "./getDynamoDBDocClientArgs";

export interface ReplaceDocClientCreationOptions {
Expand All @@ -21,7 +24,7 @@ export const replaceDocClientCreation = (
source
.find(
j.NewExpression,
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
getClientNewExpressionFromGlobalName(v2GlobalName, DYNAMODB_DOCUMENT_CLIENT)
)
.replaceWith((v2DocClientNewExpression) =>
j.callExpression(
Expand All @@ -34,7 +37,7 @@ export const replaceDocClientCreation = (
source
.find(
j.NewExpression,
getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` })
getClientNewExpressionFromLocalName(`${v2ClientLocalName}.${DOCUMENT_CLIENT}`)
)
.replaceWith((v2DocClientNewExpression) =>
j.callExpression(
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/v2-to-v3/client-names/getNamesFromNewExpr.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import type { Collection, Identifier, JSCodeshift, MemberExpression } from "jscodeshift";

import { DYNAMODB_DOCUMENT_CLIENT } from "../config";
import { getClientNewExpression } from "../utils";
import { getClientNewExpressionFromGlobalName } from "../utils";

export const getNamesFromNewExpr = (
j: JSCodeshift,
source: Collection<unknown>,
v2GlobalName: string
): string[] => [
...source
.find(j.NewExpression, getClientNewExpression({ v2GlobalName }))
.find(j.NewExpression, getClientNewExpressionFromGlobalName(v2GlobalName))
.nodes()
.map(
(newExpression) => ((newExpression.callee as MemberExpression).property as Identifier).name
),
...source
.find(
j.NewExpression,
getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT })
getClientNewExpressionFromGlobalName(v2GlobalName, DYNAMODB_DOCUMENT_CLIENT)
)
.nodes()
.map(
Expand Down
9 changes: 6 additions & 3 deletions src/transforms/v2-to-v3/modules/getNewExpressionCount.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import type { Collection, JSCodeshift } from "jscodeshift";

import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config";
import { getClientNewExpression } from "../utils";
import {
getClientNewExpressionFromGlobalName,
getClientNewExpressionFromLocalName,
} from "../utils";
import type { ClientModulesOptions } from "./types";

export const getNewExpressionCount = (
Expand All @@ -15,14 +18,14 @@ export const getNewExpressionCount = (
if (v2GlobalName) {
const newExpressionsFromGlobalName = source.find(
j.NewExpression,
getClientNewExpression({ v2ClientName, v2GlobalName })
getClientNewExpressionFromGlobalName(v2GlobalName, v2ClientName)
);
newExpressionCount += newExpressionsFromGlobalName.length;
}

const newExpressionsFromClientLocalName = source.find(
j.NewExpression,
getClientNewExpression({ v2ClientLocalName })
getClientNewExpressionFromLocalName(v2ClientLocalName)
);
newExpressionCount += newExpressionsFromClientLocalName.length;

Expand Down
81 changes: 0 additions & 81 deletions src/transforms/v2-to-v3/utils/getClientNewExpression.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import type { NewExpression } from "jscodeshift";

export const getClientNewExpressionFromGlobalName = (
v2GlobalName: string,
v2ClientName?: string
): NewExpression => {
if (v2ClientName) {
// Support for DynamoDB.DocumentClient
const [clientName, subClientName] = v2ClientName.split(".");

if (subClientName) {
return {
type: "NewExpression",
callee: {
type: "MemberExpression",
object: {
type: "MemberExpression",
object: { type: "Identifier", name: v2GlobalName },
property: { type: "Identifier", name: clientName },
},
property: { type: "Identifier", name: subClientName },
},
} as NewExpression;
}

return {
type: "NewExpression",
callee: {
object: { type: "Identifier", name: v2GlobalName },
property: { type: "Identifier", name: clientName },
},
} as NewExpression;
}

return {
type: "NewExpression",
callee: {
object: { type: "Identifier", name: v2GlobalName },
property: { type: "Identifier" },
},
} as NewExpression;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { NewExpression } from "jscodeshift";

export const getClientNewExpressionFromLocalName = (v2ClientLocalName: string): NewExpression => {
// Support for DynamoDB.DocumentClient
const [clientName, subClientName] = v2ClientLocalName.split(".");

if (subClientName) {
return {
type: "NewExpression",
callee: {
object: { type: "Identifier", name: clientName },
property: { type: "Identifier", name: subClientName },
},
} as NewExpression;
}

return {
type: "NewExpression",
callee: { type: "Identifier", name: clientName },
} as NewExpression;
};
3 changes: 2 additions & 1 deletion src/transforms/v2-to-v3/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from "./getClientDeepImportPath";
export * from "./getClientNewExpression";
export * from "./getClientNewExpressionFromGlobalName";
export * from "./getClientNewExpressionFromLocalName";
export * from "./getFormattedSourceString";
export * from "./getMostUsedIndentationType";
export * from "./getMostUsedStringLiteralQuote";
Expand Down

0 comments on commit a301bf9

Please sign in to comment.