dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
5.03k stars 196 forks source link

Unable to rename bindingElement with usePrefixAndSuffixTextForRename:true #1488

Open huntye1 opened 10 months ago

huntye1 commented 10 months ago

Describe the bug

Version: 21.0.1

To Reproduce

import { Project, ts } from "ts-morph";

const project = new Project({
  manipulationSettings: {
    usePrefixAndSuffixTextForRename: true,
  },
});

const sourceFile = project.createSourceFile(
  "foo.ts",
  `const fn = ({ p, baz }) => {
    set({ p, baz });
  };
  `
);

const fns = [...sourceFile.getDescendantsOfKind(ts.SyntaxKind.ArrowFunction)];

fns.forEach((fn) => {
  const bindingElements = fn.getDescendantsOfKind(ts.SyntaxKind.BindingElement);
  bindingElements.forEach((b) => {
    b.rename("inner" + b.getName());
  });
});

Expected behavior

expect output code:

const fn = ({ p: innerP, baz: innerBaz }) => {
    set({ p: innerP, baz: innerBaz) });
  };

Actrual behavior

node_modules/.pnpm/ts-morph@21.0.1/node_modules/ts-morph/dist/ts-morph.js:3042
            throw new common.errors.InvalidOperationError(message);
                  ^
InvalidOperationError: Attempted to get information from a node that was removed or forgotten.

Node text: baz
    at BindingElement.get compilerNode [as compilerNode] (node_modules/.pnpm/ts-morph@21.0.1/node_modules/ts-morph/dist/ts-morph.js:3042:19)
    at BindingElement.getNameNode (node_modules/.pnpm/ts-morph@21.0.1/node_modules/ts-morph/dist/ts-morph.js:9467:55)
    at BindingElement.getName (node_modules/.pnpm/ts-morph@21.0.1/node_modules/ts-morph/dist/ts-morph.js:9470:25)
    at test/ts-m.ts:22:26
    at Array.forEach (<anonymous>)
    at test/ts-m.ts:21:20
    at Array.forEach (<anonymous>)
    at Object.<anonymous> (test/ts-m.ts:19:5)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
    at Module.m._compile (node_modules/.pnpm/ts-node@10.9.2_@types+node@20.11.4_typescript@5.3.3/node_modules/ts-node/src/index.ts:1618:23)