You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Develop new ast-grep rules and test them: `ast-grep test`.
11
+
* Start Language Server for editor integration: `ast-grep lsp`.
12
12
13
13
We will walk through some important features that are common to these commands.
14
14
@@ -26,7 +26,7 @@ Pressing `y` will accept the rewrite, `n` will skip it, `e` will open the file i
26
26
Example:
27
27
28
28
```bash
29
-
sg scan --interactive
29
+
ast-grep scan --interactive
30
30
```
31
31
32
32
## JSON Mode
@@ -36,7 +36,7 @@ Composability is a key perk of command line tooling. ast-grep is no exception.
36
36
`--json` will output results in JSON format. This is useful to pipe the results to other tools. For example, you can use [jq](https://stedolan.github.io/jq/) to extract information from the results and render it in [jless](https://jless.io/).
The format of the JSON output is an array of match objects.
@@ -71,18 +71,18 @@ See [JSON mode doc](/guide/tools/json.html) for more detailed explanation and ex
71
71
## Run One Single Query or One Single Rule
72
72
73
73
You can also use ast-grep to explore a proper pattern for your query. There are two ways to try your pattern or rule.
74
-
For testing one pattern, you can use `sg run` command.
74
+
For testing one pattern, you can use `ast-grep run` command.
75
75
76
76
```bash
77
-
sg run -p 'YOUR_PATTERN' --debug-query
77
+
ast-grep run -p 'YOUR_PATTERN' --debug-query
78
78
```
79
79
80
80
The `--debug-query` option will output the tree-sitter ast of the query.
81
81
82
-
To test one single rule, you can use `sg scan -r`.
82
+
To test one single rule, you can use `ast-grep scan -r`.
83
83
84
84
```bash
85
-
sg scan -r path/to/your/rule.yml
85
+
ast-grep scan -r path/to/your/rule.yml
86
86
```
87
87
It is useful to test one rule in isolation.
88
88
@@ -94,11 +94,11 @@ You can use bash's [pipe operator](https://linuxhint.com/bash_pipe_tutorial/) `|
94
94
95
95
### Example: Simple Web Crawler
96
96
97
-
Let's see an example in action. Combining with `curl`, `ast-grep` and `jq`, we can build a [simple web crawler](https://twitter.com/trevmanz/status/1671572111582978049) on command line. The command below uses `curl` to fetch the HTML source of SciPy conference website, and then uses `sg` to parse and extract relevant information as JSON from source, and finally uses `jq` to transform our matching results.
97
+
Let's see an example in action. Combining with `curl`, `ast-grep` and `jq`, we can build a [simple web crawler](https://twitter.com/trevmanz/status/1671572111582978049) on command line. The command below uses `curl` to fetch the HTML source of SciPy conference website, and then uses `ast-grep` to parse and extract relevant information as JSON from source, and finally uses `jq` to transform our matching results.
@@ -121,15 +121,15 @@ The command above will produce a list of authors from the SciPy 2022 conference
121
121
122
122
With this feature, even if your preferred language does not have native bindings for ast-grep, you can still parse code from standard input (StdIn) to use ast-grep programmatically from the command line.
123
123
124
-
You can invoke sg, the command-line interface for ast-grep, as a subprocess to search and replace code.
124
+
You can invoke `ast-grep`, the command-line interface for ast-grep, as a subprocess to search and replace code.
125
125
126
126
### Caveats
127
127
128
128
**StdIn mode has several restrictions**, though:
129
129
130
130
* It conflicts with `--interactive` mode, which reads user responses from StdIn.
131
-
* For the `run` command, you must specify the language of the StdIn code with `--lang` or `-l` flag. For example: `echo "print('Hello world')" | sg run --lang python`. This is because ast-grep cannot infer code language without file extension.
132
-
* Similarly, you can only `scan` StdIn code against _one single rule_, specified by `--rule` or `-r` flag. The rule must match the language of the StdIn code. For example: `echo "print('Hello world')" | sg scan --rule "python-rule.yml"`
131
+
* For the `run` command, you must specify the language of the StdIn code with `--lang` or `-l` flag. For example: `echo "print('Hello world')" | ast-grep run --lang python`. This is because ast-grep cannot infer code language without file extension.
132
+
* Similarly, you can only `scan` StdIn code against _one single rule_, specified by `--rule` or `-r` flag. The rule must match the language of the StdIn code. For example: `echo "print('Hello world')" | ast-grep scan --rule "python-rule.yml"`
133
133
134
134
### Enable StdIn Mode
135
135
@@ -155,7 +155,7 @@ ast-grep will hang there if you run it in a tty terminal session with `--stdin`
155
155
Here is a bonus example to use [fzf](https://github.com/junegunn/fzf/blob/master/ADVANCED.md#using-fzf-as-interactive-ripgrep-launcher) as interactive ast-grep launcher.
@@ -172,12 +172,12 @@ See the [editor integration](/guide/tools/editors.md) doc page.
172
172
173
173
## Shell Completions
174
174
ast-grep comes with shell autocompletion scripts. You can generate a shell script and eval it when your shell starts up.
175
-
The script will enable you to smoothly complete `sg` command's options by `tab`bing.
175
+
The script will enable you to smoothly complete `ast-grep` command's options by `tab`bing.
176
176
177
177
This command will instruct ast-grep to generate shell completion script:
178
178
179
179
```shell
180
-
sg completions <SHELL>
180
+
ast-grep completions <SHELL>
181
181
```
182
182
183
183
`<SHELL>` is an optional argument and can be one of the `bash`, `elvish`, `fish`, `powershell` and `zsh`. If shell is not specified, ast-grep will infer the correct shell from environment variable like `$SHELL`.
@@ -189,7 +189,7 @@ The exact steps required to enable autocompletion will vary by shell. For instru
189
189
If you are using zsh, add this line to your `~/.zshrc`.
@@ -200,7 +200,7 @@ If you want to automate [ast-grep linting](https://github.com/marketplace/action
200
200
201
201
For example, you can run ast-grep linting every time you push a new commit to your main branch.
202
202
203
-
To use ast-grep in GitHub Action, you need to [set up a project](/guide/scan-project.html) first. You can do this by running `sg new` in your terminal, which will guide you through the process of creating a configuration file and a rules file.
203
+
To use ast-grep in GitHub Action, you need to [set up a project](/guide/scan-project.html) first. You can do this by running `ast-grep new` in your terminal, which will guide you through the process of creating a configuration file and a rules file.
204
204
205
205
Next, you need to create a workflow file for GitHub Action. This is a YAML file that defines the steps and actions that will be executed when a certain event occurs. You can create a workflow file named `ast-grep.yml` under the `.github/workflows/` folder in your repository, with the following content:
206
206
@@ -219,7 +219,7 @@ jobs:
219
219
```
220
220
221
221
This workflow file tells GitHub Action to run ast-grep linting on every push event, using the latest Ubuntu image and the official ast-grep action.
222
-
The action will check out your code and run [`sg scan`](/reference/cli.html#sg-scan) on it, reporting any errors or warnings.
222
+
The action will check out your code and run [`ast-grep scan`](/reference/cli.html#sg-scan) on it, reporting any errors or warnings.
223
223
224
224
That's it! You have successfully set up ast-grep linting in GitHub Action. Now, every time you push a new commit to your main branch, GitHub Action will automatically run ast-grep linting and show you the results. You can see an example of how it looks like below.
0 commit comments