From f5ab6b116a0b1d52aa23fb0cd58c949bd727bc6e Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Sun, 28 Jul 2024 15:20:02 -0700 Subject: [PATCH] chore: remove type assertion in getImportSpecifiers --- .../requireModule/getImportSpecifiers.ts | 37 ++++++------------- 1 file changed, 11 insertions(+), 26 deletions(-) diff --git a/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts b/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts index 5148ca87f..2b4076e16 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts @@ -1,29 +1,7 @@ -import type { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift"; -import { OBJECT_PROPERTY_TYPE_LIST } from "../../config"; +import type { Collection, JSCodeshift } from "jscodeshift"; import type { ImportSpecifierType } from "../types"; import { getRequireDeclarators } from "./getRequireDeclarators"; -const getImportSpecifiersFromObjectPattern = (properties: (Property | ObjectProperty)[]) => { - const importSpecifiers = new Set(); - - 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); -}; - export const getImportSpecifiers = ( j: JSCodeshift, source: Collection, @@ -54,9 +32,16 @@ export const getImportSpecifiers = ( if (declaratorInit?.type !== "CallExpression") { continue; } - const properties = declaratorId.properties as (Property | ObjectProperty)[]; - for (const importSpecifier of getImportSpecifiersFromObjectPattern(properties)) { - importSpecifiers.add(importSpecifier); + for (const property of declaratorId.properties) { + if (property.type !== "Property" && property.type !== "ObjectProperty") { + continue; + } + if (property.key.type === "Identifier" && property.value.type === "Identifier") { + importSpecifiers.add({ + importedName: property.key.name, + localName: property.value.name, + }); + } } } }