-
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.
Removes unused v2 default import (#23)
- Loading branch information
Showing
6 changed files
with
40 additions
and
5 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 | ||
--- | ||
|
||
Removes unused v2 default import |
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 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,5 +1,3 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
import { DynamoDB } from "@aws-sdk/client-dynamodb"; | ||
|
||
const region = "us-west-2"; | ||
|
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 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
31 changes: 31 additions & 0 deletions
31
src/transforms/v2-to-v3/utils/removeDefaultImportIfNotUsed.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,31 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
export const removeDefaultImportIfNotUsed = ( | ||
j: JSCodeshift, | ||
source: Collection<any>, | ||
defaultImportName: string | ||
) => { | ||
const identifierUsages = source | ||
.find(j.Identifier, { name: defaultImportName }) | ||
// Ignore identifier from import. | ||
.filter((identifierPath) => identifierPath.parentPath.value.type !== "ImportDefaultSpecifier"); | ||
|
||
if (identifierUsages.size() === 0) { | ||
source | ||
.find(j.ImportDeclaration, { | ||
specifiers: [{ type: "ImportDefaultSpecifier", local: { name: defaultImportName } }], | ||
}) | ||
.forEach((declerationPath) => { | ||
// Remove default import from ImportDecleration. | ||
declerationPath.value.specifiers = declerationPath.value.specifiers.filter( | ||
(specifier) => | ||
specifier.type !== "ImportDefaultSpecifier" && | ||
specifier.local.name !== defaultImportName | ||
); | ||
// Remove ImportDeclaration if there are no other imports. | ||
if (declerationPath.value.specifiers.length === 0) { | ||
j(declerationPath).remove(); | ||
} | ||
}); | ||
} | ||
}; |