Skip to content

Commit

Permalink
chore: add utility importEqualsModule getImportSpecifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Feb 29, 2024
1 parent 5282e99 commit b663d42
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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);
})
.forEach((importEqualsDeclaration) => {
importSpecifiers.add({ localName: importEqualsDeclaration.value.id.name });
});

return Array.from(importSpecifiers);
};
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./addNamedModule";
export * from "./getImportSpecifiers";

0 comments on commit b663d42

Please sign in to comment.