Skip to content

Commit 9621c39

Browse files
authored
Check only for client constructors in named imports (#212)
1 parent 800d239 commit 9621c39

File tree

6 files changed

+48
-15
lines changed

6 files changed

+48
-15
lines changed

.changeset/strong-coats-flow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Check only for client constructors in named imports
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ToDo: Update when transformation is added.
2+
// Current test verifies error is not thrown.
3+
import { ListTablesInputLimit } from "aws-sdk/clients/dynamodb";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ToDo: Update when transformation is added.
2+
// Current test verifies error is not thrown.
3+
import { ListTablesInputLimit } from "aws-sdk/clients/dynamodb";
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

3-
export const getImportIdentifierName = (
3+
export const getImportSpecifiers = (
44
j: JSCodeshift,
55
source: Collection<unknown>,
66
literalValue: string
7-
): string | undefined =>
7+
) =>
88
source
99
.find(j.ImportDeclaration, {
1010
source: { value: literalValue },
1111
})
12-
.nodes()[0]?.specifiers?.[0]?.local?.name;
12+
.nodes()[0]?.specifiers;

src/transforms/v2-to-v3/utils/get/getV2DefaultModuleName.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ import { Collection, JSCodeshift } from "jscodeshift";
22

33
import { PACKAGE_NAME } from "../config";
44
import { containsRequire } from "../containsRequire";
5-
import { getImportIdentifierName } from "./getImportIdentifierName";
5+
import { getImportSpecifiers } from "./getImportSpecifiers";
66
import { getRequireIdentifierName } from "./getRequireIdentifierName";
77

88
export const getV2DefaultModuleName = (
99
j: JSCodeshift,
1010
source: Collection<unknown>
11-
): string | undefined =>
12-
containsRequire(j, source)
13-
? getRequireIdentifierName(j, source, PACKAGE_NAME)
14-
: getImportIdentifierName(j, source, PACKAGE_NAME);
11+
): string | undefined => {
12+
if (containsRequire(j, source)) {
13+
return getRequireIdentifierName(j, source, PACKAGE_NAME);
14+
}
15+
16+
const importSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME);
17+
if (importSpecifiers && importSpecifiers.length === 1) {
18+
if (["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(importSpecifiers[0].type)) {
19+
return importSpecifiers[0].local?.name;
20+
}
21+
}
22+
23+
return undefined;
24+
};

src/transforms/v2-to-v3/utils/get/getV2ServiceModuleNames.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,25 @@ import { Collection, JSCodeshift } from "jscodeshift";
22

33
import { CLIENT_NAMES } from "../config";
44
import { containsRequire } from "../containsRequire";
5-
import { getImportIdentifierName } from "./getImportIdentifierName";
5+
import { getImportSpecifiers } from "./getImportSpecifiers";
66
import { getRequireIdentifierName } from "./getRequireIdentifierName";
77
import { getV2ServiceModulePath } from "./getV2ServiceModulePath";
88

9-
export const getV2ServiceModuleNames = (j: JSCodeshift, source: Collection<unknown>): string[] =>
10-
CLIENT_NAMES.map((clientName) =>
11-
containsRequire(j, source)
12-
? (getRequireIdentifierName(j, source, getV2ServiceModulePath(clientName)) as string)
13-
: (getImportIdentifierName(j, source, getV2ServiceModulePath(clientName)) as string)
14-
).filter((v2ServiceModuleName) => v2ServiceModuleName !== undefined);
9+
export const getV2ServiceModuleNames = (j: JSCodeshift, source: Collection<unknown>): string[] => {
10+
if (containsRequire(j, source)) {
11+
return CLIENT_NAMES.map((clientName) =>
12+
getRequireIdentifierName(j, source, getV2ServiceModulePath(clientName))
13+
).filter((v2ServiceModuleName) => v2ServiceModuleName !== undefined) as string[];
14+
}
15+
16+
return CLIENT_NAMES.filter((clientName) => {
17+
const importSpecifiers = getImportSpecifiers(j, source, getV2ServiceModulePath(clientName));
18+
if (
19+
importSpecifiers &&
20+
importSpecifiers.map((importSpecifier) => importSpecifier.local?.name).includes(clientName)
21+
) {
22+
return true;
23+
}
24+
return false;
25+
});
26+
};

0 commit comments

Comments
 (0)