UNPKG

@reyalp/debug-utils

Version:

ts transformers for debug

100 lines (70 loc) 1.64 kB
# debug-utils debug utilities via typescript transformer ## `source_location` main.ts: ``` typescript import { source_location } from '@reyalp/debug-utils' function foo() { function bar() { console.log(source_location.file.fullname) console.log(source_location.line) console.log(source_location.sympath) console.log(source_location.up.sympath) use_source_location('hello') } } function use_source_location(v: string, caller = source_location.up.sympath) { console.log(v, caller) } ``` transpiles to: ``` javascript function foo() { function bar() { console.log('main.ts') console.log(6) console.log('foo.bar') console.log('foo') use_source_location('hello', 'foo.bar') } } function use_source_location(v, caller) { console.log(v, caller) } ``` ## `introspect`, `quoteval` ``` typescript import { introspect, } from '@reyalp/debug-utils' const [expr, val] = introspect(1 + 2 + 3) console.log(quoteval(1 + 2 + 3)) ``` transpiles to: ``` javascript const [expr, val] = ['1 + 2 + 3', 1 + 2 + 3] console.log(`1 + 2 + 3 = ${1 + 2 + 3}`) ``` ## Logger main.ts ``` typescript import { Logger } from '@reyalp/debug-utils' const logger = Logger.create() function foo() { logger.info('hello').dump(1 + 2 + 3) } foo() ``` prints: ``` 01-29 12:57:30.771 I main:6 foo: hello 1 + 2 + 3 = 6 ``` ## `tsconfig.json` setup see [ts-patch](https://github.com/nonara/ts-patch) ``` json { "compilerOptions": { "plugins": [ { "transform": "@reyalp/debug-utils/lib/transform" } ] } } ```