Skip to content

Commit

Permalink
chore: replace getRequireIds with getImportSpecifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Feb 27, 2024
1 parent 8426e85 commit 1e4fcff
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 43 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import {
Collection,
Identifier,
JSCodeshift,
ObjectPattern,
ObjectProperty,
Property,
} from "jscodeshift";
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { CLIENT_NAMES, OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../config";
import { getRequireDeclaratorsWithProperty } from "../modules";
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
import {
ImportSpecifierDefault,
ImportSpecifierPattern,
getRequireDeclaratorsWithProperty,
} from "../modules";
import { getImportSpecifiers } from "../modules/requireModule";
import { getClientDeepImportPath } from "../utils";
import { getRequireIds } from "./getRequireIds";

export const getClientNamesRecordFromRequire = (
j: JSCodeshift,
Expand All @@ -19,22 +16,13 @@ export const getClientNamesRecordFromRequire = (
) => {
const clientNamesRecord: Record<string, string> = {};

const idPropertiesFromObjectPattern = getRequireIds(j, source, PACKAGE_NAME)
.filter((id) => id.type === "ObjectPattern")
.map((objectPattern) => (objectPattern as ObjectPattern).properties)
.flat();
const idPropertiesFromObjectPattern = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
(importSpecifier) => typeof importSpecifier === "object"
) as ImportSpecifierPattern[];

for (const idProperty of idPropertiesFromObjectPattern) {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(idProperty.type)) {
continue;
}
const key = (idProperty as Property | ObjectProperty).key;
const value = (idProperty as Property | ObjectProperty).value;
if (key.type !== "Identifier" || value.type !== "Identifier") {
continue;
}
if (CLIENT_NAMES.includes(key.name)) {
clientNamesRecord[key.name] = value.name;
for (const { importedName, localName } of idPropertiesFromObjectPattern) {
if (CLIENT_NAMES.includes(importedName)) {
clientNamesRecord[importedName] = localName || importedName;
}
}

Expand All @@ -59,11 +47,11 @@ export const getClientNamesRecordFromRequire = (

for (const clientName of clientNamesFromDeepImport) {
const deepImportPath = getClientDeepImportPath(clientName);
const idsFromDefaultImport = getRequireIds(j, source, deepImportPath).filter(
(id) => id.type === "Identifier"
);
const idsFromDefaultImport = getImportSpecifiers(j, source, deepImportPath).filter(
(importSpecifier) => typeof importSpecifier === "string"
) as ImportSpecifierDefault[];
if (idsFromDefaultImport.length) {
clientNamesRecord[clientName] = (idsFromDefaultImport[0] as Identifier).name;
clientNamesRecord[clientName] = idsFromDefaultImport[0];
}
}

Expand Down
13 changes: 0 additions & 13 deletions src/transforms/v2-to-v3/client-names/getRequireIds.ts

This file was deleted.

0 comments on commit 1e4fcff

Please sign in to comment.