Powerfully autofixing code with Semgrep's new AST-based approach
Blog post from Semgrep
Semgrep, a code searching tool supporting over 20 languages, has enhanced its autofix capability by transitioning from a text-based approach to an Abstract Syntax Tree (AST)-based method, allowing for more accurate code corrections. Unlike the previous method that often resulted in incorrect code due to simple text replacement, the new approach involves parsing the fix into an AST, replacing metavariables within the AST, and printing the AST back to text, which helps maintain the original code's formatting and comments. This transition not only improves the accuracy of autofixes, particularly for Python and JavaScript/TypeScript with high correctness rates, but also sets the stage for future enhancements without requiring the development of individual printers for each language. While this new method is currently available for expressions in Python and JavaScript/TypeScript, ongoing improvements aim to expand its applicability and accuracy across more languages.