Skip to content

Commit

Permalink
Remove passing and using importType in leaf functions
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 17, 2023
1 parent 143bddd commit bc7edd1
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 181 deletions.
21 changes: 4 additions & 17 deletions src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,16 @@ import {
ObjectProperty,
} from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST, S3 } from "../config";
import { ImportType } from "../modules";
import { getV3ClientTypeName } from "../ts-type";
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { ClientIdentifier } from "../types";
import { getClientApiCallExpression } from "./getClientApiCallExpression";
import { getCommandName } from "./getCommandName";

export interface ReplaceS3GetSignedUrlApiOptions {
clientIdentifiers: ClientIdentifier[];
importType: ImportType;
}

// Updates `s3.getSignedUrl()` API with `await getSignedUrl(s3, command)` API.
export const replaceS3GetSignedUrlApi = (
j: JSCodeshift,
source: Collection<unknown>,
{ clientIdentifiers, importType }: ReplaceS3GetSignedUrlApiOptions
clientIdentifiers: ClientIdentifier[]
): void => {
for (const clientId of clientIdentifiers) {
for (const getSignedUrlApiName of ["getSignedUrl", "getSignedUrlPromise"]) {
Expand Down Expand Up @@ -78,23 +71,17 @@ export const replaceS3GetSignedUrlApi = (
);
}

const commandName = getV3ClientTypeName(getCommandName(apiName), S3, importType);
const getSignedUrlArgs: (ClientIdentifier | NewExpression | ObjectExpression)[] = [
clientId,
j.newExpression(j.identifier(commandName), [params]),
j.newExpression(j.identifier(getCommandName(apiName)), [params]),
];

if (options.properties.length > 0) {
getSignedUrlArgs.push(options);
}

const getSignedUrlIdentifier = getV3ClientTypeName(
"getSignedUrl",
"s3_request_presigner",
importType
);
const outputCallExpression = j.callExpression.from({
callee: j.identifier(getSignedUrlIdentifier),
callee: j.identifier("getSignedUrl"),
arguments: getSignedUrlArgs,
});

Expand Down
12 changes: 2 additions & 10 deletions src/transforms/v2-to-v3/apis/replaceS3UploadApi.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { ImportType } from "../modules";
import { getV3ClientTypeName } from "../ts-type";
import { ClientIdentifier } from "../types";
import { getClientApiCallExpression } from "./getClientApiCallExpression";

export interface ReplaceS3UploadApiOptions {
clientIdentifiers: ClientIdentifier[];
importType: ImportType;
}

// Updates `s3.upload()` API with `new Upload()` API.
export const replaceS3UploadApi = (
j: JSCodeshift,
source: Collection<unknown>,
{ clientIdentifiers, importType }: ReplaceS3UploadApiOptions
clientIdentifiers: ClientIdentifier[]
): void => {
for (const clientId of clientIdentifiers) {
// Replace `.promise()` call with `.done()` if present.
Expand Down Expand Up @@ -66,8 +59,7 @@ export const replaceS3UploadApi = (
}
}

const uploadApi = getV3ClientTypeName("Upload", "lib_storage", importType);
return j.newExpression(j.identifier(uploadApi), [j.objectExpression(properties)]);
return j.newExpression(j.identifier("Upload"), [j.objectExpression(properties)]);
});
}
};
17 changes: 2 additions & 15 deletions src/transforms/v2-to-v3/apis/replaceWaiterApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { ImportType } from "../modules";
import { getV3ClientTypeName } from "../ts-type";
import { ClientIdentifier } from "../types";
import { getArgsWithoutWaiterConfig } from "./getArgsWithoutWaiterConfig";
import { getClientWaiterCallExpression } from "./getClientWaiterCallExpression";
Expand All @@ -10,27 +8,16 @@ import { getV3ClientWaiterApiName } from "./getV3ClientWaiterApiName";
import { getWaiterConfig } from "./getWaiterConfig";
import { getWaiterConfigValue } from "./getWaiterConfigValue";

export interface ReplaceWaiterApiOptions {
clientIdentifiers: ClientIdentifier[];
v2ClientName: string;
importType: ImportType;
}

