diff --git a/.changeset/hungry-grapes-burn.md b/.changeset/hungry-grapes-burn.md new file mode 100644 index 000000000..5da33d079 --- /dev/null +++ b/.changeset/hungry-grapes-burn.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Split getClientNewExpression into global/local name diff --git a/src/transforms/v2-to-v3/apis/getClientIdNamesFromNewExpr.ts b/src/transforms/v2-to-v3/apis/getClientIdNamesFromNewExpr.ts index b4f11b1f8..d2596eff8 100644 --- a/src/transforms/v2-to-v3/apis/getClientIdNamesFromNewExpr.ts +++ b/src/transforms/v2-to-v3/apis/getClientIdNamesFromNewExpr.ts @@ -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; @@ -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}`) ) ); } diff --git a/src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts b/src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts index ad269b1f6..8521db075 100644 --- a/src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts +++ b/src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts @@ -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 { @@ -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 }) ); diff --git a/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts b/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts index 2b0019736..50b71a9ed 100644 --- a/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts +++ b/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts @@ -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 { @@ -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( @@ -34,7 +37,7 @@ export const replaceDocClientCreation = ( source .find( j.NewExpression, - getClientNewExpression({ v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}` }) + getClientNewExpressionFromLocalName(`${v2ClientLocalName}.${DOCUMENT_CLIENT}`) ) .replaceWith((v2DocClientNewExpression) => j.callExpression( diff --git a/src/transforms/v2-to-v3/client-names/getNamesFromNewExpr.ts b/src/transforms/v2-to-v3/client-names/getNamesFromNewExpr.ts index 50e403c96..5a8e1f712 100644 --- a/src/transforms/v2-to-v3/client-names/getNamesFromNewExpr.ts +++ b/src/transforms/v2-to-v3/client-names/getNamesFromNewExpr.ts @@ -1,7 +1,7 @@ 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, @@ -9,7 +9,7 @@ export const getNamesFromNewExpr = ( 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 @@ -17,7 +17,7 @@ export const getNamesFromNewExpr = ( ...source .find( j.NewExpression, - getClientNewExpression({ v2GlobalName, v2ClientName: DYNAMODB_DOCUMENT_CLIENT }) + getClientNewExpressionFromGlobalName(v2GlobalName, DYNAMODB_DOCUMENT_CLIENT) ) .nodes() .map( diff --git a/src/transforms/v2-to-v3/modules/getNewExpressionCount.ts b/src/transforms/v2-to-v3/modules/getNewExpressionCount.ts index 03ae259d9..cf756b2cc 100644 --- a/src/transforms/v2-to-v3/modules/getNewExpressionCount.ts +++ b/src/transforms/v2-to-v3/modules/getNewExpressionCount.ts @@ -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 = ( @@ -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; diff --git a/src/transforms/v2-to-v3/utils/getClientNewExpression.ts b/src/transforms/v2-to-v3/utils/getClientNewExpression.ts deleted file mode 100644 index fe5505e69..000000000 --- a/src/transforms/v2-to-v3/utils/getClientNewExpression.ts +++ /dev/null @@ -1,81 +0,0 @@ -import type { NewExpression } from "jscodeshift"; - -export interface ClientNewExpressionOptions { - v2ClientLocalName?: string; - v2ClientName?: string; - v2GlobalName?: string; -} - -export const getClientNewExpression = ({ - v2ClientLocalName, - v2ClientName, - v2GlobalName, -}: ClientNewExpressionOptions): NewExpression => { - if (!v2GlobalName && !v2ClientLocalName) { - throw new Error( - `One of the following options must be provided: v2ClientLocalName, v2GlobalName` - ); - } - - if (v2GlobalName && v2ClientLocalName) { - throw new Error( - `Only one of the following options must be provided: v2ClientLocalName, v2GlobalName` - ); - } - - if (v2GlobalName) { - 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; - } - - // 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; -}; diff --git a/src/transforms/v2-to-v3/utils/getClientNewExpressionFromGlobalName.ts b/src/transforms/v2-to-v3/utils/getClientNewExpressionFromGlobalName.ts new file mode 100644 index 000000000..f9079418c --- /dev/null +++ b/src/transforms/v2-to-v3/utils/getClientNewExpressionFromGlobalName.ts @@ -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; +}; diff --git a/src/transforms/v2-to-v3/utils/getClientNewExpressionFromLocalName.ts b/src/transforms/v2-to-v3/utils/getClientNewExpressionFromLocalName.ts new file mode 100644 index 000000000..7175d2921 --- /dev/null +++ b/src/transforms/v2-to-v3/utils/getClientNewExpressionFromLocalName.ts @@ -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; +}; diff --git a/src/transforms/v2-to-v3/utils/index.ts b/src/transforms/v2-to-v3/utils/index.ts index e7efe5903..45a1144c3 100644 --- a/src/transforms/v2-to-v3/utils/index.ts +++ b/src/transforms/v2-to-v3/utils/index.ts @@ -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";