From f2185da443468975f867bad55ab89f2a3508b0fd Mon Sep 17 00:00:00 2001 From: Trivikram Kamat <16024985+trivikr@users.noreply.github.com> Date: Thu, 26 Oct 2023 15:15:06 -0700 Subject: [PATCH] Prefer import over require when both are present (#652) --- .changeset/stale-geese-grow.md | 5 +++ .../misc/import-over-require.input.ts | 4 +++ .../misc/import-over-require.output.ts | 3 ++ .../modules/getGlobalNameFromModule.ts | 32 ++++++++----------- .../v2-to-v3/modules/getImportType.ts | 8 ++--- src/transforms/v2-to-v3/modules/hasImport.ts | 11 +++++++ src/transforms/v2-to-v3/modules/hasRequire.ts | 18 ----------- .../v2-to-v3/modules/removeGlobalModule.ts | 18 +++-------- src/transforms/v2-to-v3/transformer.ts | 4 +-- 9 files changed, 47 insertions(+), 56 deletions(-) create mode 100644 .changeset/stale-geese-grow.md create mode 100644 src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.input.ts create mode 100644 src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.output.ts create mode 100644 src/transforms/v2-to-v3/modules/hasImport.ts delete mode 100644 src/transforms/v2-to-v3/modules/hasRequire.ts diff --git a/.changeset/stale-geese-grow.md b/.changeset/stale-geese-grow.md new file mode 100644 index 000000000..849078fe3 --- /dev/null +++ b/.changeset/stale-geese-grow.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Prefer import over require when both are present diff --git a/src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.input.ts b/src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.input.ts new file mode 100644 index 000000000..c6fc54f2c --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.input.ts @@ -0,0 +1,4 @@ +import { DynamoDB } from "aws-sdk"; +const AWS = require("aws-sdk"); + +const client: DynamoDB = new AWS.DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.output.ts b/src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.output.ts new file mode 100644 index 000000000..1e8737829 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.output.ts @@ -0,0 +1,3 @@ +import { DynamoDB } from "@aws-sdk/client-dynamodb"; + +const client: DynamoDB = new DynamoDB(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/modules/getGlobalNameFromModule.ts b/src/transforms/v2-to-v3/modules/getGlobalNameFromModule.ts index 4c53c2671..6bb2b9307 100644 --- a/src/transforms/v2-to-v3/modules/getGlobalNameFromModule.ts +++ b/src/transforms/v2-to-v3/modules/getGlobalNameFromModule.ts @@ -3,28 +3,24 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift"; import { PACKAGE_NAME } from "../config"; import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType"; import { getImportSpecifiers } from "./getImportSpecifiers"; -import { ImportType } from "./types"; export const getGlobalNameFromModule = ( j: JSCodeshift, - source: Collection, - importType: ImportType + source: Collection ): string | undefined => { - if (importType === ImportType.REQUIRE) { - const requireIdentifiers = source - .find(j.VariableDeclarator, { - id: { type: "Identifier" }, - init: { - type: "CallExpression", - callee: { type: "Identifier", name: "require" }, - arguments: [{ value: PACKAGE_NAME }], - }, - }) - .nodes(); - - if (requireIdentifiers.length > 0) { - return (requireIdentifiers[0]?.id as Identifier).name; - } + const requireIdentifiers = source + .find(j.VariableDeclarator, { + id: { type: "Identifier" }, + init: { + type: "CallExpression", + callee: { type: "Identifier", name: "require" }, + arguments: [{ value: PACKAGE_NAME }], + }, + }) + .nodes(); + + if (requireIdentifiers.length > 0) { + return (requireIdentifiers[0]?.id as Identifier).name; } const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter((specifier) => diff --git a/src/transforms/v2-to-v3/modules/getImportType.ts b/src/transforms/v2-to-v3/modules/getImportType.ts index a7c4a1727..005ff336e 100644 --- a/src/transforms/v2-to-v3/modules/getImportType.ts +++ b/src/transforms/v2-to-v3/modules/getImportType.ts @@ -1,11 +1,11 @@ import { Collection, JSCodeshift } from "jscodeshift"; +import { hasImport } from "./hasImport"; import { hasImportEquals } from "./hasImportEquals"; -import { hasRequire } from "./hasRequire"; import { ImportType } from "./types"; export const getImportType = (j: JSCodeshift, source: Collection) => - hasRequire(j, source) - ? ImportType.REQUIRE + hasImport(j, source) + ? ImportType.IMPORT : hasImportEquals(j, source) ? ImportType.IMPORT_EQUALS - : ImportType.IMPORT; + : ImportType.REQUIRE; diff --git a/src/transforms/v2-to-v3/modules/hasImport.ts b/src/transforms/v2-to-v3/modules/hasImport.ts new file mode 100644 index 000000000..185cfbb35 --- /dev/null +++ b/src/transforms/v2-to-v3/modules/hasImport.ts @@ -0,0 +1,11 @@ +import { Collection, JSCodeshift } from "jscodeshift"; +import { PACKAGE_NAME } from "../config"; + +export const hasImport = (j: JSCodeshift, source: Collection) => + source + .find(j.ImportDeclaration) + .filter((importDeclaration) => { + const { value: sourceValue } = importDeclaration.value.source; + return typeof sourceValue === "string" && sourceValue.startsWith(PACKAGE_NAME); + }) + .size() > 0; diff --git a/src/transforms/v2-to-v3/modules/hasRequire.ts b/src/transforms/v2-to-v3/modules/hasRequire.ts deleted file mode 100644 index 624832c7f..000000000 --- a/src/transforms/v2-to-v3/modules/hasRequire.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Collection, JSCodeshift, Literal } from "jscodeshift"; -import { PACKAGE_NAME, STRING_LITERAL_TYPE_LIST } from "../config"; - -export const hasRequire = (j: JSCodeshift, source: Collection) => - source - .find(j.CallExpression, { - callee: { type: "Identifier", name: "require" }, - }) - .filter((callExpression) => { - const { arguments: args } = callExpression.value; - - if (args.length === 0) return false; - if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return false; - - const value = (args[0] as Literal).value; - return typeof value === "string" && value.startsWith(PACKAGE_NAME); - }) - .size() > 0; diff --git a/src/transforms/v2-to-v3/modules/removeGlobalModule.ts b/src/transforms/v2-to-v3/modules/removeGlobalModule.ts index e9848284e..c2758eccf 100644 --- a/src/transforms/v2-to-v3/modules/removeGlobalModule.ts +++ b/src/transforms/v2-to-v3/modules/removeGlobalModule.ts @@ -4,18 +4,12 @@ import { PACKAGE_NAME } from "../config"; import { removeImportDefault } from "./removeImportDefault"; import { removeImportEquals } from "./removeImportEquals"; import { removeRequireIdentifier } from "./removeRequireIdentifier"; -import { ImportType } from "./types"; - -export interface RemoveGlobalModuleOptions { - importType: ImportType; - v2GlobalName?: string; -} // Removes the import of "aws-sdk" if it's not used. export const removeGlobalModule = ( j: JSCodeshift, source: Collection, - { importType, v2GlobalName }: RemoveGlobalModuleOptions + v2GlobalName?: string ) => { if (!v2GlobalName) return; @@ -24,12 +18,8 @@ export const removeGlobalModule = ( // Only usage is import/require. if (identifierUsages.size() === 1) { const defaultOptions = { localName: v2GlobalName, sourceValue: PACKAGE_NAME }; - if (importType === ImportType.REQUIRE) { - removeRequireIdentifier(j, source, defaultOptions); - } else if (importType === ImportType.IMPORT_EQUALS) { - removeImportEquals(j, source, defaultOptions); - } else { - removeImportDefault(j, source, defaultOptions); - } + removeRequireIdentifier(j, source, defaultOptions); + removeImportEquals(j, source, defaultOptions); + removeImportDefault(j, source, defaultOptions); } }; diff --git a/src/transforms/v2-to-v3/transformer.ts b/src/transforms/v2-to-v3/transformer.ts index c9994b89d..988d577ad 100644 --- a/src/transforms/v2-to-v3/transformer.ts +++ b/src/transforms/v2-to-v3/transformer.ts @@ -39,7 +39,7 @@ const transformer = async (file: FileInfo, api: API) => { addNotSupportedComments(j, source, importType); - const v2GlobalName = getGlobalNameFromModule(j, source, importType); + const v2GlobalName = getGlobalNameFromModule(j, source); const v2ClientNamesRecord = getClientNamesRecord(j, source, importType); if (!v2GlobalName && Object.keys(v2ClientNamesRecord).length === 0) { @@ -99,7 +99,7 @@ const transformer = async (file: FileInfo, api: API) => { replaceAwsConfig(j, source, v2GlobalName); replaceAwsIdentity(j, source, { v2GlobalName, importType }); replaceAwsUtilFunctions(j, source, v2GlobalName); - removeGlobalModule(j, source, { v2GlobalName, importType }); + removeGlobalModule(j, source, v2GlobalName); return source.toSource(); };