From f4d249796803b2be0e715024c0dd2076a02d6e41 Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Wed, 26 Jul 2023 15:04:01 -0700 Subject: [PATCH] Preserve import comments when transforming code (#514) --- .changeset/dirty-owls-turn.md | 5 +++++ .../global-import-equals.block.input.ts | 6 +++++ .../global-import-equals.block.output.ts | 10 +++++++++ .../global-import-equals.inline.input.ts | 4 ++++ .../global-import-equals.inline.output.ts | 8 +++++++ .../global-import.block.input.js | 6 +++++ .../global-import.block.output.js | 6 +++++ .../global-import.inline.input.js | 4 ++++ .../global-import.inline.output.js | 4 ++++ .../global-require.block.input.js | 6 +++++ .../global-require.block.output.js | 8 +++++++ .../global-require.inline.input.js | 4 ++++ .../global-require.inline.output.js | 6 +++++ .../service-import.block.input.js | 6 +++++ .../service-import.block.output.js | 6 +++++ .../service-import.inline.input.js | 4 ++++ .../service-import.inline.output.js | 4 ++++ .../service-require.block.input.js | 6 +++++ .../service-require.block.output.js | 8 +++++++ .../service-require.inline.input.js | 4 ++++ .../service-require.inline.output.js | 6 +++++ .../modules/removeImportDeclaration.ts | 19 ++++++++++++++++ .../v2-to-v3/modules/removeImportDefault.ts | 3 ++- .../v2-to-v3/modules/removeImportEquals.ts | 22 ++++++++++--------- .../v2-to-v3/modules/removeImportNamed.ts | 3 ++- 25 files changed, 156 insertions(+), 12 deletions(-) create mode 100644 .changeset/dirty-owls-turn.md create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.input.ts create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.output.ts create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.input.ts create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.output.ts create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.output.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.output.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.output.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.output.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.output.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.output.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.output.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.output.js create mode 100644 src/transforms/v2-to-v3/modules/removeImportDeclaration.ts diff --git a/.changeset/dirty-owls-turn.md b/.changeset/dirty-owls-turn.md new file mode 100644 index 000000000..b1a04d071 --- /dev/null +++ b/.changeset/dirty-owls-turn.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Preserve import comments when transforming code diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.input.ts b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.input.ts new file mode 100644 index 000000000..03c83fac7 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.input.ts @@ -0,0 +1,6 @@ +/* + * Example comment which should not be removed. + */ +import AWS = require("aws-sdk"); + +const client = new AWS.DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.output.ts b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.output.ts new file mode 100644 index 000000000..fdbb1c09a --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.block.output.ts @@ -0,0 +1,10 @@ +/* + * Example comment which should not be removed. + */ +import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); + +const { + DynamoDB +} = AWS_DynamoDB; + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.input.ts b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.input.ts new file mode 100644 index 000000000..320e5436d --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.input.ts @@ -0,0 +1,4 @@ +// Example comment which should not be removed. +import AWS = require("aws-sdk"); + +const client = new AWS.DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.output.ts b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.output.ts new file mode 100644 index 000000000..4f7f2b243 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import-equals.inline.output.ts @@ -0,0 +1,8 @@ +// Example comment which should not be removed. +import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); + +const { + DynamoDB +} = AWS_DynamoDB; + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.input.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.input.js new file mode 100644 index 000000000..78482dd92 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.input.js @@ -0,0 +1,6 @@ +/* + * Example comment which should not be removed. + */ +import AWS from "aws-sdk"; + +const client = new AWS.DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.output.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.output.js new file mode 100644 index 000000000..203ccb7f1 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.block.output.js @@ -0,0 +1,6 @@ +/* + * Example comment which should not be removed. + */ +import { DynamoDB } from "@aws-sdk/client-dynamodb"; + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.input.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.input.js new file mode 100644 index 000000000..7833d886f --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.input.js @@ -0,0 +1,4 @@ +// Example comment which should not be removed. +import AWS from "aws-sdk"; + +const client = new AWS.DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.output.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.output.js new file mode 100644 index 000000000..593906ff5 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-import.inline.output.js @@ -0,0 +1,4 @@ +// Example comment which should not be removed. +import { DynamoDB } from "@aws-sdk/client-dynamodb"; + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.input.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.input.js new file mode 100644 index 000000000..1e0dd2509 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.input.js @@ -0,0 +1,6 @@ +/* + * Example comment which should not be removed. + */ +const AWS = require("aws-sdk"); + +const client = new AWS.DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.output.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.output.js new file mode 100644 index 000000000..29fadad6e --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.block.output.js @@ -0,0 +1,8 @@ +/* + * Example comment which should not be removed. + */ +const { + DynamoDB +} = require("@aws-sdk/client-dynamodb"); + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.input.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.input.js new file mode 100644 index 000000000..16c903473 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.input.js @@ -0,0 +1,4 @@ +// Example comment which should not be removed. +const AWS = require("aws-sdk"); + +const client = new AWS.DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.output.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.output.js new file mode 100644 index 000000000..806cb0219 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/global-require.inline.output.js @@ -0,0 +1,6 @@ +// Example comment which should not be removed. +const { + DynamoDB +} = require("@aws-sdk/client-dynamodb"); + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.input.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.input.js new file mode 100644 index 000000000..16fc25f69 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.input.js @@ -0,0 +1,6 @@ +/* + * Example comment which should not be removed. + */ +import { DynamoDB } from "aws-sdk"; + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.output.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.output.js new file mode 100644 index 000000000..203ccb7f1 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.block.output.js @@ -0,0 +1,6 @@ +/* + * Example comment which should not be removed. + */ +import { DynamoDB } from "@aws-sdk/client-dynamodb"; + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.input.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.input.js new file mode 100644 index 000000000..7dd3ccf1c --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.input.js @@ -0,0 +1,4 @@ +// Example comment which should not be removed. +import { DynamoDB } from "aws-sdk"; + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.output.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.output.js new file mode 100644 index 000000000..593906ff5 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-import.inline.output.js @@ -0,0 +1,4 @@ +// Example comment which should not be removed. +import { DynamoDB } from "@aws-sdk/client-dynamodb"; + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.input.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.input.js new file mode 100644 index 000000000..36e12a4a8 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.input.js @@ -0,0 +1,6 @@ +/* + * Example comment which should not be removed. + */ +const { DynamoDB } = require("aws-sdk"); + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.output.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.output.js new file mode 100644 index 000000000..29fadad6e --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.block.output.js @@ -0,0 +1,8 @@ +/* + * Example comment which should not be removed. + */ +const { + DynamoDB +} = require("@aws-sdk/client-dynamodb"); + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.input.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.input.js new file mode 100644 index 000000000..77f400de6 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.input.js @@ -0,0 +1,4 @@ +// Example comment which should not be removed. +const { DynamoDB } = require("aws-sdk"); + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.output.js b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.output.js new file mode 100644 index 000000000..806cb0219 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/import-comments/service-require.inline.output.js @@ -0,0 +1,6 @@ +// Example comment which should not be removed. +const { + DynamoDB +} = require("@aws-sdk/client-dynamodb"); + +const client = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/modules/removeImportDeclaration.ts b/src/transforms/v2-to-v3/modules/removeImportDeclaration.ts new file mode 100644 index 000000000..67cb1d1e7 --- /dev/null +++ b/src/transforms/v2-to-v3/modules/removeImportDeclaration.ts @@ -0,0 +1,19 @@ +import { ASTPath, ImportDeclaration, JSCodeshift } from "jscodeshift"; + +/** + * Removes import declaration, but preserves comments by adding them to next sibling. + */ +export const removeImportDeclaration = ( + j: JSCodeshift, + declarationPath: ASTPath +) => { + const { comments } = declarationPath.value; + if (comments?.length) { + const siblings = declarationPath.parent?.value.body; + if (siblings?.length) { + const nextSibling = siblings[siblings.indexOf(declarationPath.value) + 1]; + nextSibling.comments = [...comments, ...(nextSibling.comments || [])]; + } + } + j(declarationPath).remove(); +}; diff --git a/src/transforms/v2-to-v3/modules/removeImportDefault.ts b/src/transforms/v2-to-v3/modules/removeImportDefault.ts index aeff68a47..485c1acb0 100644 --- a/src/transforms/v2-to-v3/modules/removeImportDefault.ts +++ b/src/transforms/v2-to-v3/modules/removeImportDefault.ts @@ -1,4 +1,5 @@ import { Collection, JSCodeshift } from "jscodeshift"; +import { removeImportDeclaration } from "./removeImportDeclaration"; export interface RemoveImportDefaultOptions { localName: string; @@ -26,7 +27,7 @@ export const removeImportDefault = ( // Remove ImportDeclaration if there are no import specifiers. if (declarationPath.value.specifiers?.length === 0) { - j(declarationPath).remove(); + removeImportDeclaration(j, declarationPath); } }); }; diff --git a/src/transforms/v2-to-v3/modules/removeImportEquals.ts b/src/transforms/v2-to-v3/modules/removeImportEquals.ts index 3786587e2..444616779 100644 --- a/src/transforms/v2-to-v3/modules/removeImportEquals.ts +++ b/src/transforms/v2-to-v3/modules/removeImportEquals.ts @@ -1,4 +1,5 @@ import { Collection, JSCodeshift } from "jscodeshift"; +import { removeImportDeclaration } from "./removeImportDeclaration"; export interface RemoveImportEqualsOptions { localName: string; @@ -10,14 +11,15 @@ export const removeImportEquals = ( source: Collection, { localName, sourceValue }: RemoveImportEqualsOptions ) => { - source - .find(j.TSImportEqualsDeclaration, { - type: "TSImportEqualsDeclaration", - id: { name: localName }, - moduleReference: { - type: "TSExternalModuleReference", - expression: { type: "StringLiteral", value: sourceValue }, - }, - }) - .remove(); + const importEqualsDeclaration = source.find(j.TSImportEqualsDeclaration, { + type: "TSImportEqualsDeclaration", + id: { name: localName }, + moduleReference: { + type: "TSExternalModuleReference", + expression: { type: "StringLiteral", value: sourceValue }, + }, + }); + if (importEqualsDeclaration.length) { + removeImportDeclaration(j, importEqualsDeclaration.get()); + } }; diff --git a/src/transforms/v2-to-v3/modules/removeImportNamed.ts b/src/transforms/v2-to-v3/modules/removeImportNamed.ts index 347d7ea41..a2b7e1c27 100644 --- a/src/transforms/v2-to-v3/modules/removeImportNamed.ts +++ b/src/transforms/v2-to-v3/modules/removeImportNamed.ts @@ -1,4 +1,5 @@ import { Collection, JSCodeshift } from "jscodeshift"; +import { removeImportDeclaration } from "./removeImportDeclaration"; export interface RemoveImportNamedOptions { importedName?: string; @@ -36,7 +37,7 @@ export const removeImportNamed = ( // Remove ImportDeclaration if there are no import specifiers. if (declarationPath.value.specifiers?.length === 0) { - j(declarationPath).remove(); + removeImportDeclaration(j, declarationPath); } }); };