Skip to content

Commit

Permalink
Check only for client constructors in named imports (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Dec 24, 2022
1 parent 800d239 commit 9621c39
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-coats-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Check only for client constructors in named imports
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// ToDo: Update when transformation is added.
// Current test verifies error is not thrown.
import { ListTablesInputLimit } from "aws-sdk/clients/dynamodb";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// ToDo: Update when transformation is added.
// Current test verifies error is not thrown.
import { ListTablesInputLimit } from "aws-sdk/clients/dynamodb";
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Collection, JSCodeshift } from "jscodeshift";

export const getImportIdentifierName = (
export const getImportSpecifiers = (
j: JSCodeshift,
source: Collection<unknown>,
literalValue: string
): string | undefined =>
) =>
source
.find(j.ImportDeclaration, {
source: { value: literalValue },
})
.nodes()[0]?.specifiers?.[0]?.local?.name;
.nodes()[0]?.specifiers;
20 changes: 15 additions & 5 deletions src/transforms/v2-to-v3/utils/get/getV2DefaultModuleName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>
): 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;
};
26 changes: 19 additions & 7 deletions src/transforms/v2-to-v3/utils/get/getV2ServiceModuleNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>): 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<unknown>): 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;
});
};

0 comments on commit 9621c39

Please sign in to comment.