Utilizing ts-morph for code analysis, I am attempting to retrieve the parent CallExpression
from a specific Identifier
location.
Despite using
.getParentWhileKind(SyntaxKind.CallExpression)
, the function is returning a null
value.
Why is this happening? I clearly have the relevant CallExpression
which should be the parent of the identified foo
.
What key element am I overlooking? And how can this issue be resolved? (aside from resorting to using multiple getParent()
calls.)
import { Identifier, Project, SyntaxKind } from "ts-morph";
console.clear();
const project = new Project();
const sourceFile = project.createSourceFile(
"test.ts",
`
const fn = () => {
chain.foo.bar('arg');
}
`
);
const a = sourceFile.getDescendants().find((d) => d.getText() === "foo");
console.log({ a: a?.getParentWhileKind(SyntaxKind.CallExpression) });