question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Any example of macros-like, source-modifying transformer?

See original GitHub issue

Hello,

I’m trying to implement the following “macros-transformer”.

I’d like to expand a definition like:

class SourceClass {
    @lazy
    lazyProperty    : Map<number, string>       = new Map()
}

to:

class TransformedClass {
    $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 to be available for the rest of the program (to be able to check the lazy value presence).

For that I need the transformation to happen before the type-checking stage in compiler’s pipeline. Is there any example of similar transformer? Is this something that can be done with the program transformer?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
canonic-epicurecommented, Jun 24, 2020

Thank you! It seems this task is more complex than I thought initially. However, as you mentioned, it seems that it is doable. It is very tempting to implement it - the possibilities looks endless. I’ll be making attempts from time to time. Hopefully one day I’ll submit a PR with support for type : 'macros' transformers 😃

0reactions
nonaracommented, Jun 23, 2020

There is no native way to do it. Even with what cevek mentioned, the language service in the IDE will still report errors, because it’s entirely separate from the TSC process.

What you’re talking about goes beyond transformer capability and gets into the territory of extending the typescript compiler itself. This leaves you with the options of forking typescript or creating a dual language-service + transformer scenario using either program transformer or source transformer.

  1. With the program transformer, you could avoid altering diagnostics in tsc, however, you’d need to find a way to manually update the generated sourcemaps. This would mean studying how they’re built so you could make sure they matched the original source instead of your updated source. That would ensure debugging works smoothly.

  2. With source transformer, your sourcemaps would be fine, as they’d be generated by tsc during the transform process. After that, you’d simply need to find and remove relevant diagnostic errors…

When you’ve accomplished either of those routes, you’ll still need to remove diagnostics via a language service plugin, assuming that is still possible.

Believe it or not, while it feels hacky, that type of methdology is employed by Angular and others to add in their extensions without forking the compiler. If you think about it, TypeScript has two parts. 1) A compiler, 2) A Diagnostics reporter. Transformation can take care of the compiler side, and altering the results of the diagnostics report takes care of the other. This affords you the ability to alter TS behaviour without forking it.

Officially, TypeScript doesn’t support changing/adding to diagnostics. Unofficially, it’s built in a way that allows it to be done, which things like Angular rely on.

That said, if you prefer the behaviour to be a part of the TypeScript compiler, itself, you’d probably be better served forking.

I wish you luck! What you’re hoping to do isn’t easy, but it’s not impossible. If you decide to pursue it, you will likely be spending a lot of time in the TS compiler base. At this level, you’re not likely to find any tutorials or answers to questions along the way from StackOveflow, etc. It’s even rare to get answers in the #compiler discord room, which is where the few people who work in the compiler tend to congregate. This is simply because there are very few people who know it well enough. So whatever you take on in extending compiler functionality will be mostly solo pioneering! Just a heads up if you decide to take it on.

Again - best of luck.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Top 6 Interview Questions on Transformer - Analytics Vidhya
The article covers interview-winning questions to help you become more familiar with the transformer model and ace your next interview!
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found