|
1 |
| -import { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift"; |
| 1 | +import { |
| 2 | + Collection, |
| 3 | + JSCodeshift, |
| 4 | + Literal, |
| 5 | + ObjectPattern, |
| 6 | + ObjectProperty, |
| 7 | + Property, |
| 8 | + StringLiteral, |
| 9 | +} from "jscodeshift"; |
2 | 10 |
|
3 | 11 | import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME, STRING_LITERAL_TYPE_LIST } from "../../config";
|
4 | 12 | import { getRequireDeclarators } from "../getRequireDeclarators";
|
@@ -56,57 +64,22 @@ export const addNamedModule = (
|
56 | 64 | ),
|
57 | 65 | ]);
|
58 | 66 |
|
59 |
| - const v2RequireDeclarations = source.find(j.VariableDeclaration).filter((variableDeclaration) => |
60 |
| - variableDeclaration.value.declarations.some( |
61 |
| - // @ts-expect-error Type 'JSXIdentifier' is not assignable to type 'Identifier'. |
62 |
| - (declaration: VariableDeclarator | Identifier) => { |
63 |
| - if (declaration.type === "Identifier") return false; |
64 |
| - |
65 |
| - const init = declaration.init; |
66 |
| - if (!init) return false; |
67 |
| - |
68 |
| - // Checks for require identifier or object pattern. |
69 |
| - if (init.type === "CallExpression") { |
70 |
| - const callee = init.callee; |
71 |
| - if (!callee) return false; |
72 |
| - if (callee.type !== "Identifier") return false; |
73 |
| - if (callee.name !== "require") return false; |
74 |
| - |
75 |
| - const args = init.arguments; |
76 |
| - if (!args) return false; |
77 |
| - if (args.length !== 1) return false; |
78 |
| - if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true; |
79 |
| - if (typeof args[0].value !== "string") return false; |
80 |
| - if (!args[0].value.startsWith(PACKAGE_NAME)) return false; |
81 |
| - |
82 |
| - return true; |
83 |
| - } |
84 |
| - |
85 |
| - // Checks for require property. |
86 |
| - if (init.type === "MemberExpression") { |
87 |
| - const object = init.object; |
88 |
| - if (object.type !== "CallExpression") return false; |
89 |
| - |
90 |
| - const callee = object.callee; |
91 |
| - if (callee.type !== "Identifier") return false; |
92 |
| - if (callee.name !== "require") return false; |
93 |
| - |
94 |
| - const args = object.arguments; |
95 |
| - if (args.length !== 1) return false; |
96 |
| - if (!STRING_LITERAL_TYPE_LIST.includes(args[0].type)) return true; |
97 |
| - if (args[0].value !== PACKAGE_NAME) return false; |
98 |
| - |
99 |
| - return true; |
100 |
| - } |
101 |
| - |
| 67 | + const v2RequireCallExpressions = source |
| 68 | + .find(j.CallExpression, { |
| 69 | + callee: { type: "Identifier", name: "require" }, |
| 70 | + }) |
| 71 | + .filter((callExpression) => { |
| 72 | + const arg = callExpression.value.arguments[0]; |
| 73 | + if (!STRING_LITERAL_TYPE_LIST.includes(arg.type)) { |
102 | 74 | return false;
|
103 | 75 | }
|
104 |
| - ) |
105 |
| - ); |
| 76 | + const argValue = (arg as Literal | StringLiteral).value; |
| 77 | + return typeof argValue === "string" && argValue.startsWith(PACKAGE_NAME); |
| 78 | + }); |
106 | 79 |
|
107 |
| - if (v2RequireDeclarations.size()) { |
| 80 | + if (v2RequireCallExpressions.size()) { |
108 | 81 | // Insert it after the first v2 require declaration.
|
109 |
| - v2RequireDeclarations.at(0).insertAfter(v3RequireDeclaration); |
| 82 | + v2RequireCallExpressions.at(0).closest(j.VariableDeclaration).insertAfter(v3RequireDeclaration); |
110 | 83 | return;
|
111 | 84 | }
|
112 | 85 |
|
|
0 commit comments