Skip to content

Commit

Permalink
Add utility to remove unused modules (#793)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Mar 1, 2024
1 parent 07ebed7 commit c1669be
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 388 deletions.
5 changes: 5 additions & 0 deletions .changeset/shiny-dogs-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add utility to remove unused modules

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./addNamedModule";
export * from "./getImportSpecifiers";
export * from "./getImportEqualsDeclarations";
export * from "./removeImportEquals";
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeDeclaration } from "../removeDeclaration";
import { getImportEqualsDeclarations } from "./getImportEqualsDeclarations";

const isAnotherSpecifier = (j: JSCodeshift, source: Collection<unknown>, localName: string) =>
source.find(j.TSImportEqualsDeclaration, { id: { name: localName } }).size() > 1;

export const removeImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
getImportEqualsDeclarations(j, source).forEach((importEqualsDeclaration) => {
const localName = importEqualsDeclaration.value.id.name;
const identifiers = source.find(j.Identifier, { name: localName });

// Either the identifier is the only occurence on the page.
// Or there is another specifier with the same name imported from JS SDK v3.
if (identifiers.size() === 1 || isAnotherSpecifier(j, source, localName)) {
removeDeclaration(j, source, importEqualsDeclaration.get());
}
});
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/modules/importModule/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./addNamedModule";
export * from "./getImportSpecifiers";
export * from "./getImportDeclarations";
export * from "./removeImport";
42 changes: 42 additions & 0 deletions src/transforms/v2-to-v3/modules/importModule/removeImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeDeclaration } from "../removeDeclaration";
import { getImportDeclarations } from "./getImportDeclarations";

const isAnotherSpecifier = (j: JSCodeshift, source: Collection<unknown>, localName: string) =>
source
.find(j.ImportDeclaration, { specifiers: [{ local: { name: localName } }] })
.filter((importDeclaration) => {
const sourceValue = importDeclaration.value.source.value;
if (typeof sourceValue !== "string") {
return false;
}
return sourceValue.startsWith("@aws-sdk/");
})
.size() > 0;

export const removeImport = (j: JSCodeshift, source: Collection<unknown>) =>
getImportDeclarations(j, source).forEach((importDeclaration) => {
importDeclaration.value.specifiers = (importDeclaration.value.specifiers || []).filter(
(specifier) => {
const localName = specifier.local?.name;
if (!localName) {
return true;
}
const identifiers = source.find(j.Identifier, { name: localName });
const importedName = specifier.type === "ImportSpecifier" && specifier.imported?.name;

// For default or namespace import, there's only one occurrence of local identifier.
// For named import, there can be two occurrences: one imported identifier and one local identifier.
const identifierNum = importedName && importedName === localName ? 2 : 1;

// Either the identifiers are the only occurences on the page.
// Or there's another specifier with the same name imported from JS SDK v3.
return !(identifiers.size() === identifierNum || isAnotherSpecifier(j, source, localName));
}
);

// Remove ImportDeclaration if there are no import specifiers.
if (importDeclaration.value.specifiers.length === 0) {
removeDeclaration(j, source, importDeclaration);
}
});
3 changes: 1 addition & 2 deletions src/transforms/v2-to-v3/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ export * from "./addNamedModule";
export * from "./getGlobalNameFromModule";
export * from "./getImportType";
export * from "./getRequireDeclaratorsWithProperty";
export * from "./removeClientModule";
export * from "./removeGlobalModule";
export * from "./removeModules";
export * from "./types";
50 changes: 0 additions & 50 deletions src/transforms/v2-to-v3/modules/removeClientModule.ts

This file was deleted.

25 changes: 0 additions & 25 deletions src/transforms/v2-to-v3/modules/removeGlobalModule.ts

This file was deleted.

33 changes: 0 additions & 33 deletions src/transforms/v2-to-v3/modules/removeImportDefault.ts

This file was deleted.

21 changes: 0 additions & 21 deletions src/transforms/v2-to-v3/modules/removeImportEquals.ts

This file was deleted.

43 changes: 0 additions & 43 deletions src/transforms/v2-to-v3/modules/removeImportNamed.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/transforms/v2-to-v3/modules/removeModules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { removeImportEquals } from "./importEqualsModule";
import { removeImport } from "./importModule";
import { removeRequire } from "./requireModule";
import { ImportType } from "./types";

export const removeModules = (
j: JSCodeshift,
source: Collection<unknown>,
importType: ImportType
) => {
switch (importType) {
case ImportType.REQUIRE: {
removeRequire(j, source);
break;
}
case ImportType.IMPORT_EQUALS: {
removeImportEquals(j, source);
break;
}
case ImportType.IMPORT: {
removeImport(j, source);
break;
}
}
};
60 changes: 0 additions & 60 deletions src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts

This file was deleted.

Loading

0 comments on commit c1669be

Please sign in to comment.