Hi,
I am fairly new to static analysis in general and am trying to find a good place to start with the dart analyzer. I found a couple of conversations here about this, but they are several years old and the repo has clearly changed since then. I have 2 main questions: first, how does the analyzer work in general (the modification part), and second, how to extend the analyzer's code factorization for custom edits.
Around a year ago I attempted to write a program that would dynamically modify dart code. I did this by creating a handwritten parser for dart (v2.2) which could convert itself back to source code. I then created several wrapper libraries which understood how to modify the parser and thus create modified source code. This actually worked quite well. However, about 6 months ago this stopped being very useful or reliable given several updates to the dart grammar.
Thus, for a short while (have since become busy with other things) I started worked on creating a program to automatically generate a parser (similar to ANTLR) to avoid future grammar inconsistencies. Now I'm beginning to work on this problem again and realized I should probably just extend the functionality of the Analyzer since it already seems to do what I want - but I would like to know in greater detail about how the dart analyzer actually works. While looking in this group, I found the following quote:
"I'm still curious as to why you want to modify the AST. If you're doing it to edit files, our experience is that that generally isn't the best way (from a user's perspective) to do so. And we have some generalized support for editing files (used for quick fixes and refactorings) that might be of use to you."
As well as:
"Many people assume that the best way to write code editing applications is to build an AST, modify the AST, then write the AST out as source code. But after many decades of experience writing such tools we have come to realize that it's actually much easier to modify the source code directly and then rebuild the AST from source in order to continue editing."
-----------------------------------------------------------------
So I'm clearly missing something when it comes to modifying source code if it is easier to NOT modify the AST. What I'm wondering is how is it possible to reliably refactor code without using the AST? Or is the AST still used in some helper role like identifying locations and then just updating the locations? I'm especially curious because I actually had a fairly positive/successful experience creating libraries to modify my custom AST - issues arose because of the inflexibility of updating my parser by hand.
So that's kinda my general how does it work question :). Secondly, I'd love to know if there are any examples of custom analyzer code so I can start understanding how to use the analyzer to modify source code. Does it use the AST or not?
Sorry for such a long post, I'm just very curious about this and trying to find a better way to accomplish my program now that I'm starting to get into it again - cheers!
-----------------------------------------------------------------
TLDR;
1. How does the analyzer modify code if it doesn't modify the AST? Or if it does, where does it do that?
2. Where to get started with creating custom refactor code using the analyzer?
Thanks!
Isaac