-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.ts
32 lines (25 loc) · 925 Bytes
/
run.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import * as ts from "typescript"
const hl = (x: string) => `\u001b[94;1;40m${x}\u001b[0m`
const run = (file: string) => {
const prg = ts.createProgram([file], {})
const src_file = prg.getSourceFile(file)
if (!src_file) {
console.error(`typescript borked`)
process.exit(1)
}
const checker = prg.getTypeChecker()
ts.forEachChild(src_file, node => {
if (!ts.isTypeAliasDeclaration(node) || !node.name) return
const pos = src_file.getLineAndCharacterOfPosition(node.pos)
const symbol = checker.getSymbolAtLocation(node.name)
const symbol_type = checker.getWidenedType(checker.getDeclaredTypeOfSymbol(symbol!))
const type_string = checker.typeToString(
symbol_type,
undefined,
ts.TypeFormatFlags.NoTruncation
)
const binding = node.getChildAt(1).getText()
console.log(`line ${pos.line}:${hl(binding)} -> ${hl(type_string)}`)
})
}
run(process.argv[2])