Skip to content

Commit

Permalink
Add comments for unsupported waiters with callback (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Mar 1, 2023
1 parent 445e95e commit adf104f
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/olive-tomatoes-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add comments for unsupported waiters with callback
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import AWS from "aws-sdk";

const Bucket = "BUCKET_NAME";
const client = new AWS.S3({ region: "REGION" });

client.waitFor("bucketExists", { Bucket }, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
11 changes: 11 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/waiters/callback.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AWS from "aws-sdk";

const Bucket = "BUCKET_NAME";
const client = new AWS.S3({ region: "REGION" });

// Waiters with callbacks are not supported in AWS SDK for JavaScript (v3).
// Please convert to `await client.waitFor(state, params).promise()`, and re-run aws-sdk-js-codemod.
client.waitFor("bucketExists", { Bucket }, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
47 changes: 47 additions & 0 deletions src/transforms/v2-to-v3/apis/addNotSupportedComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { FUNCTION_TYPE_LIST } from "../config";
import { getClientWaiterStates } from "./getClientWaiterStates";
import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers";
import { getV2ClientWaiterCallExpression } from "./getV2ClientWaiterCallExpression";

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

export const addNotSupportedComments = (
j: JSCodeshift,
source: Collection<unknown>,
options: CommentsForUnsupportedAPIsOptions
): void => {
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options);

for (const v2ClientId of v2ClientIdentifiers) {
const waiterStates = getClientWaiterStates(j, source, options);

for (const waiterState of waiterStates) {
source
.find(j.CallExpression, getV2ClientWaiterCallExpression(v2ClientId, waiterState))
.forEach((callExpression) => {
const args = callExpression.node.arguments;

if (FUNCTION_TYPE_LIST.includes(args[args.length - 1].type)) {
const comments = callExpression.node.comments || [];
comments.push(
j.commentLine(
" Waiters with callbacks are not supported in AWS SDK for JavaScript (v3)."
)
);
comments.push(
j.commentLine(
" Please convert to `await client.waitFor(state, params).promise()`, and re-run aws-sdk-js-codemod."
)
);
callExpression.node.comments = comments;
}
});
}
}
};
17 changes: 17 additions & 0 deletions src/transforms/v2-to-v3/apis/getV2ClientWaiterCallExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { CallExpression } from "jscodeshift";

import { V2ClientIdentifier } from "./getV2ClientIdentifiers";

export const getV2ClientWaiterCallExpression = (
v2ClientId: V2ClientIdentifier,
waiterState: string
): CallExpression => ({
type: "CallExpression",
callee: {
type: "MemberExpression",
object: v2ClientId,
property: { type: "Identifier", name: "waitFor" },
},
// @ts-expect-error Type 'string' is not assignable to type 'RegExp'
arguments: [{ value: waiterState }],
});
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./addNotSupportedComments";
export * from "./getClientWaiterStates";
export * from "./getV3ClientWaiterApiName";
export * from "./isS3UploadApiUsed";
Expand Down
11 changes: 2 additions & 9 deletions src/transforms/v2-to-v3/apis/replaceWaiterApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Collection, JSCodeshift } from "jscodeshift";
import { getArgsWithoutWaiterConfig } from "./getArgsWithoutWaiterConfig";
import { getClientWaiterStates } from "./getClientWaiterStates";
import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers";
import { getV2ClientWaiterCallExpression } from "./getV2ClientWaiterCallExpression";
import { getV3ClientWaiterApiName } from "./getV3ClientWaiterApiName";
import { getWaiterConfig } from "./getWaiterConfig";
import { getWaiterConfigValue } from "./getWaiterConfigValue";
Expand All @@ -27,15 +28,7 @@ export const replaceWaiterApi = (
for (const waiterState of waiterStates) {
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
source
.find(j.CallExpression, {
type: "CallExpression",
callee: {
type: "MemberExpression",
object: v2ClientId,
property: { type: "Identifier", name: "waitFor" },
},
arguments: [{ value: waiterState }],
})
.find(j.CallExpression, getV2ClientWaiterCallExpression(v2ClientId, waiterState))
.replaceWith((callExpression) => {
const waiterConfig = getWaiterConfig(callExpression.node.arguments[1]);
const delay = getWaiterConfigValue(waiterConfig, "delay");
Expand Down
5 changes: 5 additions & 0 deletions src/transforms/v2-to-v3/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,8 @@ export const V2_CLIENT_INPUT_SUFFIX_LIST = ["Input", "Request"];
export const V2_CLIENT_OUTPUT_SUFFIX_LIST = ["Output", "Response"];

export const OBJECT_PROPERTY_TYPE_LIST = ["Property", "ObjectProperty"];
export const FUNCTION_TYPE_LIST = [
"FunctionDeclaration",
"FunctionExpression",
"ArrowFunctionExpression",
];
12 changes: 11 additions & 1 deletion src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { API, FileInfo } from "jscodeshift";

import { removePromiseCalls, replaceWaiterApi } from "./apis";
import { addNotSupportedComments, removePromiseCalls, replaceWaiterApi } from "./apis";
import { replaceS3UploadApi } from "./apis/replaceS3UploadApi";
import { replaceClientCreation } from "./client-instances";
import {
Expand Down Expand Up @@ -37,6 +37,16 @@ const transformer = async (file: FileInfo, api: API) => {
}

const clientMetadataRecord = getClientMetadataRecord(v2ClientNamesRecord);

for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
const { v2ClientLocalName } = v3ClientMetadata;
addNotSupportedComments(j, source, { v2ClientName, v2ClientLocalName, v2GlobalName });
}

if (source.toSource() !== file.source) {
return source.toSource();
}

for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
const { v2ClientLocalName, v3ClientName, v3ClientPackageName } = v3ClientMetadata;

Expand Down

0 comments on commit adf104f

Please sign in to comment.