Skip to content

Commit 3b3401a

Browse files
authored
Use closest API to find package import variable declaration (#782)
1 parent 858acad commit 3b3401a

File tree

2 files changed

+26
-48
lines changed

2 files changed

+26
-48
lines changed

.changeset/pretty-beers-float.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Use closest API to find package import variable declaration

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

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
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";
210

311
import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME, STRING_LITERAL_TYPE_LIST } from "../../config";
412
import { getRequireDeclarators } from "../getRequireDeclarators";
@@ -56,57 +64,22 @@ export const addNamedModule = (
5664
),
5765
]);
5866

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)) {
10274
return false;
10375
}
104-
)
105-
);
76+
const argValue = (arg as Literal | StringLiteral).value;
77+
return typeof argValue === "string" && argValue.startsWith(PACKAGE_NAME);
78+
});
10679

107-
if (v2RequireDeclarations.size()) {
80+
if (v2RequireCallExpressions.size()) {
10881
// Insert it after the first v2 require declaration.
109-
v2RequireDeclarations.at(0).insertAfter(v3RequireDeclaration);
82+
v2RequireCallExpressions.at(0).closest(j.VariableDeclaration).insertAfter(v3RequireDeclaration);
11083
return;
11184
}
11285

0 commit comments

Comments
 (0)