Skip to content

Fix renameTo renaming TypeScript and Flow type member keys - #760

Open
kevin9327 wants to merge 1 commit into
meta:mainfrom
kevin9327:fix/rename-to-type-member-keys
Open

kevin9327 wants to merge 1 commit into
meta:mainfrom
kevin9327:fix/rename-to-type-member-keys

Conversation

@kevin9327

Copy link
Copy Markdown

Problem

VariableDeclarator#renameTo renames the key of a type member that happens to share the variable's name. That changes the type's shape:

const j = require('jscodeshift').withParser('ts');
const root = j('const foo = 1;\ninterface X { foo: string }\ntype T = { foo: number };\nenum E { foo }');
root.findVariableDeclarators('foo').renameTo('bar');
console.log(root.toSource());
const bar = 1;
interface X { bar: string }      // expected: foo
type T = { bar: number };        // expected: foo
enum E { bar }                   // expected: foo

The same happens with --parser=flow for type T = { foo: number }, whose key is an ObjectTypeProperty.

The filter already skips the keys of Property, ObjectProperty, ObjectMethod, ClassProperty and the like, but none of the type-level members.

Fix

Also skip a non-computed key of TSPropertySignature, TSMethodSignature and ObjectTypeProperty, and the id of a TSEnumMember. A computed key such as interface X { [foo]: string } refers to the variable and is still renamed.

Tests

New type members cases in src/collections/__tests__/VariableDeclarator-test.js (TS property/method signatures and type literal, TS enum, Flow object type, computed key). The first three fail without the fix (the keys come out as newFoo), and all pass with it. yarn test: 225 passed, 1 skipped. Added a patch changeset.

Not covered by #753 (shorthand properties) or #757 (export specifiers).

This PR was prepared with AI assistance; I reviewed the change and ran the tests.

@changeset-bot

changeset-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: e8ceaf1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
jscodeshift Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@vercel

vercel Bot commented Oct 31, 2026

Copy link
Copy Markdown

@kevin9327 is attempting to deploy a commit to the Meta Open Source Team on Vercel.

A member of the Team first needs to authorize it.

@meta-cla meta-cla Bot added the CLA Signed label Oct 31, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant