diff --git a/.changeset/strong-coats-flow.md b/.changeset/strong-coats-flow.md new file mode 100644 index 000000000..135c70ad1 --- /dev/null +++ b/.changeset/strong-coats-flow.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Check only for client constructors in named imports diff --git a/src/transforms/v2-to-v3/__fixtures__/unsupported-type-import.input.ts b/src/transforms/v2-to-v3/__fixtures__/unsupported-type-import.input.ts new file mode 100644 index 000000000..99199e845 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/unsupported-type-import.input.ts @@ -0,0 +1,3 @@ +// ToDo: Update when transformation is added. +// Current test verifies error is not thrown. +import { ListTablesInputLimit } from "aws-sdk/clients/dynamodb"; \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/unsupported-type-import.output.ts b/src/transforms/v2-to-v3/__fixtures__/unsupported-type-import.output.ts new file mode 100644 index 000000000..99199e845 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/unsupported-type-import.output.ts @@ -0,0 +1,3 @@ +// ToDo: Update when transformation is added. +// Current test verifies error is not thrown. +import { ListTablesInputLimit } from "aws-sdk/clients/dynamodb"; \ No newline at end of file diff --git a/src/transforms/v2-to-v3/utils/get/getImportIdentifierName.ts b/src/transforms/v2-to-v3/utils/get/getImportSpecifiers.ts similarity index 65% rename from src/transforms/v2-to-v3/utils/get/getImportIdentifierName.ts rename to src/transforms/v2-to-v3/utils/get/getImportSpecifiers.ts index 1aa58edbe..90ae6cb0d 100644 --- a/src/transforms/v2-to-v3/utils/get/getImportIdentifierName.ts +++ b/src/transforms/v2-to-v3/utils/get/getImportSpecifiers.ts @@ -1,12 +1,12 @@ import { Collection, JSCodeshift } from "jscodeshift"; -export const getImportIdentifierName = ( +export const getImportSpecifiers = ( j: JSCodeshift, source: Collection, literalValue: string -): string | undefined => +) => source .find(j.ImportDeclaration, { source: { value: literalValue }, }) - .nodes()[0]?.specifiers?.[0]?.local?.name; + .nodes()[0]?.specifiers; diff --git a/src/transforms/v2-to-v3/utils/get/getV2DefaultModuleName.ts b/src/transforms/v2-to-v3/utils/get/getV2DefaultModuleName.ts index 8732e8b27..92b5a22d9 100644 --- a/src/transforms/v2-to-v3/utils/get/getV2DefaultModuleName.ts +++ b/src/transforms/v2-to-v3/utils/get/getV2DefaultModuleName.ts @@ -2,13 +2,23 @@ import { Collection, JSCodeshift } from "jscodeshift"; import { PACKAGE_NAME } from "../config"; import { containsRequire } from "../containsRequire"; -import { getImportIdentifierName } from "./getImportIdentifierName"; +import { getImportSpecifiers } from "./getImportSpecifiers"; import { getRequireIdentifierName } from "./getRequireIdentifierName"; export const getV2DefaultModuleName = ( j: JSCodeshift, source: Collection -): string | undefined => - containsRequire(j, source) - ? getRequireIdentifierName(j, source, PACKAGE_NAME) - : getImportIdentifierName(j, source, PACKAGE_NAME); +): string | undefined => { + if (containsRequire(j, source)) { + return getRequireIdentifierName(j, source, PACKAGE_NAME); + } + + const importSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME); + if (importSpecifiers && importSpecifiers.length === 1) { + if (["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(importSpecifiers[0].type)) { + return importSpecifiers[0].local?.name; + } + } + + return undefined; +}; diff --git a/src/transforms/v2-to-v3/utils/get/getV2ServiceModuleNames.ts b/src/transforms/v2-to-v3/utils/get/getV2ServiceModuleNames.ts index 0869705da..1ae3a683f 100644 --- a/src/transforms/v2-to-v3/utils/get/getV2ServiceModuleNames.ts +++ b/src/transforms/v2-to-v3/utils/get/getV2ServiceModuleNames.ts @@ -2,13 +2,25 @@ import { Collection, JSCodeshift } from "jscodeshift"; import { CLIENT_NAMES } from "../config"; import { containsRequire } from "../containsRequire"; -import { getImportIdentifierName } from "./getImportIdentifierName"; +import { getImportSpecifiers } from "./getImportSpecifiers"; import { getRequireIdentifierName } from "./getRequireIdentifierName"; import { getV2ServiceModulePath } from "./getV2ServiceModulePath"; -export const getV2ServiceModuleNames = (j: JSCodeshift, source: Collection): string[] => - CLIENT_NAMES.map((clientName) => - containsRequire(j, source) - ? (getRequireIdentifierName(j, source, getV2ServiceModulePath(clientName)) as string) - : (getImportIdentifierName(j, source, getV2ServiceModulePath(clientName)) as string) - ).filter((v2ServiceModuleName) => v2ServiceModuleName !== undefined); +export const getV2ServiceModuleNames = (j: JSCodeshift, source: Collection): string[] => { + if (containsRequire(j, source)) { + return CLIENT_NAMES.map((clientName) => + getRequireIdentifierName(j, source, getV2ServiceModulePath(clientName)) + ).filter((v2ServiceModuleName) => v2ServiceModuleName !== undefined) as string[]; + } + + return CLIENT_NAMES.filter((clientName) => { + const importSpecifiers = getImportSpecifiers(j, source, getV2ServiceModulePath(clientName)); + if ( + importSpecifiers && + importSpecifiers.map((importSpecifier) => importSpecifier.local?.name).includes(clientName) + ) { + return true; + } + return false; + }); +};