Skip to content

Commit

Permalink
chore: add utility getRequireVarDeclarators
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Feb 28, 2024
1 parent 013b7f2 commit 4cf0500
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import {
CallExpression,
Collection,
JSCodeshift,
Literal,
ObjectProperty,
Property,
} from "jscodeshift";
import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config";
import { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
import { OBJECT_PROPERTY_TYPE_LIST } from "../../config";
import { ImportSpecifierType } from "../types";
import { getRequireVarDeclarators } from "./getRequireVarDeclarators";

export const getImportSpecifiers = (
j: JSCodeshift,
Expand All @@ -16,38 +10,9 @@ export const getImportSpecifiers = (
): ImportSpecifierType[] => {
const importSpecifiers = new Set<ImportSpecifierType>();

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);
}
});
const varDeclarators = getRequireVarDeclarators(j, source, path);

for (const varDeclarator of varDeclarators) {
for (const varDeclarator of varDeclarators.nodes()) {
const declaratorId = varDeclarator.id;

if (declaratorId.type === "Identifier") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { CallExpression, Collection, JSCodeshift, Literal, VariableDeclarator } from "jscodeshift";
import { PACKAGE_NAME } from "../../config";

export const getRequireVarDeclarators = (
j: JSCodeshift,
source: Collection<unknown>,
path?: string
): Collection<VariableDeclarator> =>
source
.find(j.VariableDeclarator, {
init: {
type: "CallExpression",
callee: { type: "Identifier", name: "require" },
},
})
.filter((varDeclarator) => {
const declaratorInit = varDeclarator.value.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;
}

return sourceValue.startsWith(PACKAGE_NAME);
});

0 comments on commit 4cf0500

Please sign in to comment.