I try to resolve foo and bar from nodes variable.
After I checked at ts-ast-viewer you can see in the picture that typescript can know about foo and bar from nodes node, under FlowNode section. (node -> initializer -> elements -> escapedText (foo, bar)).
But how I can access FlowNode?
I using ts-morph to work on my typescript ast code. and I already have the nodes Identifier. unfortunately I can't access to the properties I can see in the FlowNode section:
console.log({ n: nodes.node }); //<-- node not exist in nodes but I can see this in picture.
The full code:
import { Project, SyntaxKind } from "ts-morph";
console.clear();
const project = new Project({
skipAddingFilesFromTsConfig: true
});
const sourceFile = project.createSourceFile(
"foo.ts",
`
const items = [foo, bar];
const nodes = [...items];
`
);
const nodes = sourceFile
.getDescendantsOfKind(SyntaxKind.Identifier)
.find((n) => n.getText() === "nodes");
console.log({ n: nodes.node.initializer }); // <-- error: node is undefined
