-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility to remove unused modules (#793)
- Loading branch information
Showing
21 changed files
with
218 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
4 changes: 0 additions & 4 deletions
4
src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.input.ts
This file was deleted.
Oops, something went wrong.
3 changes: 0 additions & 3 deletions
3
src/transforms/v2-to-v3/__fixtures__/misc/import-over-require.output.ts
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithIdentifier.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
18 changes: 18 additions & 0 deletions
18
src/transforms/v2-to-v3/modules/importEqualsModule/removeImportEquals.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/transforms/v2-to-v3/modules/importModule/removeImport.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
60
src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.