dsherret / dsherret/ts-morph

node.setOrder() throws ManipulationError if comment is present

Open
#1,462 3 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
6.2k
Forks
238
Avg merge
2m
Merged PRs (30d)
1

Description

**Describe the bug**

Version: 20.0.0
Node.js v20.5.0
TS Node: 10.9.1

**To Reproduce**

Save this file as `bug.mts`:

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

const project = new Project();
const sourceFile = project.createSourceFile(
"example.ts",
`class A {
constructor() {}
// IF I REMOVE THIS COMMENT, IT WORKS OK

/** A */
get disabled(): boolean {
return true;
}
}
`
);

const myClass = sourceFile.getClassOrThrow("A");

// CRASHES HERE:
myClass.getGetAccessorOrThrow("disabled").setOrder(0);
```

Run it:

```sh
node --no-warnings=ExperimentalWarning --loader ts-node/esm/transpile-only ./bug.mts
```

See the error:

```yaml

/node_modules/ts-morph/dist/ts-morph.js:2325
throw new ManipulationError(sourceFile.getFilePath(), oldFileText, newFileText, message);
^

ManipulationError: Manipulation error: Error replacing tree! Perhaps a syntax error was inserted (Current: GetAccessor -- New: SingleLineCommentTrivia).

-- Details --
Path: /example.ts
Text: "class A {\n // IF I REMOVE THIS COMMENT, IT WORKS OK\n\n /** A */\n get disabled(): boolean {\n return true;\n }\n\n constructor() {}\n}\n"
Stack: Error: Error replacing tree! Perhaps a syntax error was inserted (Current: GetAccessor -- New: SingleLineCommentTrivia).
at StraightReplacementNodeHandler.handleNode (/node_modules/ts-morph/dist/ts-morph.js:1453:19)
at NodeHandlerHelper.handleForValues (/node_modules/ts-morph/dist/ts-morph.js:1400:21)
at ChangeChildOrderParentHandler.handleNode (/node_modules/ts-morph/dist/ts-morph.js:1484:25)
at ParentFinderReplacementNodeHandler.handleNode (/node_modules/ts-morph/dist/ts-morph.js:1594:36)
at NodeHandlerHelper.handleForValues (/node_modules/ts-morph/dist/ts-morph.js:1400:21)
at ParentFinderReplacementNodeHandler.handleChildren (/node_modules/ts-morph/dist/ts-morph.js:1467:25)
at ParentFinderReplacementNodeHandler.handleNode (/node_modules/ts-morph/dist/ts-morph.js:1457:18)
at ParentFinderReplacementNodeHandler.handleNode (/node_modules/ts-morph/dist/ts-morph.js:1597:19)
at NodeHandlerHelper.handleForValues (/node_modules/ts-morph/dist/ts-morph.js:1400:21)
at ParentFinderReplacementNodeHandler.handleChildren (/node_modules/ts-morph/dist/ts-morph.js:1467:25)
at throwError (/node_modules/ts-morph/dist/ts-morph.js:2325:19)
at doManipulation (/node_modules/ts-morph/dist/ts-morph.js:2323:9)
at changeChildOrder (/node_modules/ts-morph/dist/ts-morph.js:2593:5)
at GetAccessorDeclaration.setOrder (/node_modules/ts-morph/dist/ts-morph.js:5889:13)
at file:////bug.mts:15:43
at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
at async CustomizedModuleLoader.import (node:internal/modules/esm/loader:228:24)
at async loadESM (node:internal/process/esm_loader:40:7)
at async handleMainPromise (node:internal/modules/run_main:66:12) {
filePath: '//example.ts',
oldText: 'class A {\n' +
' constructor() {}\n' +
' // IF I REMOVE THIS COMMENT, IT WORKS OK\n' +
'\n' +
' /** A */\n' +
' get disabled(): boolean {\n' +
' return true;\n' +
' }\n' +
'}\n',
newText: 'class A {\n' +
' // IF I REMOVE THIS COMMENT, IT WORKS OK\n' +
'\n' +
' /** A */\n' +
' get disabled(): boolean {\n' +
' return true;\n' +
' }\n' +
'\n' +
' constructor() {}\n' +
'}\n'
}
```

(Possible cause) It looks like the code has a check for whether manipulation caused a bug, and that check results in a false positive when a comment is present:

![Screenshot 2023-10-17 at 14 31 39](https://github.com/dsherret/ts-morph/assets/40512816/17295a4a-6457-492b-b654-096691e22986)

**Expected behavior**

Node is moved without an error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read CONTRIBUTING.md, then reproduce the failure with the bug.mts example and trace GetAccessorDeclaration.setOrder through the manipulation code shown in the stack. Verify the fix by rerunning the example with the comment present and confirming the accessor moves without a ManipulationError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.