diff --git a/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts b/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts index 508918fd9..139e1982e 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts @@ -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, @@ -16,38 +10,9 @@ export const getImportSpecifiers = ( ): ImportSpecifierType[] => { const importSpecifiers = new Set(); - 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") { diff --git a/src/transforms/v2-to-v3/modules/requireModule/getRequireVarDeclarators.ts b/src/transforms/v2-to-v3/modules/requireModule/getRequireVarDeclarators.ts new file mode 100644 index 000000000..0d4285ba3 --- /dev/null +++ b/src/transforms/v2-to-v3/modules/requireModule/getRequireVarDeclarators.ts @@ -0,0 +1,37 @@ +import { CallExpression, Collection, JSCodeshift, Literal, VariableDeclarator } from "jscodeshift"; +import { PACKAGE_NAME } from "../../config"; + +export const getRequireVarDeclarators = ( + j: JSCodeshift, + source: Collection, + path?: string +): Collection => + 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); + });