From adb21f56f1941bb50032b7448d753c6f1801c340 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 11 Apr 2024 15:45:17 -0700 Subject: [PATCH 1/5] test: api-promise/orphan-promise --- .../__fixtures__/api-promise/orphan-promise.input.js | 2 ++ .../__fixtures__/api-promise/orphan-promise.output.js | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.output.js diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.input.js b/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.input.js new file mode 100644 index 000000000..3432e5b6a --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.input.js @@ -0,0 +1,2 @@ +// client is AWS SDK JS v2 client here, but jscodeshift can't detect it because of lack of types/import. +export const listTables = (client) => client.listTables().promise(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.output.js b/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.output.js new file mode 100644 index 000000000..8e77f196b --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.output.js @@ -0,0 +1,5 @@ +// client is AWS SDK JS v2 client here, but jscodeshift can't detect it because of lack of types/import. +export const listTables = (client) => + // 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(); \ No newline at end of file From e0fb32588dbee76e38d9f0201d31b1e64e6d86ce Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:25:09 -0700 Subject: [PATCH 2/5] Add comment to investigate removal of .promise() --- .../v2-to-v3/apis/addPromiseRemovalComments.ts | 18 ++++++++++++++++++ src/transforms/v2-to-v3/apis/index.ts | 1 + src/transforms/v2-to-v3/transformer.ts | 3 +++ 3 files changed, 22 insertions(+) create mode 100644 src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts diff --git a/src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts b/src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts new file mode 100644 index 000000000..73744ca83 --- /dev/null +++ b/src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts @@ -0,0 +1,18 @@ +import { Collection, JSCodeshift } from "jscodeshift"; + +export const addPromiseRemovalComments = (j: JSCodeshift, source: Collection): void => { + // Add comment for .promise() calls which weren't removed. + source + .find(j.CallExpression, { + callee: { + type: "MemberExpression", + property: { type: "Identifier", name: "promise" }, + }, + }) + .forEach((callExpression) => { + callExpression.value.comments = [ + ...(callExpression.value.comments || []), + j.commentLine(" This is a comment before the CallExpression"), + ]; + }); +}; diff --git a/src/transforms/v2-to-v3/apis/index.ts b/src/transforms/v2-to-v3/apis/index.ts index 8dc7e504a..d1860ac3b 100644 --- a/src/transforms/v2-to-v3/apis/index.ts +++ b/src/transforms/v2-to-v3/apis/index.ts @@ -5,6 +5,7 @@ export * from "./getClientWaiterStates"; export * from "./getCommandName"; export * from "./getS3SignedUrlApiNames"; export * from "./getV3ClientWaiterApiName"; +export * from "./addPromiseRemovalComments"; export * from "./isS3GetSignedUrlApiUsed"; export * from "./isS3UploadApiUsed"; export * from "./removePromiseCalls"; diff --git a/src/transforms/v2-to-v3/transformer.ts b/src/transforms/v2-to-v3/transformer.ts index 70b012d32..8c5594f92 100644 --- a/src/transforms/v2-to-v3/transformer.ts +++ b/src/transforms/v2-to-v3/transformer.ts @@ -12,6 +12,7 @@ import { replaceAwsError, addEmptyObjectForUndefined, renameErrorCodeWithName, + addPromiseRemovalComments, } from "./apis"; import { replaceAwsUtilFunctions } from "./aws-util"; import { @@ -50,6 +51,7 @@ 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; } @@ -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 })); From 4832b7523cba753a445aaa29e013ba9b0f8f6aae Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:38:45 -0700 Subject: [PATCH 3/5] chore: return source.toSource() from transformer after adding comments --- .../__fixtures__/api-promise/orphan-promise.input.js | 4 +++- .../api-promise/orphan-promise.output.js | 11 +++++++---- .../v2-to-v3/apis/addPromiseRemovalComments.ts | 12 +++++++----- src/transforms/v2-to-v3/transformer.ts | 2 +- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.input.js b/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.input.js index 3432e5b6a..6a72b5110 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.input.js @@ -1,2 +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) => client.listTables().promise(); \ No newline at end of file +export const listTables = (client) => { + return client.listTables().promise(); +} \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.output.js b/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.output.js index 8e77f196b..e3f6c1551 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/orphan-promise.output.js @@ -1,5 +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) => - // 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(); \ No newline at end of file +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() + ); +} \ No newline at end of file diff --git a/src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts b/src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts index 73744ca83..494fabe02 100644 --- a/src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts +++ b/src/transforms/v2-to-v3/apis/addPromiseRemovalComments.ts @@ -9,10 +9,12 @@ export const addPromiseRemovalComments = (j: JSCodeshift, source: Collection { - callExpression.value.comments = [ - ...(callExpression.value.comments || []), - j.commentLine(" This is a comment before the CallExpression"), - ]; + .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; }); }; diff --git a/src/transforms/v2-to-v3/transformer.ts b/src/transforms/v2-to-v3/transformer.ts index 8c5594f92..900ffa06a 100644 --- a/src/transforms/v2-to-v3/transformer.ts +++ b/src/transforms/v2-to-v3/transformer.ts @@ -53,7 +53,7 @@ const transformer = async (file: FileInfo, api: API) => { 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 }); From b989ed4137c37f73c7a8888cff38d9b6a2cc6bd6 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:40:10 -0700 Subject: [PATCH 4/5] chore: yarn changeset --- .changeset/popular-ties-arrive.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/popular-ties-arrive.md diff --git a/.changeset/popular-ties-arrive.md b/.changeset/popular-ties-arrive.md new file mode 100644 index 000000000..cbbea541c --- /dev/null +++ b/.changeset/popular-ties-arrive.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Add comment to investigate removal of .promise() From ea31b5de6488effd1c2c3e4bd7ce8e133260c65f Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Thu, 11 Apr 2024 17:42:46 -0700 Subject: [PATCH 5/5] test: update misc/no-formatting --- .../v2-to-v3/__fixtures__/misc/no-formatting.input.js | 2 +- .../v2-to-v3/__fixtures__/misc/no-formatting.output.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/transforms/v2-to-v3/__fixtures__/misc/no-formatting.input.js b/src/transforms/v2-to-v3/__fixtures__/misc/no-formatting.input.js index 0799d24d9..9f03b0342 100644 --- a/src/transforms/v2-to-v3/__fixtures__/misc/no-formatting.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/misc/no-formatting.input.js @@ -1 +1 @@ -const env = {...process.env, ...this.config.env || {}} \ No newline at end of file +const env = {...process.env, ...(this.config.env || {})} \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/misc/no-formatting.output.js b/src/transforms/v2-to-v3/__fixtures__/misc/no-formatting.output.js index 0799d24d9..9f03b0342 100644 --- a/src/transforms/v2-to-v3/__fixtures__/misc/no-formatting.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/misc/no-formatting.output.js @@ -1 +1 @@ -const env = {...process.env, ...this.config.env || {}} \ No newline at end of file +const env = {...process.env, ...(this.config.env || {})} \ No newline at end of file