ts-morph
ts-morph copied to clipboard
Unable to rename bindingElement with usePrefixAndSuffixTextForRename:true
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/[email protected]/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/[email protected]/node_modules/ts-morph/dist/ts-morph.js:3042:19)
at BindingElement.getNameNode (node_modules/.pnpm/[email protected]/node_modules/ts-morph/dist/ts-morph.js:9467:55)
at BindingElement.getName (node_modules/.pnpm/[email protected]/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/[email protected]_@[email protected][email protected]/node_modules/ts-node/src/index.ts:1618:23)
```
<!-- A description of what you expected to happen. -->
I have the same issue. The problem seems that as soon as one binding element is renamed, the whole source file is somewhat regenerated and all existing node are forgotten. In you example, the first binding element would probably be properly renamed, but as soon as this is done, all nodes referenced in bindingElements would be forgotten, hence why subsequent call to rename fail.