Skip to content

Commit

Permalink
Replace jscodeshift-find-imports with source.find() call (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Mar 7, 2022
1 parent 339e74a commit f4d79c8
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 23 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-experts-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Replace jscodeshift-find-imports with source.find() call
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
},
"dependencies": {
"jscodeshift": "0.13.1",
"jscodeshift-find-imports": "2.0.4",
"table": "6.8.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as AWS from "aws-sdk";

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const client = new DynamoDB();
24 changes: 17 additions & 7 deletions src/transforms/v2-to-v3/utils/getV2DefaultImportName.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import { Collection, JSCodeshift } from "jscodeshift";
import findImports from "jscodeshift-find-imports";

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

for (const importObj of Object.values(imports)) {
if (importObj.type === "Identifier") return importObj.name;
}
// Set specifier name in v2DefaultImportName if it is imported in the source.
source
.find(j.ImportDeclaration, {
source: { value: "aws-sdk" },
})
.forEach((declerationPath) => {
declerationPath.value.specifiers.forEach((specifier) => {
if (
specifier.type === "ImportDefaultSpecifier" ||
specifier.type === "ImportNamespaceSpecifier"
) {
v2DefaultImportName = specifier.local.name;
}
});
});

return undefined;
return v2DefaultImportName;
};
8 changes: 3 additions & 5 deletions src/transforms/v2-to-v3/utils/removeDefaultImportIfNotUsed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ export const removeDefaultImportIfNotUsed = (
const identifierUsages = source
.find(j.Identifier, { name: defaultImportName })
// Ignore identifier from import.
.filter((identifierPath) => identifierPath.parentPath.value.type !== "ImportDefaultSpecifier");
.filter((identifierPath) => !identifierPath.parentPath.value.type.startsWith("Import"));

if (identifierUsages.size() === 0) {
source
.find(j.ImportDeclaration, {
specifiers: [{ type: "ImportDefaultSpecifier", local: { name: defaultImportName } }],
specifiers: [{ local: { name: defaultImportName } }],
source: { value: "aws-sdk" },
})
.forEach((declerationPath) => {
// Remove default import from ImportDeclaration.
declerationPath.value.specifiers = declerationPath.value.specifiers.filter(
(specifier) =>
specifier.type !== "ImportDefaultSpecifier" &&
specifier.local.name !== defaultImportName
(specifier) => specifier.local.name !== defaultImportName
);
// Remove ImportDeclaration if there are no other imports.
if (declerationPath.value.specifiers.length === 0) {
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1841,7 +1841,6 @@ __metadata:
eslint-plugin-simple-import-sort: 7.0.0
jest: 27.5.1
jscodeshift: 0.13.1
jscodeshift-find-imports: 2.0.4
lint-staged: 12.3.4
prettier: 2.5.1
simple-git-hooks: 2.7.0
Expand Down Expand Up @@ -4595,15 +4594,6 @@ __metadata:
languageName: node
linkType: hard

"jscodeshift-find-imports@npm:2.0.4":
version: 2.0.4
resolution: "jscodeshift-find-imports@npm:2.0.4"
peerDependencies:
jscodeshift: ^0.7.0 || ^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0
checksum: b57e6f6ca7c94a528d8dd6ea888765ccae03d4005026ea4423f2c8572c7abb1bff5fab96ad0f04c6cb80628ecd6944386b450dc8d231905d2cb17a34129d9b86
languageName: node
linkType: hard

"jscodeshift@npm:0.13.1":
version: 0.13.1
resolution: "jscodeshift@npm:0.13.1"
Expand Down

0 comments on commit f4d79c8

Please sign in to comment.