Slide 19
Slide 19 text
import ts, { Node, ScriptTarget } from "typescript";
const code = `
export const message: string = 'hello world';
`;
const sourceFile = ts.createSourceFile(
"src/index.ts",
code,
ScriptTarget.ES2022
);
const visitor = (depth: number) => (node: Node) => {
console.log(
`${Array(depth).fill("-").join("")}> [${
ts.SyntaxKind[node.kind]
}] ${node.getText(sourceFile)}`
);
ts.forEachChild(node, visitor(depth + 1));
};
ts.forEachChild(sourceFile, visitor(1));