Skip to content

Commit

Permalink
chore: remove type assertion in getImportSpecifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Jul 28, 2024
1 parent 3aed785 commit f5ab6b1
Showing 1 changed file with 11 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -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<ImportSpecifierType>();

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<unknown>,
Expand Down Expand Up @@ -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,
});
}
}
}
}
Expand Down

0 comments on commit f5ab6b1

Please sign in to comment.