Replies: 1 comment
-
I agree this would be super cool. I'm using @swc/core now to parse my ts files on the fly. It's super fast but |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It would be nice to have easy access to the AST of a source file.
Approaches
Expose AST as variable
Maybe something like:
import.meta.ast
...Allow passing AST nodes to macros
Might also be cool to be able to do reflection really simply, by passing in AST nodes as input to macros...
E.g.
Tricky though to indicate when you want to pass a value vs AST node.
Workarounds
You can use 3rd party AST parsing libraries such as:
ts-morph
Bun Macro
an-app-source-file.ts
./my-ast-parser.macro.ts
Bun Plugin
You essentially end up re-implementing macros.
Not shown here, but you would normally parse comments to indicate what code should be run/inserted.
./my-ast-parser.ts
index.ts
Notes
There is an existing issue to expose AST via
Bun.Transpiler
: #2729. This would be a first step...Similar stuff
Type reflection
Good discussion: https://news.ycombinator.com/item?id=36637221
reflect-metadata
https://github.com/rbuckton/reflect-metadata
Uses
@Reflect.metadata
decorator proposal (not from TypeScript team).TypeScript has built-in support for this specific library to avoid needing to annotate specific methods...
From Typescript documentation: https://www.typescriptlang.org/docs/handbook/decorators.html#metadata
There are no function decorators though so this is sucky.
tst-reflect
https://github.com/Hookyns/tst-reflect
typescript-rtti
https://github.com/typescript-rtti/typescript-rtti
Requires
ttypescript
(patching typescript), but is deprecated.Beta Was this translation helpful? Give feedback.
All reactions