From 8426e85acb4e0f93513c2fc9d76f1733c05c3848 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Mon, 26 Feb 2024 14:28:49 -0800 Subject: [PATCH] chore: add utility requireModule/getImportSpecifiers --- .../requireModule/getImportSpecifiers.ts | 78 +++++++++++++++++++ .../v2-to-v3/modules/requireModule/index.ts | 1 + 2 files changed, 79 insertions(+) create mode 100644 src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts diff --git a/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts b/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts new file mode 100644 index 000000000..508918fd9 --- /dev/null +++ b/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts @@ -0,0 +1,78 @@ +import { + CallExpression, + Collection, + JSCodeshift, + Literal, + ObjectProperty, + Property, +} from "jscodeshift"; +import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config"; +import { ImportSpecifierType } from "../types"; + +export const getImportSpecifiers = ( + j: JSCodeshift, + source: Collection, + path?: string +): ImportSpecifierType[] => { + const importSpecifiers = new Set(); + + const varDeclarators = source + .find(j.VariableDeclarator, { + init: { + type: "CallExpression", + callee: { type: "Identifier", name: "require" }, + }, + }) + .nodes() + .filter((varDeclarator) => { + const declaratorInit = varDeclarator.init; + if (!declaratorInit || declaratorInit.type !== "CallExpression") { + return false; + } + + const callExpression = declaratorInit as CallExpression; + if (callExpression.arguments.length !== 1) { + return false; + } + + const { value: sourceValue } = callExpression.arguments[0] as Literal; + if (typeof sourceValue !== "string") { + return false; + } + + if (path) { + return sourceValue === path; + } else { + return sourceValue.startsWith(PACKAGE_NAME); + } + }); + + for (const varDeclarator of varDeclarators) { + const declaratorId = varDeclarator.id; + + if (declaratorId.type === "Identifier") { + const declaratorIdName = declaratorId.name; + importSpecifiers.add(declaratorIdName); + } + + if (declaratorId.type === "ObjectPattern") { + const properties = declaratorId.properties; + for (const property of properties) { + if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) { + continue; + } + const objectProperty = property as Property | ObjectProperty; + const key = objectProperty.key; + const value = objectProperty.value; + if (key.type === "Identifier" && value.type === "Identifier") { + importSpecifiers.add({ + importedName: key.name, + localName: value.name, + }); + } + } + } + } + + return Array.from(importSpecifiers); +}; diff --git a/src/transforms/v2-to-v3/modules/requireModule/index.ts b/src/transforms/v2-to-v3/modules/requireModule/index.ts index 0a5092ed4..2c50dabb5 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/index.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/index.ts @@ -1 +1,2 @@ export * from "./addNamedModule"; +export * from "./getImportSpecifiers";