Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions website/advanced/custom-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ gcc -shared -fPIC -fno-exceptions -g -I 'src' -o mojo.so -O2 src/scanner.cc -xc
## Register Language in `sgconfig.yml`

Once you have compiled the dynamic library for your custom language, you need to register it in the `sgconfig.yml` file.
You can use the command [`sg new`](/guide/scan-project.html#create-scaffolding) to create a project and find the configuration file in the project root.
You can use the command [`ast-grep new`](/guide/scan-project.html#create-scaffolding) to create a project and find the configuration file in the project root.

You need to add a new entry under the `customLanguages` key with the name of your custom language and some properties:

Expand Down Expand Up @@ -120,7 +120,7 @@ Now you are ready to use your custom language with ast-grep! You can use it as a
For example, to search for all occurrences of `print` in mojo files, you can run:

```bash
sg -p "print" -l mojo
ast-grep -p "print" -l mojo
```

Or you can write a rule in yaml like this:
Expand Down
2 changes: 1 addition & 1 deletion website/advanced/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ The encoding difference may cause different fallback parsing during [error recov
To debug the issue, you can use the [`--debug-query`](/reference/cli/run.html#debug-query-format) in the CLI to see the parsed AST nodes and meta variables.

```sh
sg run -p <PATTERN> --debug-query ast
ast-grep run -p <PATTERN> --debug-query ast
```

The debug output will show the parsed AST nodes and you can compare them with the [Playground](/playground.html). You can also use different debug formats like `cst` or `pattern`.
Expand Down
14 changes: 7 additions & 7 deletions website/advanced/language-injection.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Let's start with a simple example of searching for JavaScript and CSS within HTM
ast-grep has builtin support to search JavaScript and CSS inside HTML files.


### **Using `sg run`**: find patterns of CSS in an HTML file
### **Using `ast-grep run`**: find patterns of CSS in an HTML file

Suppose we have an HTML file like below:

Expand All @@ -43,7 +43,7 @@ Suppose we have an HTML file like below:
Running this ast-grep command will extract the matching CSS style code out of the HTML file!

```sh
sg run -p 'color: $COLOR'
ast-grep run -p 'color: $COLOR'
```

ast-grep outputs this beautiful CLI report.
Expand All @@ -55,7 +55,7 @@ test.html
ast-grep works well even if just providing the pattern without specifying the pattern language!


### **Using `sg scan`**: find JavaScript in HTML with rule files
### **Using `ast-grep scan`**: find JavaScript in HTML with rule files

You can also use ast-grep's [rule file](https://ast-grep.github.io/guide/rule-config.html) to search injected languages.

Expand All @@ -70,10 +70,10 @@ rule:
message: Prefer use appropriate custom UI instead of obtrusive alert call.
```

The rule above will detect usage of `alert` in JavaScript. Running the rule via `sg scan`.
The rule above will detect usage of `alert` in JavaScript. Running the rule via `ast-grep scan`.

```sh
sg scan --rule no-alert.yml
ast-grep scan --rule no-alert.yml
```

The command leverages built-in behaviors in ast-grep to handle language injection seamlessly. It will produce the following warning message for the HTML file above.
Expand Down Expand Up @@ -159,7 +159,7 @@ With the above `languageInjections` configuration, ast-grep will:
You can search the CSS inside JavaScript in the project configuration folder using this command:

```sh
sg -p 'background: $COLOR' -C 2
ast-grep -p 'background: $COLOR' -C 2
```

It will produce the match result:
Expand Down Expand Up @@ -223,7 +223,7 @@ const artistsQuery = graphql`
We can search the GraphQL fragment via this `--inline-rules` scan.

```sh
sg scan --inline-rules="{id: test, language: graphql, rule: {kind: fragment_spread}}"
ast-grep scan --inline-rules="{id: test, language: graphql, rule: {kind: fragment_spread}}"
```

Output
Expand Down
6 changes: 3 additions & 3 deletions website/advanced/match-algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ The table below summarize how nodes are skipped during matching.

ast-grep has two ways to configure pattern strictness.

1. Using `--strictness` in `sg run`
1. Using `--strictness` in `ast-grep run`

You can use the `--strictness` flag in [`sg run`](/reference/cli/run.html)
You can use the `--strictness` flag in [`ast-grep run`](/reference/cli/run.html)

```bash
sg run -p '$FOO($BAR)' --strictness ast
ast-grep run -p '$FOO($BAR)' --strictness ast
```

2. Using `strictness` in Pattern Object
Expand Down
14 changes: 7 additions & 7 deletions website/blog/migrate-bevy.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Before we start, we need to make sure that we have the following tools installed
Compared to the other two tools, ast-grep is lesser-known. In short it can do search and replace based on [abstract syntax trees](https://www.wikiwand.com/en/Abstract_syntax_tree). You can install it via [`cargo`](https://crates.io/crates/ast-grep) or [`brew`](https://formulae.brew.sh/formula/ast-grep).

```shell
# install the binary `sg`/`ast-grep`
# install the binary `ast-grep`
cargo install ast-grep
# or use brew
brew install ast-grep
Expand Down Expand Up @@ -149,7 +149,7 @@ So we just need to change the import name. [Using ast-grep is trivial here](http
We need to provide a pattern, `-p`, for it to search as well as a rewrite string, `-r` to replace the old API with the new one. The command should be quite self-explanatory.

```
sg -p 'CoreStage' -r CoreSet -i
ast-grep -p 'CoreStage' -r CoreSet -i
```

We suggest to add `-i` flag for `--interactive` editing. ast-grep will display the changed code diff and ask your decision to accept or not.
Expand Down Expand Up @@ -183,7 +183,7 @@ The [doc](https://bevyengine.org/learn/migration-guides/0.9-0.10/#label-types):

The command:
```bash
sg -p 'StageLabel' -r SystemSet -i
ast-grep -p 'StageLabel' -r SystemSet -i
```

3. `SystemStage`
Expand Down Expand Up @@ -217,7 +217,7 @@ app.configure_set(
Let's write a command for this code migration.

```bash
sg \
ast-grep \
-p '$APP.add_stage_after($STAGE, $OWN_STAGE, SystemStage::parallel())' \
-r '$APP.configure_set($OWN_STAGE.after($STAGE))' -i
```
Expand All @@ -231,7 +231,7 @@ meta-variable is a wildcard expression that can match any single AST node. So we
However, I found some `add_stage_after`s are not replaced. Nah, ast-grep is [quite dumb](https://github.com/ast-grep/ast-grep/issues/374) that it cannot handle the optional comma after the last argument. So I used another query with a trailing comma.

```shell
sg \
ast-grep \
-p 'app.add_stage_after($STAGE, $OWN_STAGE, SystemStage::parallel(),)' \
-r 'app.configure_set($OWN_STAGE.after($STAGE))' -i
```
Expand Down Expand Up @@ -267,7 +267,7 @@ app.add_system(my_system.in_base_set(CoreSet::PostUpdate))
Let's also write a pattern for it.

```sh
sg \
ast-grep \
-p '$APP.add_system_to_stage($STAGE, $SYS)' \
-r '$APP.add_system($SYS.in_base_set($STAGE))' -i
```
Expand Down Expand Up @@ -305,7 +305,7 @@ _It is still faster than me scratching my head about how to automate everything.
Another change is to use `add_systems` instead of `add_system_set`. This is a simple pattern!

```sh
sg \
ast-grep \
-p '$APP.add_system_set_to_stage($STAGE, $SYS,)' \
-r '$APP.add_systems($SYS.in_set($STAGE))' -i
```
Expand Down
6 changes: 3 additions & 3 deletions website/blog/stars-3000.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ ast-grep is constantly evolving and improving thanks to the feedback and contrib

* ast-grep’s YAML rule now has a new `transform` rule: `conversion`, which can change matches to different cases, such as upper, lower, or camelcase.
* ast-grep’s diff/rewriting now can fix multiple rules at once. See [commit](https://github.com/ast-grep/ast-grep/commit/2b301116996b7b010ed271672d35a3529fb36e56)
* `sg test -f`now accepts regex to selectively run ast-grep’s test case.
* `sg --json` supports multiple formats that powers [telescope-sg](https://github.com/Marskey/telescope-sg), a neovim plugin that integrates ast-grep with telescope.
* `ast-grep test -f`now accepts regex to selectively run ast-grep’s test case.
* `ast-grep --json` supports multiple formats that powers [telescope-sg](https://github.com/Marskey/telescope-sg), a neovim plugin that integrates ast-grep with telescope.
* ast-grep now prints matches with context like `grep -A -B -C`. See [issue](https://github.com/ast-grep/ast-grep/issues/464)
* JSON schema is added for better YAML rule editing. See [folder](https://github.com/ast-grep/ast-grep/tree/main/schemas)
* ast-grep now has official github action setup! See [action](https://github.com/ast-grep/action)
Expand All @@ -61,7 +61,7 @@ ast-grep has many plans and goals for the future to make it more useful and user
* Add python api support to allow users to write custom scripts using ast-grep. See [issue](https://github.com/ast-grep/ast-grep/issues/389)
* Support global language config to let users specify default options for each language. See [issue](https://github.com/ast-grep/ast-grep/issues/658)
* Improve napi documentation to help users understand how to use the native node module of ast-grep. See [issue](https://github.com/ast-grep/ast-grep/issues/682)
* Add metavar filter to make sg run more powerful by allowing users to filter matches based on metavariable values. See [issue](https://github.com/ast-grep/ast-grep/issues/379)
* Add metavar filter to make ast-grep run more powerful by allowing users to filter matches based on metavariable values. See [issue](https://github.com/ast-grep/ast-grep/issues/379)
* Add ast-grep’s pattern/rule tutorial to teach users how to write effective and efficient patterns and rules for ast-grep. See [issue](https://github.com/ast-grep/ast-grep.github.io/issues/154)
* Add examples to ast-grep’s reference page to illustrate the usage and functionality of each option and feature. See [issue](https://github.com/ast-grep/ast-grep.github.io/issues/266)

Expand Down
2 changes: 1 addition & 1 deletion website/blog/stars-5000.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ We have introduced a lot of new features in the past few months, and we want to
### CLI
* You can now use [`--inline-rules`](https://ast-grep.github.io/reference/cli/scan.html#inline-rules-rule-text) to run rules without creating any files on your disk! You can pass everything, pattern/rule/input, as a string. This is great for scripting!
* [`--stdin`](https://ast-grep.github.io/reference/cli/run.html#stdin) will always wait for your input so you can match some code written in your terminal.
* You can also select [custom languages](https://ast-grep.github.io/advanced/custom-language.html) in [`sg new`](https://ast-grep.github.io/reference/cli/new.html).
* You can also select [custom languages](https://ast-grep.github.io/advanced/custom-language.html) in [`ast-grep new`](https://ast-grep.github.io/reference/cli/new.html).

### Language Support
* We have added support for three new languages: bash, php and elixir.
Expand Down
2 changes: 1 addition & 1 deletion website/blog/stars-6000.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ The [ast-grep VSCode extension](https://marketplace.visualstudio.com/items?itemN

- **Tree-Sitter Version Bump**: We've upgraded to the latest tree-sitter version, enhancing parsing accuracy and speed. In future releases, we plan to leverage tree-sitter's [new Web Assembly grammar](https://zed.dev/blog/language-extensions-part-1) to support even more languages.
- **Scan and Diff Merge**: The [refactor](https://github.com/ast-grep/ast-grep/commit/c78299d2902662cd98bda44f3faf3fbc88439078) combines `CombinedScan::scan` and `CombinedScan::diff` for a more streamlined process.
- **Input Stream Optimization**: Now, ast-grep avoids unnecessary input stream usage when updating all rules [#943](https://github.com/ast-grep/ast-grep/pull/943), making it possible to use `sg scan --update-all`.
- **Input Stream Optimization**: Now, ast-grep avoids unnecessary input stream usage when updating all rules [#943](https://github.com/ast-grep/ast-grep/pull/943), making it possible to use `ast-grep scan --update-all`.

## Usability Improvements

Expand Down
6 changes: 3 additions & 3 deletions website/catalog/rule-template.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
language: JavaScript # please fully spell the language
playgroundLink: '[TODO]'
command: 'sg -p [TODO] -r [TODO]'
command: 'ast-grep -p [TODO] -r [TODO]'
hasFix: true
ruleType: 'pattern' # 'pattern' or 'yaml'
---
Expand All @@ -20,9 +20,9 @@ Some Description for your rule!
### Pattern

```shell
sg -p pattern -r rewrite -l js
ast-grep -p pattern -r rewrite -l js
# or without fixer
sg -p pattern -l js
ast-grep -p pattern -l js
```

<!-- Use YAML in the example. Delete this section if use pattern. -->
Expand Down
2 changes: 1 addition & 1 deletion website/catalog/rust/boshen-footgun.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Depending on your use case, you may want to use `char_indices()` instead of `cha
### Pattern

```shell
sg -p '$A.chars().enumerate()' \
ast-grep -p '$A.chars().enumerate()' \
-r '$A.char_indices()' \
-l rs
```
Expand Down
2 changes: 1 addition & 1 deletion website/catalog/rust/get-digit-count-in-usize.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The snippet above computes the integer logarithm base 10 of the number and adds
### Pattern

```shell
sg -p '$NUM.to_string().chars().count()' \
ast-grep -p '$NUM.to_string().chars().count()' \
-r '$NUM.checked_ilog10().unwrap_or(0) + 1' \
-l rs
```
Expand Down
6 changes: 3 additions & 3 deletions website/catalog/typescript/redundant-usestate-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ We can usually skip annotating if the generic type argument is a single primitiv

::: code-group
```bash [number]
sg -p 'useState<number>($A)' -r 'useState($A)' -l ts
ast-grep -p 'useState<number>($A)' -r 'useState($A)' -l ts
```

```bash [string]
sg -p 'useState<string>($A)' -r 'useState($A)'
ast-grep -p 'useState<string>($A)' -r 'useState($A)'
```

```bash [boolean]
sg -p 'useState<boolean>($A)' -r 'useState($A)'
ast-grep -p 'useState<boolean>($A)' -r 'useState($A)'
```
:::

Expand Down
6 changes: 3 additions & 3 deletions website/guide/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ast-grep is a new AST based tool for managing your code, at massive scale.
Using ast-grep can be as simple as running a single command in your terminal:

```bash
sg --pattern 'var code = $PAT' --rewrite 'let code = $PAT' --lang js
ast-grep --pattern 'var code = $PAT' --rewrite 'let code = $PAT' --lang js
```

The command above will replace `var` statement with `let` for all <abbr title="ast-grep will also infer the language if you omit --lang">JavaScript</abbr> files.
Expand All @@ -24,7 +24,7 @@ The command above will replace `var` statement with `let` for all <abbr title="a

ast-grep is a versatile tool for searching, linting and rewriting code in various languages.

* **Search**: As a command line tool in your terminal, ast-grep, `sg`, can precisely search code based on AST, running through ten thousand files in sub seconds.
* **Search**: As a command line tool in your terminal, `ast-grep` can precisely search code based on AST, running through ten thousand files in sub seconds.
* **Lint**: You can also use ast-grep as a linter. Thanks to the flexible rule configuration, adding a new customized rule is more intuitive and straightforward. It also has a pretty error reporting out of box
* **Rewrite**: ast-grep provide jQuery like utility methods to traverse and manipulate syntax tree. Besides, you can also use operators to compose complex matching from simple patterns.

Expand Down Expand Up @@ -71,7 +71,7 @@ Consider it as same as `grep` but based on AST instead of text.
In comparison to Babel, we can complete this hello-world task in ast-grep trivially

```bash
sg -p "console.log"
ast-grep -p "console.log"
```

See [playground](/playground.html) in action!
Expand Down
2 changes: 1 addition & 1 deletion website/guide/project/lint-rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ After you have written your rule, you can test it with ast-grep's builtin `test`
Let's see it in [next section](/guide/test-rule).

:::tip Pro Tip
You can write a standalone [rule file](/reference/rule.html) and the command `sg scan -r rule.yml` to perform an [ad-hoc search](/guide/tooling-overview.html#run-one-single-query-or-one-single-rule).
You can write a standalone [rule file](/reference/rule.html) and the command `ast-grep scan -r rule.yml` to perform an [ad-hoc search](/guide/tooling-overview.html#run-one-single-query-or-one-single-rule).
:::
6 changes: 3 additions & 3 deletions website/guide/project/project-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Root Configuration File

ast-grep supports using [YAML](https://yaml.org/) to configure its linting rules to scan your code repository.
We need a root configuration file `sgconfig.yml` to specify directories where `sg` can find all rules.
We need a root configuration file `sgconfig.yml` to specify directories where `ast-grep` can find all rules.

In your project root, add `sgconfig.yml` with content as below.

Expand All @@ -28,10 +28,10 @@ my-awesome-project
|- not-a-rule.yml
```

All the YAML files under `rules` folder will be treated as rule files by `sg`, while`not-a-rule.yml` is ignored by ast-grep.
All the YAML files under `rules` folder will be treated as rule files by `ast-grep`, while`not-a-rule.yml` is ignored.


**Note, the [`sg scan`](/reference/cli.html#scan) command requires you have an `sgconfig.yml` in your project root.**
**Note, the [`ast-grep scan`](/reference/cli.html#scan) command requires you have an `sgconfig.yml` in your project root.**

:::tip Pro tip
We can also use directories in `node_modules` to reuse preconfigured rules published on npm!
Expand Down
20 changes: 10 additions & 10 deletions website/guide/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@ pip install ast-grep-cli
```
:::

The binary command, `sg`, or `ast-grep`, should be available now. Let's try it with `--help`.
The binary command, `ast-grep` or `sg`, should be available now. Let's try it with `--help`.

```shell
sg --help
# if you are on Linux
ast-grep --help
# if you are not on Linux
sg --help
```

:::danger Use `sg` on Linux
Linux has a default command `sg` for `setgroups`. You can use the full command name `ast-grep` instead of `sg`.
You can also use shorter alias if you want by `alias sg=ast-grep`.
We will use `sg` in the guide below.
We will use `ast-grep` in the guide below.
:::


Expand Down Expand Up @@ -103,22 +103,22 @@ Optionally, we can use `lang` to tell ast-grep our target code language.

:::code-group
```shell [Full Command]
sg --pattern '$PROP && $PROP()' --lang ts TypeScript/src
ast-grep --pattern '$PROP && $PROP()' --lang ts TypeScript/src
```
```shell [Short Form]
sg -p '$PROP && $PROP()' -l ts TypeScript/src
ast-grep -p '$PROP && $PROP()' -l ts TypeScript/src
```
```shell [Without Lang]
# ast-grep will infer languages based on file extensions
sg -p '$PROP && $PROP()' TypeScript/src
ast-grep -p '$PROP && $PROP()' TypeScript/src
```
:::

:::tip Pro Tip
Pattern must be quoted by single quote `'` to prevent shell from interpreting `$` sign.
`sg -p '$PROP && $PROP()'` is okay.
`ast-grep -p '$PROP && $PROP()'` is okay.

But `sg -p "$PROP && $PROP()"` will be interpreted as `sg -p " && ()"` after shell expansion.
But `ast-grep -p "$PROP && $PROP()"` will be interpreted as `ast-grep -p " && ()"` after shell expansion.
:::

## Rewrite
Expand All @@ -127,7 +127,7 @@ Cool? Now we can use this pattern to refactor TypeScript source!

```shell
# pattern and language argument support short form
sg -p '$PROP && $PROP()' \
ast-grep -p '$PROP && $PROP()' \
--rewrite '$PROP?.()' \
--interactive \
-l ts \
Expand Down
Loading