2017-01-23 14:48 GMT+01:00 Yuriy Tymchuk <yuriy.tymc...@me.com>: > Hi everyone, > > does anyone have a better knowledge about replacing nodes in AST? Because > what I saw is that there are methods like replaceSourceWith: that can be > sent to an AST node with another AST node as a parameter. But this is not > enough to get a new source code. Because when you ask the AST for newSource > it generates a new source by taking replacements into account, parses the > new source, compares it with itself and in case the new parse tree is equal > it returns the new source. So it’s not enough to just use > replaceSourceWith: but you also have to replace the current node with a new > one it AST. > > Now the question is: am I doing something wrong, or we have strange API > because we need to replace both source and node, replacing only one of them > does not work… >
The API is strange : ) The nodeMapping and replacements are used by the tree rewriter. And the way the treerewriter works, it does not rewrite a AST-Tree but generates a new method by compiling the returned value, and it works on the returned value, the new/reformatted source text, not on a changed AST-Tree. >From the rewriter point of view (and some other parts of RB), the AST is more like a immutable structure. Only since when we started to use the RB-AST for the compiler, we started to use the AST for other purposes (remember when we got into trouble, because some lint rules from critic browser were modifying the AST and we used that modified source as the method source, although the actual method source didn't changed ) > > Uko >