Skip to content

Commit

Permalink
chore: add utility getRequireDeclarators
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Feb 28, 2024
1 parent 013b7f2 commit 4ffee62
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 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 { getRequireDeclarators } from "./getRequireDeclarators";

export const getImportSpecifiers = (
j: JSCodeshift,
Expand All @@ -16,38 +10,7 @@ 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);
}
});

for (const varDeclarator of varDeclarators) {
for (const varDeclarator of getRequireDeclarators(j, source, path).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 getRequireDeclarators = (
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);
});
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/modules/requireModule/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./addNamedModule";
export * from "./getImportSpecifiers";
export * from "./getRequireDeclarators";

0 comments on commit 4ffee62

Please sign in to comment.