Ast Grep: The CLI Treesitter for Efficient Code Pattern Searching
Ast-grep is a command-line tool that allows you to search for specific patterns in source code files. It is particularly useful for developers who want to find specific code patterns within their codebase.
For example, let's say you are working on a large software project and you need to find all instances where a particular function is being called. You can use ast-grep to search through all the source code files in the project and quickly locate all the places where that function is being used. This can save you a lot of time and effort compared to manually searching through each file.
Install
When it comes to installation, a common approach is to install ast-grep using Python's pip within a virtual environment.
pip install ast-grep-cli
The PowerShell shell script (vox) described here can assist in creating new virtual environments. This script saves time and reduces the potential for errors, making it an invaluable tool for developers.
Grep Code Patterns
As demonstrated in the video below, a particular pattern within the entire code can be located either from the command line or within vim. The latter method uses a specific vim9script plugin.
Rules
In JavaScript, it is generally recommended not to leave console.log, console.error, or debugger statements in your code when deploying it to production. These statements are typically used for debugging purposes during development to log information or track errors. Leaving them in production code can potentially create unnecessary overhead in terms of performance. It's best practice to remove or comment out these statements before deploying your code to ensure a cleaner and more secure production environment.
The ast-grep scan command allows you to execute multiple rules on your repository without having to input the pattern query each time. This streamlines the process, making it more efficient and less repetitive.
To utilize the scan functionality of ast-grep, you must set up a configuration file for your project. This configuration file will define the rules and settings for the scan. Additionally, you can create a dedicated folder containing a set of rules defined within several YAML files. This can be done using the command line with the following commands:
ast-grep new
ast-grep new rule
Below the rule used as an example in the video.
# yaml-language-server: $schema=https://raw.githubusercontent.com/ast-grep/ast-grep/main/schemas/rule.json
id: no-console
message: Remove or comment console.log
severity: info
language: TypeScript
rule:
pattern: console.log($MSG)
Vim LSP
Ultimately, it is also possible to use ast-grep as a Language Server Protocol (LSP) in Vim. This integration allows code lines that match the defined rules to be immediately highlighted within the editor, providing instant feedback and enhancing your coding workflow
Below the configuration settings required by yegappan/lsp client.
...
\#{
\ name: 'ast-grep lsp',
\ filetype: ['typescript'],
\ path: 'C:\\Users\\Seve\\workspace\\toolbox\\pyvenv\\astgrep\\Scripts\\ast-grep.exe',
\ args: ['lsp'],
\ initializationOptions: {},
\ },
...