diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/getImportSpecifiers.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/getImportSpecifiers.ts new file mode 100644 index 000000000..659b6d91f --- /dev/null +++ b/src/transforms/v2-to-v3/modules/importEqualsModule/getImportSpecifiers.ts @@ -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, + path?: string +): ImportSpecifierType[] => { + const importSpecifiers = new Set(); + + 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); +}; diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/index.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/index.ts index 0a5092ed4..2c50dabb5 100644 --- a/src/transforms/v2-to-v3/modules/importEqualsModule/index.ts +++ b/src/transforms/v2-to-v3/modules/importEqualsModule/index.ts @@ -1 +1,2 @@ export * from "./addNamedModule"; +export * from "./getImportSpecifiers";