ts-morph icon indicating copy to clipboard operation
ts-morph copied to clipboard

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

Open KeterSCP opened this issue 1 year ago • 0 comments

Describe the bug See the repro

Version: 20.0.0

To Reproduce

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);

KeterSCP avatar Oct 16 '23 16:10 KeterSCP