-
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.
chore: add utility importEqualsModule getImportSpecifiers
- Loading branch information
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
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,31 @@ | ||
import { Collection, JSCodeshift, StringLiteral, TSExternalModuleReference } from "jscodeshift"; | ||
import { PACKAGE_NAME } from "../../config"; | ||
import { ImportSpecifierType } from "../types"; | ||
|
||
export const getImportSpecifiers = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
path?: string | ||
): ImportSpecifierType[] => { | ||
const importSpecifiers = new Set<ImportSpecifierType>(); | ||
|
||
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); | ||
}); | ||
|
||
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,2 @@ | ||
export * from "./addNamedModule"; | ||
export * from "./getImportSpecifiers"; |