I'm trying to implement the following "macros" transformer in TypeScript. I'd like to expand a definition like:
class SourceClass {
@lazy
lazyProperty : Map<number, string> = new Map()
}
to:
class SourceClass {
$lazyProperty : Map<number, string> = undefined
get lazyProperty () : Map<number, string> {
if (this.$lazyProperty !== undefined) return this.$lazyProperty
return this.$lazyProperty = new Map()
}
set lazyProperty (value : Map<number, string>) {
this.$lazyProperty = value
}
}
And I want to make the $lazyProperty property to be available for the rest of the program (to be able to check the lazy value presence). So, for example, I'd like this code to typecheck correctly, even that $lazyProperty is generated in the "macros":
class SourceClass {
@lazy
lazyProperty : Map<number, string> = new Map()
method () {
if (this.$lazyProperty !== undefined) { ... }
}
}
For that I need the transformation to happen before the type-checking stage in compiler's pipeline, which, as I figured out after some research, is not how the regular transformers works.
However, if I'm correct, there are various workarounds, the most advanced is ts-morph, but I'm looking for something that is compatible with ts-patch and preferable something that integrates well with the IDE language services.
Can someone experienced with this kind of transformers outline the implementation plan? Perhaps there's a working example somewhere? (there has to be one, macros transformation should be a quite common requirement).
So far what I found is something related to overriding the CompilerHost: https://stackoverflow.com/a/59147818/365104 but its far from complete example.
Thanks a lot!