Skip to content

Commit

Permalink
Store clientIdentifiersRecord at transformer level (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Aug 7, 2023
1 parent 2e5d752 commit 1431238
Show file tree
Hide file tree
Showing 21 changed files with 123 additions and 136 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-lemons-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Store clientIdentifiersRecord at transformer level
13 changes: 6 additions & 7 deletions src/transforms/v2-to-v3/apis/addNotSupportedClientComments.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { FUNCTION_TYPE_LIST } from "../config";
import { FUNCTION_TYPE_LIST, S3 } from "../config";
import { ClientIdentifier } from "../types";
import { getClientApiCallExpression } from "./getClientApiCallExpression";
import { getClientIdentifiers } from "./getClientIdentifiers";
import { getClientWaiterCallExpression } from "./getClientWaiterCallExpression";
import { getClientWaiterStates } from "./getClientWaiterStates";

export interface CommentsForUnsupportedAPIsOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
clientIdentifiers: ClientIdentifier[];
}

export const addNotSupportedClientComments = (
j: JSCodeshift,
source: Collection<unknown>,
options: CommentsForUnsupportedAPIsOptions
): void => {
const clientIdentifiers = getClientIdentifiers(j, source, options);
const { v2ClientName, clientIdentifiers } = options;

for (const clientId of clientIdentifiers) {
const waiterStates = getClientWaiterStates(j, source, options);
const waiterStates = getClientWaiterStates(j, source, clientIdentifiers);

for (const waiterState of waiterStates) {
source
Expand All @@ -46,7 +45,7 @@ export const addNotSupportedClientComments = (
}
}

if (options.v2ClientName === "S3") {
if (v2ClientName === S3) {
for (const clientId of clientIdentifiers) {
const apiMetadata = [
{
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/v2-to-v3/apis/getClientApiCallExpression.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CallExpression } from "jscodeshift";

import { ClientIdentifier } from "./getClientIdentifiers";
import { ClientIdentifier } from "../types";

export const getClientApiCallExpression = (
clientId: ClientIdentifier,
Expand Down
9 changes: 2 additions & 7 deletions src/transforms/v2-to-v3/apis/getClientIdThisExpressions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { Collection, Identifier, JSCodeshift, MemberExpression, ThisExpression } from "jscodeshift";

export interface ThisMemberExpression {
type: "MemberExpression";
object: ThisExpression;
property: Identifier;
}
import { Collection, Identifier, JSCodeshift, MemberExpression } from "jscodeshift";
import { ThisMemberExpression } from "../types";

const thisMemberExpression = { type: "MemberExpression", object: { type: "ThisExpression" } };

Expand Down
5 changes: 2 additions & 3 deletions src/transforms/v2-to-v3/apis/getClientIdentifiers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { ClientIdentifier } from "../types";
import { getClientIdNamesFromNewExpr } from "./getClientIdNamesFromNewExpr";
import { getClientIdNamesFromTSTypeRef } from "./getClientIdNamesFromTSTypeRef";
import { getClientIdThisExpressions, ThisMemberExpression } from "./getClientIdThisExpressions";
import { getClientIdThisExpressions } from "./getClientIdThisExpressions";

export interface GetClientIdentifiersOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}

export type ClientIdentifier = Identifier | ThisMemberExpression;

export const getClientIdentifiers = (
j: JSCodeshift,
source: Collection<unknown>,
Expand Down
21 changes: 21 additions & 0 deletions src/transforms/v2-to-v3/apis/getClientIdentifiersRecord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { ClientIdentifiersRecord } from "../types";
import { getClientIdentifiers } from "./getClientIdentifiers";

export interface GetClientIdentifiersRecordOptions {
v2GlobalName?: string;
v2ClientNamesRecord: Record<string, string>;
}

export const getClientIdentifiersRecord = (
j: JSCodeshift,
source: Collection<unknown>,
{ v2GlobalName, v2ClientNamesRecord }: GetClientIdentifiersRecordOptions
): ClientIdentifiersRecord =>
Object.fromEntries(
Object.entries(v2ClientNamesRecord).map(([v2ClientName, v2ClientLocalName]) => [
v2ClientName,
getClientIdentifiers(j, source, { v2ClientName, v2ClientLocalName, v2GlobalName }),
])
);
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CallExpression } from "jscodeshift";

import { ClientIdentifier } from "./getClientIdentifiers";
import { ClientIdentifier } from "../types";

export const getClientWaiterCallExpression = (
clientId: ClientIdentifier,
Expand Down
13 changes: 2 additions & 11 deletions src/transforms/v2-to-v3/apis/getClientWaiterStates.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getClientIdentifiers } from "./getClientIdentifiers";

export interface GetClientWaiterStatesOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}
import { ClientIdentifier } from "../types";

export const getClientWaiterStates = (
j: JSCodeshift,
source: Collection<unknown>,
options: GetClientWaiterStatesOptions
clientIdentifiers: ClientIdentifier[]
): Set<string> => {
const waiterStates: string[] = [];

const clientIdentifiers = getClientIdentifiers(j, source, options);

for (const clientId of clientIdentifiers) {
source
.find(j.CallExpression, {
Expand Down
13 changes: 2 additions & 11 deletions src/transforms/v2-to-v3/apis/getS3SignedUrlApiNames.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import { Collection, JSCodeshift, Literal } from "jscodeshift";

import { getClientIdentifiers } from "./getClientIdentifiers";

export interface GetS3SignedUrlApiNameOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}
import { ClientIdentifier } from "../types";

export const getS3SignedUrlApiNames = (
j: JSCodeshift,
source: Collection<unknown>,
options: GetS3SignedUrlApiNameOptions
clientIdentifiers: ClientIdentifier[]
): string[] => {
if (options.v2ClientName !== "S3") return [];

const apiNames: Set<string> = new Set();
const clientIdentifiers = getClientIdentifiers(j, source, options);

for (const clientId of clientIdentifiers) {
for (const apiName of ["getSignedUrl", "getSignedUrlPromise"]) {
Expand Down
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./replaceS3GetSignedUrlApi";
export * from "./replaceS3UploadApi";
export * from "./replaceWaiterApi";
export * from "./getCommandName";
export * from "./getClientIdentifiersRecord";
14 changes: 2 additions & 12 deletions src/transforms/v2-to-v3/apis/isS3GetSignedUrlApiUsed.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getClientIdentifiers } from "./getClientIdentifiers";

export interface IsS3GetSignedUrlApiUsedOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}
import { ClientIdentifier } from "../types";

export const isS3GetSignedUrlApiUsed = (
j: JSCodeshift,
source: Collection<unknown>,
options: IsS3GetSignedUrlApiUsedOptions
clientIdentifiers: ClientIdentifier[]
) => {
if (options.v2ClientName !== "S3") return false;

const clientIdentifiers = getClientIdentifiers(j, source, options);

for (const clientId of clientIdentifiers) {
for (const apiName of ["getSignedUrl", "getSignedUrlPromise"]) {
const s3GetSignedUrlCallExpressions = source.find(j.CallExpression, {
Expand Down
14 changes: 2 additions & 12 deletions src/transforms/v2-to-v3/apis/isS3UploadApiUsed.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getClientIdentifiers } from "./getClientIdentifiers";

export interface IsS3UploadApiUsedOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}
import { ClientIdentifier } from "../types";

export const isS3UploadApiUsed = (
j: JSCodeshift,
source: Collection<unknown>,
options: IsS3UploadApiUsedOptions
clientIdentifiers: ClientIdentifier[]
) => {
if (options.v2ClientName !== "S3") return false;

const clientIdentifiers = getClientIdentifiers(j, source, options);

for (const clientId of clientIdentifiers) {
const s3UploadCallExpressions = source.find(j.CallExpression, {
callee: {
Expand Down
12 changes: 2 additions & 10 deletions src/transforms/v2-to-v3/apis/removePromiseCalls.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { getClientIdentifiers } from "./getClientIdentifiers";
import { ClientIdentifier } from "../types";
import { removePromiseForCallExpression } from "./removePromiseForCallExpression";

export interface RemovePromiseCallsOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}

// Removes .promise() from client API calls.
export const removePromiseCalls = (
j: JSCodeshift,
source: Collection<unknown>,
options: RemovePromiseCallsOptions
clientIdentifiers: ClientIdentifier[]
): void => {
const clientIdentifiers = getClientIdentifiers(j, source, options);

for (const clientId of clientIdentifiers) {
// Remove .promise() from client API calls.
source
Expand Down
14 changes: 2 additions & 12 deletions src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,16 @@ import {
} from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { ClientIdentifier } from "../types";
import { getClientApiCallExpression } from "./getClientApiCallExpression";
import { ClientIdentifier, getClientIdentifiers } from "./getClientIdentifiers";
import { getCommandName } from "./getCommandName";

export interface ReplaceS3GetSignedUrlApiOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}

// Updates `s3.getSignedUrl()` API with `await getSignedUrl(s3, command)` API.
export const replaceS3GetSignedUrlApi = (
j: JSCodeshift,
source: Collection<unknown>,
options: ReplaceS3GetSignedUrlApiOptions
clientIdentifiers: ClientIdentifier[]
): void => {
if (options.v2ClientName !== "S3") return;

const clientIdentifiers = getClientIdentifiers(j, source, options);

for (const clientId of clientIdentifiers) {
for (const getSignedUrlApiName of ["getSignedUrl", "getSignedUrlPromise"]) {
source
Expand Down
14 changes: 2 additions & 12 deletions src/transforms/v2-to-v3/apis/replaceS3UploadApi.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { ClientIdentifier } from "../types";
import { getClientApiCallExpression } from "./getClientApiCallExpression";
import { getClientIdentifiers } from "./getClientIdentifiers";

export interface ReplaceS3UploadApiOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}

// Updates `s3.upload()` API with `new Upload()` API.
export const replaceS3UploadApi = (
j: JSCodeshift,
source: Collection<unknown>,
options: ReplaceS3UploadApiOptions
clientIdentifiers: ClientIdentifier[]
): void => {
if (options.v2ClientName !== "S3") return;

const clientIdentifiers = getClientIdentifiers(j, source, options);

for (const clientId of clientIdentifiers) {
source
.find(j.CallExpression, getClientApiCallExpression(clientId, "upload"))
Expand Down
14 changes: 3 additions & 11 deletions src/transforms/v2-to-v3/apis/replaceWaiterApi.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { ClientIdentifier } from "../types";
import { getArgsWithoutWaiterConfig } from "./getArgsWithoutWaiterConfig";
import { getClientIdentifiers } from "./getClientIdentifiers";
import { getClientWaiterCallExpression } from "./getClientWaiterCallExpression";
import { getClientWaiterStates } from "./getClientWaiterStates";
import { getV3ClientWaiterApiName } from "./getV3ClientWaiterApiName";
import { getWaiterConfig } from "./getWaiterConfig";
import { getWaiterConfigValue } from "./getWaiterConfigValue";

export interface ReplaceWaiterApiOptions {
v2ClientName: string;
v2ClientLocalName: string;
v2GlobalName?: string;
}

// Updates .waitFor() API with waitUntil* API.
export const replaceWaiterApi = (
j: JSCodeshift,
source: Collection<unknown>,
options: ReplaceWaiterApiOptions
clientIdentifiers: ClientIdentifier[]
): void => {
const clientIdentifiers = getClientIdentifiers(j, source, options);

for (const clientId of clientIdentifiers) {
const waiterStates = getClientWaiterStates(j, source, options);
const waiterStates = getClientWaiterStates(j, source, clientIdentifiers);

for (const waiterState of waiterStates) {
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
Expand Down
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/config/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const PACKAGE_NAME = "aws-sdk";

export const S3 = "S3";
export const DYNAMODB = "DynamoDB";
export const DOCUMENT_CLIENT = "DocumentClient";
export const DYNAMODB_DOCUMENT = "DynamoDBDocument";
Expand Down
Loading

0 comments on commit 1431238

Please sign in to comment.