Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split getClientNewExpression into global/local name #899

Merged
merged 4 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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