Skip to content

Commit 4ffee62

Browse files
committed
chore: add utility getRequireDeclarators
1 parent 013b7f2 commit 4ffee62

File tree

3 files changed

+42
-41
lines changed

3 files changed

+42
-41
lines changed

src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import {
2-
CallExpression,
3-
Collection,
4-
JSCodeshift,
5-
Literal,
6-
ObjectProperty,
7-
Property,
8-
} from "jscodeshift";
9-
import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config";
1+
import { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
2+
import { OBJECT_PROPERTY_TYPE_LIST } from "../../config";
103
import { ImportSpecifierType } from "../types";
4+
import { getRequireDeclarators } from "./getRequireDeclarators";
115

126
export const getImportSpecifiers = (
137
j: JSCodeshift,
@@ -16,38 +10,7 @@ export const getImportSpecifiers = (
1610
): ImportSpecifierType[] => {
1711
const importSpecifiers = new Set<ImportSpecifierType>();
1812

19-
const varDeclarators = source
20-
.find(j.VariableDeclarator, {
21-
init: {
22-
type: "CallExpression",
23-
callee: { type: "Identifier", name: "require" },
24-
},
25-
})
26-
.nodes()
27-
.filter((varDeclarator) => {
28-
const declaratorInit = varDeclarator.init;
29-
if (!declaratorInit || declaratorInit.type !== "CallExpression") {
30-
return false;
31-
}
32-
33-
const callExpression = declaratorInit as CallExpression;
34-
if (callExpression.arguments.length !== 1) {
35-
return false;
36-
}
37-
38-
const { value: sourceValue } = callExpression.arguments[0] as Literal;
39-
if (typeof sourceValue !== "string") {
40-
return false;
41-
}
42-
43-
if (path) {
44-
return sourceValue === path;
45-
} else {
46-
return sourceValue.startsWith(PACKAGE_NAME);
47-
}
48-
});
49-
50-
for (const varDeclarator of varDeclarators) {
13+
for (const varDeclarator of getRequireDeclarators(j, source, path).nodes()) {
5114
const declaratorId = varDeclarator.id;
5215

5316
if (declaratorId.type === "Identifier") {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { CallExpression, Collection, JSCodeshift, Literal, VariableDeclarator } from "jscodeshift";
2+
import { PACKAGE_NAME } from "../../config";
3+
4+
export const getRequireDeclarators = (
5+
j: JSCodeshift,
6+
source: Collection<unknown>,
7+
path?: string
8+
): Collection<VariableDeclarator> =>
9+
source
10+
.find(j.VariableDeclarator, {
11+
init: {
12+
type: "CallExpression",
13+
callee: { type: "Identifier", name: "require" },
14+
},
15+
})
16+
.filter((varDeclarator) => {
17+
const declaratorInit = varDeclarator.value.init;
18+
if (!declaratorInit || declaratorInit.type !== "CallExpression") {
19+
return false;
20+
}
21+
22+
const callExpression = declaratorInit as CallExpression;
23+
if (callExpression.arguments.length !== 1) {
24+
return false;
25+
}
26+
27+
const { value: sourceValue } = callExpression.arguments[0] as Literal;
28+
if (typeof sourceValue !== "string") {
29+
return false;
30+
}
31+
32+
if (path) {
33+
return sourceValue === path;
34+
}
35+
36+
return sourceValue.startsWith(PACKAGE_NAME);
37+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from "./addNamedModule";
22
export * from "./getImportSpecifiers";
3+
export * from "./getRequireDeclarators";

0 commit comments

Comments
 (0)