Skip to content

Commit f4d79c8

Browse files
authored
Replace jscodeshift-find-imports with source.find() call (#59)
1 parent 339e74a commit f4d79c8

File tree

7 files changed

+31
-23
lines changed

7 files changed

+31
-23
lines changed

.changeset/rude-experts-draw.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+
Replace jscodeshift-find-imports with source.find() call

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
},
3535
"dependencies": {
3636
"jscodeshift": "0.13.1",
37-
"jscodeshift-find-imports": "2.0.4",
3837
"table": "6.8.0"
3938
},
4039
"devDependencies": {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as AWS from "aws-sdk";
2+
3+
const client = new AWS.DynamoDB();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const client = new DynamoDB();
Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
2-
import findImports from "jscodeshift-find-imports";
32

43
export const getV2DefaultImportName = (
54
j: JSCodeshift,
65
source: Collection<any>
76
): string | undefined => {
8-
const { statement } = j.template;
9-
const imports = findImports(source, statement`import AWS from 'aws-sdk'`);
7+
let v2DefaultImportName = undefined;
108

11-
for (const importObj of Object.values(imports)) {
12-
if (importObj.type === "Identifier") return importObj.name;
13-
}
9+
// Set specifier name in v2DefaultImportName if it is imported in the source.
10+
source
11+
.find(j.ImportDeclaration, {
12+
source: { value: "aws-sdk" },
13+
})
14+
.forEach((declerationPath) => {
15+
declerationPath.value.specifiers.forEach((specifier) => {
16+
if (
17+
specifier.type === "ImportDefaultSpecifier" ||
18+
specifier.type === "ImportNamespaceSpecifier"
19+
) {
20+
v2DefaultImportName = specifier.local.name;
21+
}
22+
});
23+
});
1424

15-
return undefined;
25+
return v2DefaultImportName;
1626
};

src/transforms/v2-to-v3/utils/removeDefaultImportIfNotUsed.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@ export const removeDefaultImportIfNotUsed = (
88
const identifierUsages = source
99
.find(j.Identifier, { name: defaultImportName })
1010
// Ignore identifier from import.
11-
.filter((identifierPath) => identifierPath.parentPath.value.type !== "ImportDefaultSpecifier");
11+
.filter((identifierPath) => !identifierPath.parentPath.value.type.startsWith("Import"));
1212

1313
if (identifierUsages.size() === 0) {
1414
source
1515
.find(j.ImportDeclaration, {
16-
specifiers: [{ type: "ImportDefaultSpecifier", local: { name: defaultImportName } }],
16+
specifiers: [{ local: { name: defaultImportName } }],
1717
source: { value: "aws-sdk" },
1818
})
1919
.forEach((declerationPath) => {
2020
// Remove default import from ImportDeclaration.
2121
declerationPath.value.specifiers = declerationPath.value.specifiers.filter(
22-
(specifier) =>
23-
specifier.type !== "ImportDefaultSpecifier" &&
24-
specifier.local.name !== defaultImportName
22+
(specifier) => specifier.local.name !== defaultImportName
2523
);
2624
// Remove ImportDeclaration if there are no other imports.
2725
if (declerationPath.value.specifiers.length === 0) {

yarn.lock

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,6 @@ __metadata:
18411841
eslint-plugin-simple-import-sort: 7.0.0
18421842
jest: 27.5.1
18431843
jscodeshift: 0.13.1
1844-
jscodeshift-find-imports: 2.0.4
18451844
lint-staged: 12.3.4
18461845
prettier: 2.5.1
18471846
simple-git-hooks: 2.7.0
@@ -4595,15 +4594,6 @@ __metadata:
45954594
languageName: node
45964595
linkType: hard
45974596

4598-
"jscodeshift-find-imports@npm:2.0.4":
4599-
version: 2.0.4
4600-
resolution: "jscodeshift-find-imports@npm:2.0.4"
4601-
peerDependencies:
4602-
jscodeshift: ^0.7.0 || ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0
4603-
checksum: b57e6f6ca7c94a528d8dd6ea888765ccae03d4005026ea4423f2c8572c7abb1bff5fab96ad0f04c6cb80628ecd6944386b450dc8d231905d2cb17a34129d9b86
4604-
languageName: node
4605-
linkType: hard
4606-
46074597
"jscodeshift@npm:0.13.1":
46084598
version: 0.13.1
46094599
resolution: "jscodeshift@npm:0.13.1"

0 commit comments

Comments
 (0)