dsherret / dsherret/ts-morph

Unable to rename bindingElement with usePrefixAndSuffixTextForRename:true

Open
#1,488 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

Version: 21.0.1

**To Reproduce**

```ts
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:
```ts
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 ()
at test/ts-m.ts:21:20
at Array.forEach ()
at Object. (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)
```

Contributor guide

Open the contributing guide

Research direction

Start by running the reproduction in test/ts-m.ts, focusing on BindingElement.rename with usePrefixAndSuffixTextForRename enabled. Trace the rename path reached by that example and verify that iterating over both binding elements does not access a removed node. Done means the example completes and produces the expected innerP and innerBaz references.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.