Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add comment to investigate removal of .promise() #859

Merged
merged 5 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 "./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