dsherret / dsherret/ts-morph

`getDescendantAtStartWithWidth` does not work for reported diagnostic on exported functions

Open
#1,461 0 comments 0 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**
See the repro

Version: 20.0.0

**To Reproduce**

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

const project = new Project();

const inputCode = `namespace test {
export function addTwoNumbers(first: number, second: number) {
return first + second;
}

export function testBug() {
return test.addTwoNumbers(1);
}
}
`;

project.createSourceFile("main.ts", inputCode);

const diagnostics = project.getPreEmitDiagnostics();

for (const d of diagnostics) {
const sourceFile = d.getSourceFile()!;

const start = d.getStart();
const length = d.getLength();

if (start && length) {
// This is undefined
const errorNode = sourceFile.getDescendantAtStartWithWidth(start, length);
console.log(`Error node text: ${errorNode.getText()}`);
}
}

```

**Expected behavior**
`errorNode` should not be undefined

**Actual behavior**
`errorNode` is undefined
If you remove `export` from `addTwoNumbers`, it works

**Known workaround:**
Use `const errorNode = sourceFile.getDescendantAtPos(start);`

Contributor guide

Open the contributing guide

Research direction

Run the TypeScript reproduction from the issue and inspect the diagnostic returned by Project.getPreEmitDiagnostics(). Start at sourceFile.getDescendantAtStartWithWidth and compare it with sourceFile.getDescendantAtPos, using the exported and non-exported function cases. Done means the reported diagnostic resolves to a node instead of undefined for the exported-function case.

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.