Skip to content

Instantly share code, notes, and snippets.

@abiriadev
Created March 21, 2024 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abiriadev/409596191264b3a558b5103247d75f1b to your computer and use it in GitHub Desktop.
Save abiriadev/409596191264b3a558b5103247d75f1b to your computer and use it in GitHub Desktop.
print typescript ast
import {
Node,
ScriptTarget,
SourceFile,
SyntaxKind,
createSourceFile,
} from 'typescript'
import { readFile } from 'node:fs/promises'
const rec = (node: Node, src: SourceFile, d: number) => {
console.log(
`${' '.repeat(d)}${SyntaxKind[node.kind]}: ${node.getText(src)}`,
)
node.forEachChild(ch => rec(ch, src, d + 1))
}
;(async () => {
const filename = __filename
const src = (await readFile(filename)).toString()
const sourceFile = createSourceFile(
filename,
src,
ScriptTarget.Latest,
)
rec(sourceFile, sourceFile, 0)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment