Skip to content

Fix renameTo renaming statement labels and private class members - #761

Open
kevin9327 wants to merge 1 commit into
meta:mainfrom
kevin9327:fix/rename-to-labels-private-names
Open

kevin9327 wants to merge 1 commit into
meta:mainfrom
kevin9327:fix/rename-to-labels-private-names

Conversation

@kevin9327

Copy link
Copy Markdown

Problem

VariableDeclarator#renameTo also renames identifiers that live in a different namespace from variables, when they share the variable's name:

const j = require('jscodeshift');
const root = j('var foo = 1;\nfoo: for (;;) { if (foo) break foo; else continue foo; }');
root.findVariableDeclarators('foo').renameTo('bar');
// var bar = 1;
// bar: for (;;) { if (bar) break bar; else continue bar; }
//   expected: foo: for (;;) { if (bar) break foo; else continue foo; }
const root = j.withParser('babylon')('const foo = 1;\nclass A { #foo = foo; m() { return this.#foo; } }');
root.findVariableDeclarators('foo').renameTo('bar');
// class A { #bar = bar; m() { return this.#bar; } }
//   expected: class A { #foo = bar; m() { return this.#foo; } }

Statement labels and private names never refer to a variable. Renaming them is harmless only as long as nothing else uses the same label or private name; it changes a class's private field names, and with a newName that is already used as a label in the enclosing code it turns valid code into a duplicate-label syntax error.

Fix

Skip an identifier that is the label of a LabeledStatement, BreakStatement or ContinueStatement, or the id of a PrivateName.

Tests

New other namespaces cases in src/collections/__tests__/VariableDeclarator-test.js (labels with the default parser, private members with babylon). Both fail without the fix (labels and #foo come out as newFoo) and pass with it; the variable references are still renamed. yarn test: 223 passed, 1 skipped. Added a patch changeset.

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

@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.

@changeset-bot

changeset-bot Bot commented Oct 31, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 863cafd

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

@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