Skip to content

Commit c1669be

Browse files
authored
Add utility to remove unused modules (#793)
1 parent 07ebed7 commit c1669be

21 files changed

+218
-388
lines changed

.changeset/shiny-dogs-confess.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+
Add utility to remove unused modules

src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.input.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.output.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithIdentifier.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./addNamedModule";
22
export * from "./getImportSpecifiers";
33
export * from "./getImportEqualsDeclarations";
4+
export * from "./removeImportEquals";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { removeDeclaration } from "../removeDeclaration";
3+
import { getImportEqualsDeclarations } from "./getImportEqualsDeclarations";
4+
5+
const isAnotherSpecifier = (j: JSCodeshift, source: Collection<unknown>, localName: string) =>
6+
source.find(j.TSImportEqualsDeclaration, { id: { name: localName } }).size() > 1;
7+
8+
export const removeImportEquals = (j: JSCodeshift, source: Collection<unknown>) =>
9+
getImportEqualsDeclarations(j, source).forEach((importEqualsDeclaration) => {
10+
const localName = importEqualsDeclaration.value.id.name;
11+
const identifiers = source.find(j.Identifier, { name: localName });
12+
13+
// Either the identifier is the only occurence on the page.
14+
// Or there is another specifier with the same name imported from JS SDK v3.
15+
if (identifiers.size() === 1 || isAnotherSpecifier(j, source, localName)) {
16+
removeDeclaration(j, source, importEqualsDeclaration.get());
17+
}
18+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./addNamedModule";
22
export * from "./getImportSpecifiers";
33
export * from "./getImportDeclarations";
4+
export * from "./removeImport";
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { removeDeclaration } from "../removeDeclaration";
3+
import { getImportDeclarations } from "./getImportDeclarations";
4+
5+
const isAnotherSpecifier = (j: JSCodeshift, source: Collection<unknown>, localName: string) =>
6+
source
7+
.find(j.ImportDeclaration, { specifiers: [{ local: { name: localName } }] })
8+
.filter((importDeclaration) => {
9+
const sourceValue = importDeclaration.value.source.value;
10+
if (typeof sourceValue !== "string") {
11+
return false;
12+
}
13+
return sourceValue.startsWith("@aws-sdk/");
14+
})
15+
.size() > 0;
16+
17+
export const removeImport = (j: JSCodeshift, source: Collection<unknown>) =>
18+
getImportDeclarations(j, source).forEach((importDeclaration) => {
19+
importDeclaration.value.specifiers = (importDeclaration.value.specifiers || []).filter(
20+
(specifier) => {
21+
const localName = specifier.local?.name;
22+
if (!localName) {
23+
return true;
24+
}
25+
const identifiers = source.find(j.Identifier, { name: localName });
26+
const importedName = specifier.type === "ImportSpecifier" && specifier.imported?.name;
27+
28+
// For default or namespace import, there's only one occurrence of local identifier.
29+
// For named import, there can be two occurrences: one imported identifier and one local identifier.
30+
const identifierNum = importedName && importedName === localName ? 2 : 1;
31+
32+
// Either the identifiers are the only occurences on the page.
33+
// Or there's another specifier with the same name imported from JS SDK v3.
34+
return !(identifiers.size() === identifierNum || isAnotherSpecifier(j, source, localName));
35+
}
36+
);
37+
38+
// Remove ImportDeclaration if there are no import specifiers.
39+
if (importDeclaration.value.specifiers.length === 0) {
40+
removeDeclaration(j, source, importDeclaration);
41+
}
42+
});

src/transforms/v2-to-v3/modules/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@ export * from "./addNamedModule";
33
export * from "./getGlobalNameFromModule";
44
export * from "./getImportType";
55
export * from "./getRequireDeclaratorsWithProperty";
6-
export * from "./removeClientModule";
7-
export * from "./removeGlobalModule";
6+
export * from "./removeModules";
87
export * from "./types";

src/transforms/v2-to-v3/modules/removeClientModule.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/transforms/v2-to-v3/modules/removeGlobalModule.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/transforms/v2-to-v3/modules/removeImportDefault.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/transforms/v2-to-v3/modules/removeImportEquals.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/transforms/v2-to-v3/modules/removeImportNamed.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { removeImportEquals } from "./importEqualsModule";
3+
import { removeImport } from "./importModule";
4+
import { removeRequire } from "./requireModule";
5+
import { ImportType } from "./types";
6+
7+
export const removeModules = (
8+
j: JSCodeshift,
9+
source: Collection<unknown>,
10+
importType: ImportType
11+
) => {
12+
switch (importType) {
13+
case ImportType.REQUIRE: {
14+
removeRequire(j, source);
15+
break;
16+
}
17+
case ImportType.IMPORT_EQUALS: {
18+
removeImportEquals(j, source);
19+
break;
20+
}
21+
case ImportType.IMPORT: {
22+
removeImport(j, source);
23+
break;
24+
}
25+
}
26+
};

src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)