-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility importEqualsModule getImportSpecifiers (#790)
- Loading branch information
Showing
13 changed files
with
76 additions
and
91 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 importEqualsModule getImportSpecifiers |
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
10 changes: 0 additions & 10 deletions
10
src/transforms/v2-to-v3/modules/getImportEqualsDeclarationType.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,19 +1,5 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { PACKAGE_NAME } from "../config"; | ||
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType"; | ||
import { getImportEqualsDeclarations } from "./importEqualsModule"; | ||
|
||
export const hasImportEquals = (j: JSCodeshift, source: Collection<unknown>) => | ||
source | ||
.find(j.TSImportEqualsDeclaration, getImportEqualsDeclarationType()) | ||
.filter((importEqualsDeclaration) => { | ||
const { moduleReference } = importEqualsDeclaration.value; | ||
if (moduleReference.type !== "TSExternalModuleReference") return false; | ||
const { expression } = moduleReference; | ||
return ( | ||
expression.type === "StringLiteral" && | ||
typeof expression.value === "string" && | ||
expression.value.startsWith(PACKAGE_NAME) | ||
); | ||
}) | ||
.size() > 0; | ||
getImportEqualsDeclarations(j, source).size() > 0; |
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
25 changes: 25 additions & 0 deletions
25
src/transforms/v2-to-v3/modules/importEqualsModule/getImportEqualsDeclarations.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,25 @@ | ||
import { Collection, JSCodeshift, StringLiteral, TSExternalModuleReference } from "jscodeshift"; | ||
import { PACKAGE_NAME } from "../../config"; | ||
|
||
export const getImportEqualsDeclarations = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
path?: string | ||
) => | ||
source | ||
.find(j.TSImportEqualsDeclaration, { | ||
type: "TSImportEqualsDeclaration", | ||
moduleReference: { | ||
type: "TSExternalModuleReference", | ||
expression: { type: "StringLiteral" }, | ||
}, | ||
}) | ||
.filter((importEqualsDeclaration) => { | ||
const moduleReference = importEqualsDeclaration.value | ||
.moduleReference as TSExternalModuleReference; | ||
const expressionValue = (moduleReference.expression as StringLiteral).value; | ||
if (path) { | ||
return expressionValue === path; | ||
} | ||
return expressionValue.startsWith(PACKAGE_NAME); | ||
}); |
17 changes: 17 additions & 0 deletions
17
src/transforms/v2-to-v3/modules/importEqualsModule/getImportSpecifiers.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,17 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
import { ImportSpecifierType } from "../types"; | ||
import { getImportEqualsDeclarations } from "./getImportEqualsDeclarations"; | ||
|
||
export const getImportSpecifiers = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
path?: string | ||
): ImportSpecifierType[] => { | ||
const importSpecifiers = new Set<ImportSpecifierType>(); | ||
|
||
getImportEqualsDeclarations(j, source, path).forEach((importEqualsDeclaration) => { | ||
importSpecifiers.add({ localName: importEqualsDeclaration.value.id.name }); | ||
}); | ||
|
||
return Array.from(importSpecifiers); | ||
}; |
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 +1,3 @@ | ||
export * from "./addNamedModule"; | ||
export * from "./getImportSpecifiers"; | ||
export * from "./getImportEqualsDeclarations"; |
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
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