Skip to content

Commit adf104f

Browse files
authored
Add comments for unsupported waiters with callback (#434)
1 parent 445e95e commit adf104f

File tree

9 files changed

+108
-10
lines changed

9 files changed

+108
-10
lines changed

.changeset/olive-tomatoes-enjoy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Add comments for unsupported waiters with callback
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import AWS from "aws-sdk";
2+
3+
const Bucket = "BUCKET_NAME";
4+
const client = new AWS.S3({ region: "REGION" });
5+
6+
client.waitFor("bucketExists", { Bucket }, function (err, data) {
7+
if (err) console.log(err, err.stack); // an error occurred
8+
else console.log(data); // successful response
9+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import AWS from "aws-sdk";
2+
3+
const Bucket = "BUCKET_NAME";
4+
const client = new AWS.S3({ region: "REGION" });
5+
6+
// Waiters with callbacks are not supported in AWS SDK for JavaScript (v3).
7+
// Please convert to `await client.waitFor(state, params).promise()`, and re-run aws-sdk-js-codemod.
8+
client.waitFor("bucketExists", { Bucket }, function (err, data) {
9+
if (err) console.log(err, err.stack); // an error occurred
10+
else console.log(data); // successful response
11+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { FUNCTION_TYPE_LIST } from "../config";
4+
import { getClientWaiterStates } from "./getClientWaiterStates";
5+
import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers";
6+
import { getV2ClientWaiterCallExpression } from "./getV2ClientWaiterCallExpression";
7+
8+
export interface CommentsForUnsupportedAPIsOptions {
9+
v2ClientName: string;
10+
v2ClientLocalName: string;
11+
v2GlobalName?: string;
12+
}
13+
14+
export const addNotSupportedComments = (
15+
j: JSCodeshift,
16+
source: Collection<unknown>,
17+
options: CommentsForUnsupportedAPIsOptions
18+
): void => {
19+
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options);
20+
21+
for (const v2ClientId of v2ClientIdentifiers) {
22+
const waiterStates = getClientWaiterStates(j, source, options);
23+
24+
for (const waiterState of waiterStates) {
25+
source
26+
.find(j.CallExpression, getV2ClientWaiterCallExpression(v2ClientId, waiterState))
27+
.forEach((callExpression) => {
28+
const args = callExpression.node.arguments;
29+
30+
if (FUNCTION_TYPE_LIST.includes(args[args.length - 1].type)) {
31+
const comments = callExpression.node.comments || [];
32+
comments.push(
33+
j.commentLine(
34+
" Waiters with callbacks are not supported in AWS SDK for JavaScript (v3)."
35+
)
36+
);
37+
comments.push(
38+
j.commentLine(
39+
" Please convert to `await client.waitFor(state, params).promise()`, and re-run aws-sdk-js-codemod."
40+
)
41+
);
42+
callExpression.node.comments = comments;
43+
}
44+
});
45+
}
46+
}
47+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { CallExpression } from "jscodeshift";
2+
3+
import { V2ClientIdentifier } from "./getV2ClientIdentifiers";
4+
5+
export const getV2ClientWaiterCallExpression = (
6+
v2ClientId: V2ClientIdentifier,
7+
waiterState: string
8+
): CallExpression => ({
9+
type: "CallExpression",
10+
callee: {
11+
type: "MemberExpression",
12+
object: v2ClientId,
13+
property: { type: "Identifier", name: "waitFor" },
14+
},
15+
// @ts-expect-error Type 'string' is not assignable to type 'RegExp'
16+
arguments: [{ value: waiterState }],
17+
});

src/transforms/v2-to-v3/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from "./addNotSupportedComments";
12
export * from "./getClientWaiterStates";
23
export * from "./getV3ClientWaiterApiName";
34
export * from "./isS3UploadApiUsed";

src/transforms/v2-to-v3/apis/replaceWaiterApi.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Collection, JSCodeshift } from "jscodeshift";
33
import { getArgsWithoutWaiterConfig } from "./getArgsWithoutWaiterConfig";
44
import { getClientWaiterStates } from "./getClientWaiterStates";
55
import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers";
6+
import { getV2ClientWaiterCallExpression } from "./getV2ClientWaiterCallExpression";
67
import { getV3ClientWaiterApiName } from "./getV3ClientWaiterApiName";
78
import { getWaiterConfig } from "./getWaiterConfig";
89
import { getWaiterConfigValue } from "./getWaiterConfigValue";
@@ -27,15 +28,7 @@ export const replaceWaiterApi = (
2728
for (const waiterState of waiterStates) {
2829
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState);
2930
source
30-
.find(j.CallExpression, {
31-
type: "CallExpression",
32-
callee: {
33-
type: "MemberExpression",
34-
object: v2ClientId,
35-
property: { type: "Identifier", name: "waitFor" },
36-
},
37-
arguments: [{ value: waiterState }],
38-
})
31+
.find(j.CallExpression, getV2ClientWaiterCallExpression(v2ClientId, waiterState))
3932
.replaceWith((callExpression) => {
4033
const waiterConfig = getWaiterConfig(callExpression.node.arguments[1]);
4134
const delay = getWaiterConfigValue(waiterConfig, "delay");

src/transforms/v2-to-v3/config/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,8 @@ export const V2_CLIENT_INPUT_SUFFIX_LIST = ["Input", "Request"];
44
export const V2_CLIENT_OUTPUT_SUFFIX_LIST = ["Output", "Response"];
55

66
export const OBJECT_PROPERTY_TYPE_LIST = ["Property", "ObjectProperty"];
7+
export const FUNCTION_TYPE_LIST = [
8+
"FunctionDeclaration",
9+
"FunctionExpression",
10+
"ArrowFunctionExpression",
11+
];

src/transforms/v2-to-v3/transformer.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { API, FileInfo } from "jscodeshift";
22

3-
import { removePromiseCalls, replaceWaiterApi } from "./apis";
3+
import { addNotSupportedComments, removePromiseCalls, replaceWaiterApi } from "./apis";
44
import { replaceS3UploadApi } from "./apis/replaceS3UploadApi";
55
import { replaceClientCreation } from "./client-instances";
66
import {
@@ -37,6 +37,16 @@ const transformer = async (file: FileInfo, api: API) => {
3737
}
3838

3939
const clientMetadataRecord = getClientMetadataRecord(v2ClientNamesRecord);
40+
41+
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
42+
const { v2ClientLocalName } = v3ClientMetadata;
43+
addNotSupportedComments(j, source, { v2ClientName, v2ClientLocalName, v2GlobalName });
44+
}
45+
46+
if (source.toSource() !== file.source) {
47+
return source.toSource();
48+
}
49+
4050
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
4151
const { v2ClientLocalName, v3ClientName, v3ClientPackageName } = v3ClientMetadata;
4252

0 commit comments

Comments
 (0)