chore: format codemods files

This commit is contained in:
Alem Tuzlak
2026-04-07 12:29:28 +02:00
parent 94c5bba46e
commit 31247f3663
2 changed files with 47 additions and 16 deletions
@@ -197,7 +197,9 @@ console.log(ImageUpload);
expect(output).toContain("import type { Attachment }");
expect(output).toContain("const x: Attachment");
// Local variable declaration and its reference should NOT be renamed
expect(output).toContain('const ImageUpload = "unrelated local variable"');
expect(output).toContain(
'const ImageUpload = "unrelated local variable"',
);
expect(output).toContain("console.log(ImageUpload)");
});
+44 -15
View File
@@ -76,9 +76,15 @@ export default function transform(file: FileInfo, api: API) {
// references alone since they may refer to the local binding.
if (!isAliased) {
const hasShadow =
root.find(j.VariableDeclarator, { id: { type: "Identifier", name: localName } }).length > 0 ||
root.find(j.FunctionDeclaration, { id: { type: "Identifier", name: localName } }).length > 0 ||
root.find(j.ClassDeclaration, { id: { type: "Identifier", name: localName } }).length > 0;
root.find(j.VariableDeclarator, {
id: { type: "Identifier", name: localName },
}).length > 0 ||
root.find(j.FunctionDeclaration, {
id: { type: "Identifier", name: localName },
}).length > 0 ||
root.find(j.ClassDeclaration, {
id: { type: "Identifier", name: localName },
}).length > 0;
root.find(j.Identifier, { name: localName }).forEach((idPath) => {
// Skip the import specifier itself — already renamed above
@@ -87,29 +93,50 @@ export default function transform(file: FileInfo, api: API) {
const parent = idPath.parent.node;
// Skip declaration positions — these define new bindings
if (parent.type === "VariableDeclarator" && parent.id === idPath.node) return;
if (parent.type === "FunctionDeclaration" && parent.id === idPath.node) return;
if (parent.type === "ClassDeclaration" && parent.id === idPath.node) return;
if (parent.type === "TSTypeAliasDeclaration" && parent.id === idPath.node) return;
if (parent.type === "TSInterfaceDeclaration" && parent.id === idPath.node) return;
if (
parent.type === "VariableDeclarator" &&
parent.id === idPath.node
)
return;
if (
parent.type === "FunctionDeclaration" &&
parent.id === idPath.node
)
return;
if (parent.type === "ClassDeclaration" && parent.id === idPath.node)
return;
if (
parent.type === "TSTypeAliasDeclaration" &&
parent.id === idPath.node
)
return;
if (
parent.type === "TSInterfaceDeclaration" &&
parent.id === idPath.node
)
return;
// Skip non-computed object property keys and member expression properties
if (
(parent.type === "Property" || parent.type === "ObjectProperty") &&
(parent.type === "Property" ||
parent.type === "ObjectProperty") &&
parent.key === idPath.node &&
!parent.computed
) return;
)
return;
if (
parent.type === "MemberExpression" &&
parent.property === idPath.node &&
!parent.computed
) return;
)
return;
// Skip import specifiers from other packages
if (
parent.type === "ImportSpecifier" &&
idPath.parent.parent?.node !== path.node
) return;
)
return;
// If a local declaration shadows this name, only rename
// unambiguous type-position references (e.g. type annotations)
@@ -126,9 +153,11 @@ export default function transform(file: FileInfo, api: API) {
// Only rename JSX identifiers if there's no shadow
if (!hasShadow) {
root.find(j.JSXIdentifier, { name: localName }).forEach((idPath) => {
idPath.node.name = newName;
});
root
.find(j.JSXIdentifier, { name: localName })
.forEach((idPath) => {
idPath.node.name = newName;
});
}
if (spec.local) {