// Updates .waitFor() API with waitUntil* API.
export const replaceWaiterApi = (
j: JSCodeshift,
source: Collection<unknown>,
{ clientIdentifiers, v2ClientName, importType }: ReplaceWaiterApiOptions
clientIdentifiers: ClientIdentifier[]
): void => {
for (const clientId of clientIdentifiers) {
const waiterStates = getClientWaiterStates(j, source, clientIdentifiers);

for (const waiterState of waiterStates) {
const v3WaiterApiName = getV3ClientTypeName(
getV3ClientWaiterApiName(waiterState),
v2ClientName,
importType
);
source
.find(j.CallExpression, getClientWaiterCallExpression(clientId, waiterState))
.replaceWith((callExpression) => {
Expand Down Expand Up @@ -67,7 +54,7 @@ export const replaceWaiterApi = (
})
);

return j.callExpression(j.identifier(v3WaiterApiName), [
return j.callExpression(j.identifier(getV3ClientWaiterApiName(waiterState)), [
j.objectExpression(properties),
getArgsWithoutWaiterConfig(callExpression.node.arguments[1]),
]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { ImportType } from "../modules";
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";

export interface GetDynamoDBDocClientArgsOptions {
v2ClientName: string;
v2ClientLocalName?: string;
importType: ImportType;
}

export const getDynamoDBDocClientArgs = (
j: JSCodeshift,
v2DocClientNewExpression: ASTPath<NewExpression>,
options: GetDynamoDBDocClientArgsOptions
v2ClientLocalName?: string
) => {
const dynamoDBDocClientOptions = j.objectExpression([]);

Expand Down Expand Up @@ -57,7 +50,7 @@ export const getDynamoDBDocClientArgs = (
}

return [
getDynamoDBForDocClient(j, v2DocClientNewExpression, options),
getDynamoDBForDocClient(j, v2DocClientNewExpression, v2ClientLocalName),
...(dynamoDBDocClientOptions.properties.length > 0 ? [dynamoDBDocClientOptions] : []),
];
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";

import { DYNAMODB, OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { ImportType } from "../modules";
import { getV3ClientTypeName } from "../ts-type";

export interface GetDynamoDBForDocClientOptions {
v2ClientName: string;
v2ClientLocalName?: string;
importType: ImportType;
}

export const getDynamoDBForDocClient = (
j: JSCodeshift,
v2DocClientNewExpression: ASTPath<NewExpression>,
{ v2ClientName, v2ClientLocalName, importType }: GetDynamoDBForDocClientOptions
v2ClientLocalName?: string
) => {
const v2DocClientArgs = v2DocClientNewExpression.node.arguments || [];

Expand Down Expand Up @@ -67,10 +59,5 @@ export const getDynamoDBForDocClient = (
}
}

const typeName =
importType === ImportType.IMPORT_EQUALS ? DYNAMODB : v2ClientLocalName ?? DYNAMODB;
return j.newExpression(
j.identifier(getV3ClientTypeName(typeName, v2ClientName, importType)),
v3DocClientNewExpressionArgs
);
return j.newExpression(j.identifier(v2ClientLocalName ?? DYNAMODB), v3DocClientNewExpressionArgs);
};
24 changes: 4 additions & 20 deletions src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { ImportType } from "../modules";
import { getV3ClientTypeName } from "../ts-type";
import { getClientNewExpression } from "../utils";

export interface ReplaceClientCreationOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
v3ClientName: string;
importType: ImportType;
}

// Replace v2 client creation with v3 client creation.
export const replaceClientCreation = (
j: JSCodeshift,
source: Collection<unknown>,
{
v2ClientName,
v2ClientLocalName,
v2GlobalName,
v3ClientName,
importType,
}: ReplaceClientCreationOptions
{ v2ClientName, v2ClientLocalName, v2GlobalName, v3ClientName }: ReplaceClientCreationOptions
): void => {
const clientName =
importType === ImportType.IMPORT_EQUALS || v2ClientName === v2ClientLocalName
? v3ClientName
: v2ClientLocalName;
const clientLocalName =
importType === ImportType.IMPORT_EQUALS ? v2ClientName : v2ClientLocalName;
const v3ClientConstructor = getV3ClientTypeName(clientName, clientLocalName, importType);
const clientName = v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName;

if (v2GlobalName) {
source
.find(j.NewExpression, getClientNewExpression({ v2GlobalName, v2ClientName }))
.replaceWith((v2ClientNewExpression) =>
j.newExpression(j.identifier(v3ClientConstructor), v2ClientNewExpression.node.arguments)
j.newExpression(j.identifier(clientName), v2ClientNewExpression.node.arguments)
);
}

source
.find(j.NewExpression, getClientNewExpression({ v2ClientName, v2ClientLocalName }))
.replaceWith((v2ClientNewExpression) =>
j.newExpression(j.identifier(v3ClientConstructor), v2ClientNewExpression.node.arguments)
j.newExpression(j.identifier(clientName), v2ClientNewExpression.node.arguments)
);
};
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT, DYNAMODB_DOCUMENT_CLIENT } from "../config";
import { ImportType, getImportEqualsLocalNameSuffix } from "../modules";
import { getV3ClientTypeName } from "../ts-type";
import { getClientNewExpression } from "../utils";
import { getDynamoDBDocClientArgs } from "./getDynamoDBDocClientArgs";

export interface ReplaceDocClientCreationOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
importType: ImportType;
}

export const replaceDocClientCreation = (
j: JSCodeshift,
source: Collection<unknown>,
{ v2ClientName, v2ClientLocalName, v2GlobalName, importType }: ReplaceDocClientCreationOptions
{ v2ClientName, v2ClientLocalName, v2GlobalName }: ReplaceDocClientCreationOptions
): void => {
if (v2ClientName !== DYNAMODB) return;

const v3DocClientName = getV3ClientTypeName(
DYNAMODB_DOCUMENT,
getImportEqualsLocalNameSuffix(v2ClientName, "@aws-sdk/lib-dynamodb"),
importType
);
const ddbDocClientOptions = { v2ClientName, v2ClientLocalName, importType };

if (v2GlobalName) {
source
.find(
Expand All @@ -35,8 +25,8 @@ export const replaceDocClientCreation = (
)
.replaceWith((v2DocClientNewExpression) =>
j.callExpression(
j.memberExpression(j.identifier(v3DocClientName), j.identifier("from")),
getDynamoDBDocClientArgs(j, v2DocClientNewExpression, ddbDocClientOptions)
j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")),
getDynamoDBDocClientArgs(j, v2DocClientNewExpression, v2ClientLocalName)
)
);
}
Expand All @@ -48,8 +38,8 @@ export const replaceDocClientCreation = (
)
.replaceWith((v2DocClientNewExpression) =>
j.callExpression(
j.memberExpression(j.identifier(v3DocClientName), j.identifier("from")),
getDynamoDBDocClientArgs(j, v2DocClientNewExpression, ddbDocClientOptions)
j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")),
getDynamoDBDocClientArgs(j, v2DocClientNewExpression, v2ClientLocalName)
)
);
};
2 changes: 1 addition & 1 deletion src/transforms/v2-to-v3/modules/addClientModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { ClientModulesOptions, ImportType } from "./types";
export const addClientModules = (
j: JSCodeshift,
source: Collection<unknown>,
options: ClientModulesOptions
options: ClientModulesOptions & { importType: ImportType }
): void => {
const { clientIdentifiers, v2ClientName, v3ClientName, v2ClientLocalName, importType } = options;

Expand Down
1 change: 0 additions & 1 deletion src/transforms/v2-to-v3/modules/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface ClientModulesOptions {
v3ClientName: string;
v3ClientPackageName: string;
clientIdentifiers: ClientIdentifier[];
importType: ImportType;
}

export interface ImportSpecifierOptions {
Expand Down
12 changes: 6 additions & 6 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,24 +72,24 @@ const transformer = async (file: FileInfo, api: API) => {
const v3Options = { v3ClientName, v3ClientPackageName };

addClientModules(j, source, { ...v2Options, ...v3Options, clientIdentifiers, importType });
replaceTSQualifiedName(j, source, { ...v2Options, v3ClientName, importType });
replaceTSQualifiedName(j, source, { ...v2Options, v3ClientName });
removeClientModule(j, source, { ...v2Options, importType });

if (v2ClientName === S3) {
// Needs to be called before removing promise calls, as replacement has `.done()` call.
replaceS3UploadApi(j, source, { clientIdentifiers, importType });
replaceS3UploadApi(j, source, clientIdentifiers);
}

removePromiseCalls(j, source, clientIdentifiers);

if (v2ClientName === S3) {
replaceS3GetSignedUrlApi(j, source, { clientIdentifiers, importType });
replaceS3GetSignedUrlApi(j, source, clientIdentifiers);
}

replaceWaiterApi(j, source, { clientIdentifiers, v2ClientName, importType });
replaceWaiterApi(j, source, clientIdentifiers);

replaceClientCreation(j, source, { ...v2Options, v3ClientName, importType });
replaceDocClientCreation(j, source, { ...v2Options, importType });
replaceClientCreation(j, source, { ...v2Options, v3ClientName });
replaceDocClientCreation(j, source, v2Options);
}
replaceAwsUtilFunctions(j, source, v2GlobalName);
removeGlobalModule(j, source, { v2GlobalName, importType });
Expand Down
Loading

0 comments on commit bc7edd1

Please sign in to comment.