itsdouges/typescript-transformer-handbook

Usage question: Modifying Nodes & Symbols

LukasMachetanz opened this issue · 1 comments

Hey!

Let's assume that I want to modify specific classes with my custom typescript transformer. The actual use case does not matter for this question but e.g. changing all method declarations to property declarations.

Therefore I collect all the targeted class declaration symbols before the actual transformation starts. I already achieved this. Therefore I do not want to get into detail here. Just assume that there is an array of symbols representing the transformer targets - class declaration symbols.

With this setup I can do something like the following in my visitor:

if (ts.isClassDeclaration(node) && isTransformerTarget(node)) {
   if (ts.isMethodDeclaration(node)) {
      // return new property declaration instead
   }
}

Now asking the actual question: Does the symbol of the class declaration change after modify the corresponding class declaration?

I am asking because my setup gets a little bit more specific: I am composing multiple transformers like explained in the handbook. And actually I am doing a similar thing in the next transformer. The real use case does not matter again. But for the sake of the example let's assume modifying the constructor of the class declaration with the next transformer.

if (ts.isClassDeclaration(node) && isTransformerTarget(node)) {
   if (ts.isConstructorDeclaration(node)) {
      // add something to the constructor and return the updated node 
   }
}

Here the previously asked question gets visible for me. I am not able to enter the first if-statement anymore. Exploring my source code lead me to the assumption that the symbol must have changed after applying the first transformer. Therefore the function isTransformerTarget(node) would return false now. This is maybe responsible for not entering the block anymore. Do do know what I mean?

Therefore again summarizing the actual questions:

  • Does the symbol of a node change after modifying the corresponding node (or children)?
  • Would there be a way to prevent this? E.g. I would assume that functions like ts.updateClassDeclaration would not change the symbol? Or does it make sense that the symbol changes and a check like symbol1 === symbol2 does not succeed anymore? I maybe would guess so.
  • But is it simply wrong then to compare the whole symbol in my custom function isTransformerTarget - like symbol1 === symbol2. I saw that this was done in the transformer handbook. Therefore I also tried this approach. Maybe I just have to compare a property indicating uniqueness of the symbol - like id. Any thoughts/ideas on this?

Hopefully my explanation and questions make any sense. I would appreciate any thoughts on this topic considering symbols.

Thanks in advance,
Lukas

hey mate i'm not immediately sure to be honest - how did you go with this? did you find any new information we could add to the handbook?