From 31247f36631e586fbce5230791c869f35fede770 Mon Sep 17 00:00:00 2001 From: Alem Tuzlak Date: Tue, 7 Apr 2026 12:29:28 +0200 Subject: [PATCH] chore: format codemods files --- .../__tests__/migrate-attachments.test.ts | 4 +- codemods/migrate-attachments.ts | 59 ++++++++++++++----- 2 files changed, 47 insertions(+), 16 deletions(-) diff --git a/codemods/__tests__/migrate-attachments.test.ts b/codemods/__tests__/migrate-attachments.test.ts index 8dea1a3c3f..b7b125211c 100644 --- a/codemods/__tests__/migrate-attachments.test.ts +++ b/codemods/__tests__/migrate-attachments.test.ts @@ -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)"); }); diff --git a/codemods/migrate-attachments.ts b/codemods/migrate-attachments.ts index 896656c9d6..9110967085 100644 --- a/codemods/migrate-attachments.ts +++ b/codemods/migrate-attachments.ts @@ -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) {