Skip to content

Commit

Permalink
Add comment to investigate removal of .promise() (#859)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Apr 12, 2024
1 parent 13dc42a commit 06a91b7
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/popular-ties-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add comment to investigate removal of .promise()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// client is AWS SDK JS v2 client here, but jscodeshift can't detect it because of lack of types/import.
export const listTables = (client) => {
return client.listTables().promise();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// client is AWS SDK JS v2 client here, but jscodeshift can't detect it because of lack of types/import.
export const listTables = (client) => {
return (
// The `.promise()` call might be on an JS SDK v2 client API.
// If yes, please remove .promise(). If not, remove this comment.
client.listTables().promise()
);
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const env = {...process.env, ...this.config.env || {}}
const env = {...process.env, ...(this.config.env || {})}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
const env = {...process.env, ...this.config.env || {}}
const env = {...process.env, ...(this.config.env || {})}
20 changes: 20 additions & 0 deletions src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Collection, JSCodeshift } from "jscodeshift";

export const addPromiseRemovalComments = (j: JSCodeshift, source: Collection<unknown>): void => {
// Add comment for .promise() calls which weren't removed.
source
.find(j.CallExpression, {
callee: {
type: "MemberExpression",
property: { type: "Identifier", name: "promise" },
},
})
.forEach(({ node }) => {
const comments = node.comments || [];
comments.push(j.commentLine(" The `.promise()` call might be on an JS SDK v2 client API."));
comments.push(
j.commentLine(" If yes, please remove .promise(). If not, remove this comment.")
);
node.comments = comments;
});
};
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 @@ -5,6 +5,7 @@ export * from "./getClientWaiterStates";
export * from "./getCommandName";
export * from "./getS3SignedUrlApiNames";
export * from "./getV3ClientWaiterApiName";
export * from "./addPromiseRemovalComments";

Check warning on line 8 in src/transforms/v2-to-v3/apis/index.ts

View workflow job for this annotation

GitHub Actions / call-build / build (18.x)

"export * from './addPromiseRemovalComments'" should occur before "export * from './getV3ClientWaiterApiName'"

Check warning on line 8 in src/transforms/v2-to-v3/apis/index.ts

View workflow job for this annotation

GitHub Actions / call-build / build (20.x)

"export * from './addPromiseRemovalComments'" should occur before "export * from './getV3ClientWaiterApiName'"
export * from "./isS3GetSignedUrlApiUsed";
export * from "./isS3UploadApiUsed";
export * from "./removePromiseCalls";
Expand Down
5 changes: 4 additions & 1 deletion src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
replaceAwsError,
addEmptyObjectForUndefined,
renameErrorCodeWithName,
addPromiseRemovalComments,
} from "./apis";
import { replaceAwsUtilFunctions } from "./aws-util";
import {
Expand Down Expand Up @@ -50,8 +51,9 @@ const transformer = async (file: FileInfo, api: API) => {
const importType = getImportType(j, source);

if (importType === null) {
addPromiseRemovalComments(j, source);
// Skip transformation, since no import/require statements found for "aws-sdk" package.
return file.source;
return source.toSource();
}

replaceDeepImport(j, source, { fromPath: "aws-sdk/global", toPath: PACKAGE_NAME });
Expand Down Expand Up @@ -126,6 +128,7 @@ const transformer = async (file: FileInfo, api: API) => {
replaceAwsError(j, source, { v2GlobalName, importType });
replaceAwsEndpoint(j, source, v2GlobalName);
removeModules(j, source, importType);
addPromiseRemovalComments(j, source);

const sourceString = getFormattedSourceString(source.toSource({ quote, useTabs, trailingComma }));

Expand Down

0 comments on commit 06a91b7

Please sign in to comment.