On 01/23/2017 07:48 AM, Yuriy Tymchuk wrote:
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…


This code was created so that some refactoring could be performed without having to format the entire parse tree. For example, converting direct variable access to use accessors should be able to rewrite the code without having to reformat.

When you perform the replaceWith: on a node or the rewriter performs a node replacement, it calls the replaceMethodSource: method. That method calls the replaceSourceWith: method (replaceSourceWith: is a "private" method). The replaceSourceWith: methods test the source/replacement nodes for certain patterns. If the replacement fits the condition of the pattern, then it adds the appropriate text edit operations that should transform the old source into the new source. If the replaceSourceWith: doesn't recognize what is being done, then it dispatches the replaceSourceFrom: message to the replacement node. Currently, this handles cases were any section of code is being replaced with a variable or literal.

Anyway, after all replacements are performed, it gets the new source like you explain. It performs a sanity check on the edited source. If it isn't parse tree equivalent to the rewritten tree, then it assumes that something went wrong in a replaceSourceWith: or replaceSourceFrom: method and uses the formatted code of the rewritten tree instead.


John Brant

Reply via email to