From f1eaec17906d5bd1ef9605c4810d97c450f19160 Mon Sep 17 00:00:00 2001 From: petrisorcoderabbit Date: Fri, 8 Mar 2024 18:11:19 +0200 Subject: [PATCH 1/5] Add ast-grep documentation page --- .gitignore | 2 + docs/integrations/ast-grep.md | 256 ++++++++++++++++++++++++++++++++++ sidebars.ts | 2 +- 3 files changed, 259 insertions(+), 1 deletion(-) create mode 100644 docs/integrations/ast-grep.md diff --git a/.gitignore b/.gitignore index b2d6de30..9bf902f9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +# Editors +.idea # Dependencies /node_modules diff --git a/docs/integrations/ast-grep.md b/docs/integrations/ast-grep.md new file mode 100644 index 00000000..b6f62405 --- /dev/null +++ b/docs/integrations/ast-grep.md @@ -0,0 +1,256 @@ +--- +title: Ast-Grep rules in CodeRabbit +description: Integrate Ast-Grep rules with CodeRabbit +sidebar_label: Ast-Grep +image: "/preview_meta.jpg" +--- + + + + + + + + + + + + + + + + + + +This documentation provides guidance on integrating AST-Grep rules within the CodeRabbit platform. AST-Grep is a tool used for searching code using abstract syntax trees (AST) patterns. + +### **Setting up AST-Grep rules** +By default, users can add AST-Grep rules by following these steps: + +1. Create a folder named `rules` in your project directory. +2. Add individual `.yaml` files for each AST-Grep rule within the `rules` folder. +3. Ensure each `.yaml` file contains the necessary AST-Grep rule configurations. +4. Ensure that all rules contains a `message` property, that is going to be used on the review process. + +### **The rule object** + +Rule object is the core concept of ast-grep's rule system and every other features are built on top of it. + +Below is the full list of fields in a rule object. Every rule field is optional and can be omitted but at least one field should be present in a rule. A node will match a rule if and only if it satisfies all fields in the rule object. +```yaml +rule: + # atomic rule + pattern: 'search.pattern' + kind: 'tree_sitter_node_kind' + regex: 'rust|regex' + # relational rule + inside: { pattern: 'sub.rule' } + has: { kind: 'sub_rule' } + follows: { regex: 'can|use|any' } + precedes: { kind: 'multi_keys', pattern: 'in.sub' } + # composite rule + all: [ {pattern: 'match.all'}, {kind: 'match_all'} ] + any: [ {pattern: 'match.any'}, {kind: 'match_any'} ] + not: { pattern: 'not.this' } + matches: 'utility-rule' +``` + +### **Three Rule Categories** +To summarize the rule object fields above, we have three categories of rules: + +- Atomic Rule: the most basic rule that checks if AST nodes matches. +- Relational Rule: rules that check if a node is surrounded by another node. +- Composite Rule: rules that combine sub-rules together using logical operators. + +These three categories of rules can be composed together to create more complex rules. + +The rule object is inspired by the CSS selectors but with more composability and expressiveness. Think about how selectors in CSS works can help you understand the rule object! + +> Read ast-grep [documentation](https://ast-grep.github.io/guide/rule-config.html) for detailed guides. + +#### **Atomic rule** +Atomic rule defines the most basic matching rule that determines whether one syntax node matches the rule or not. There are three kinds of atomic rule: `pattern`, `kind` and `regex`. + +> Official documentation guide on [Atomic Rule](https://ast-grep.github.io/guide/rule-config/atomic-rule.html) + +#### **Relational rule** +Relational rule defines the relationship between two syntax nodes. There are four kinds of relational rule: `inside`, `has`, `follows` and `precedes`. + +All four relational rules accept a sub rule object as their value. The sub rule will match the surrounding node while the relational rule itself will match the target node. + +> Official documentation guide on [Relational Rule](https://ast-grep.github.io/guide/rule-config/relational-rule.html) + +```yaml +rule: + pattern: await $PROMISE + inside: + kind: for_in_statement + stopBy: end +``` + +#### **Composite rule** +Composite rule defines the logical relationship between multiple sub rules. There are three kinds of composite rule: `all`, `any` and `not`. + +**all** + +The `all` rule matches if all sub rules match. +```yaml +rule: + all: + - pattern: console.log('Hello World'); + - kind: expression_statement +``` + +**any** + +`any` rule matches if any sub rule matches. +```yaml +rule: + any: + - pattern: var a = $A + - pattern: const a = $A + - pattern: let a = $A +``` + +**not** + +`not` applies negation to a sub rule. It matches if the sub rule does not match. + +```yaml +rule: + pattern: console.log($GREETING) + not: + pattern: console.log('Hello World') +``` + +> Official documentation guide on [Composite Rule](https://ast-grep.github.io/guide/rule-config/composite-rule.html) + + +## Reusing rule as utility +ast-grep chooses to use YAML for rule representation. While this decision makes writing rules easier, it does impose some limitations on the rule authoring. One of the limitations is that rule objects cannot be reused. + +#### **Local utility rule** +Local utility rules are defined in the utils field of the config file. utils is a string-keyed dictionary. + +For example, the following config file defines a local utility rule `is-literal`: + +```yaml +utils: + is-literal: + any: + - kind: string + - kind: number + - kind: boolean +rule: + matches: is-literal +``` + +#### **Global utility rule** +Global utility rules are defined in a separate file. But they are available across all rule configurations in the project. + +To create global utility rules, you need to have the `rules` folder created on the root of your project and another +`utils` directory inside the root of your project. + +```yaml +my-awesome-project # project root + |- rules # rule directory + | |- my-rule.yml + |- utils # utils directory + | |- is-literal.yml +``` + +```yaml +# is-literal.yml +id: is-literal +language: TypeScript +rule: + any: + - kind: 'false' + - kind: undefined + - kind: 'null' + - kind: 'true' + - kind: regex + - kind: number + - kind: string +``` + +> Official documentation guide on [Utility Rule](https://ast-grep.github.io/guide/rule-config/utility-rule.html) + +## Multiple Languages Support + +CodeRabbit supports multiple programming languages for defining AST-Grep rules. + +- Javascript +- Typescript +- C# +- Golang +- Java +- Kotlin +- Rust +- Python +- C + +Below are examples of AST-Grep rules in different languages: + +### **Javascript** +#### Importing files without an extension is not allowed +```yaml +id: find-import-file +language: js +message: "Importing files without an extension is not allowed" +rule: + regex: "/[^.]+[^/]$" + kind: string_fragment + any: + - inside: + stopBy: end + kind: import_statement + - inside: + stopBy: end + kind: call_expression + has: + field: function + regex: "^import$" +``` + +#### No console.log allowed except console.error on the catch block +```yaml +id: no-console-except-error +language: typescript +message: "No console.log allowed except console.error on the catch block" +rule: + any: + - pattern: console.error($$$) + not: + inside: + kind: catch_clause + stopBy: end + - pattern: console.$METHOD($$$) +constraints: + METHOD: + regex: 'log|debug|warn' +``` + +### **C** +In C, there is no built-in support for object-oriented programming, but some programmers use structs and function pointers to simulate classes and methods. + +However, this style can have some drawbacks, such as: +- extra memory allocation and reallocation for the struct and the function pointer. +- indirection overhead when calling the function pointer. + +A possible alternative is to use a plain function call with the struct pointer as the first argument. + +```yaml +id: method_receiver +language: c +rule: + pattern: $R.$METHOD($$$ARGS) +transform: + MAYBE_COMMA: + replace: + source: $$$ARGS + replace: '^.+' + by: ', ' +fix: + $METHOD(&$R$MAYBE_COMMA$$$ARGS) +``` diff --git a/sidebars.ts b/sidebars.ts index 3d051844..7599ed7b 100644 --- a/sidebars.ts +++ b/sidebars.ts @@ -40,7 +40,7 @@ const sidebars: SidebarsConfig = { { type: "category", label: "Integrations", - items: ["integrations/self-hosted-gitlab"], + items: ["integrations/self-hosted-gitlab", "integrations/ast-grep"], }, "faq/faq", ], From fc4ac6209d8a13c7910f8d72744c4b052265da89 Mon Sep 17 00:00:00 2001 From: petrisorcoderabbit Date: Fri, 8 Mar 2024 22:19:47 +0200 Subject: [PATCH 2/5] Update the ast-grep documentation to include the coderabbit config file changes --- docs/integrations/ast-grep.md | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/docs/integrations/ast-grep.md b/docs/integrations/ast-grep.md index b6f62405..85789c08 100644 --- a/docs/integrations/ast-grep.md +++ b/docs/integrations/ast-grep.md @@ -27,10 +27,20 @@ This documentation provides guidance on integrating AST-Grep rules within the Co ### **Setting up AST-Grep rules** By default, users can add AST-Grep rules by following these steps: -1. Create a folder named `rules` in your project directory. -2. Add individual `.yaml` files for each AST-Grep rule within the `rules` folder. +1. Create a folder that keeps all the `custom-name` in your project directory. +2. Add individual `.yaml` files for each AST-Grep rule within the `custom-name` folder. 3. Ensure each `.yaml` file contains the necessary AST-Grep rule configurations. 4. Ensure that all rules contains a `message` property, that is going to be used on the review process. +5. Add the `custom-name` folder to the `.code-rabbit.yml` file under `tools.ast_grep` configuration. +```yaml +#... +reviews: + #... + tools: + ast_grep: + rules_folder: "custom-name" + #... +``` ### **The rule object** @@ -159,6 +169,19 @@ my-awesome-project # project root | |- is-literal.yml ``` +>Also, you need to add the `rules` and `utils` folders to the `.code-rabbit.yml` file under `tools.ast_grep` configuration. + +```yaml +#... +reviews: + #... + tools: + ast_grep: + rules_folder: "rules" + utils_folder: "utils" + #... +``` + ```yaml # is-literal.yml id: is-literal From eec460dd1d8abf61218bbe6eadcb0a6b823223cd Mon Sep 17 00:00:00 2001 From: petrisorcoderabbit Date: Mon, 11 Mar 2024 16:40:45 +0200 Subject: [PATCH 3/5] Fix language review --- docs/integrations/ast-grep.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/integrations/ast-grep.md b/docs/integrations/ast-grep.md index 85789c08..61e56331 100644 --- a/docs/integrations/ast-grep.md +++ b/docs/integrations/ast-grep.md @@ -30,7 +30,7 @@ By default, users can add AST-Grep rules by following these steps: 1. Create a folder that keeps all the `custom-name` in your project directory. 2. Add individual `.yaml` files for each AST-Grep rule within the `custom-name` folder. 3. Ensure each `.yaml` file contains the necessary AST-Grep rule configurations. -4. Ensure that all rules contains a `message` property, that is going to be used on the review process. +4. Ensure that all rules contains a `message` property, that will be used in the review process. 5. Add the `custom-name` folder to the `.code-rabbit.yml` file under `tools.ast_grep` configuration. ```yaml #... @@ -86,7 +86,7 @@ Atomic rule defines the most basic matching rule that determines whether one syn #### **Relational rule** Relational rule defines the relationship between two syntax nodes. There are four kinds of relational rule: `inside`, `has`, `follows` and `precedes`. -All four relational rules accept a sub rule object as their value. The sub rule will match the surrounding node while the relational rule itself will match the target node. +All four relational rules accept a sub-rule object as their value. The sub-rule will match the surrounding node while the relational rule itself will match the target node. > Official documentation guide on [Relational Rule](https://ast-grep.github.io/guide/rule-config/relational-rule.html) @@ -99,11 +99,11 @@ rule: ``` #### **Composite rule** -Composite rule defines the logical relationship between multiple sub rules. There are three kinds of composite rule: `all`, `any` and `not`. +Composite rule defines the logical relationship between multiple sub-rules. There are three kinds of composite rule: `all`, `any` and `not`. **all** -The `all` rule matches if all sub rules match. +The `all` rule matches if all sub-rules match. ```yaml rule: all: @@ -113,7 +113,7 @@ rule: **any** -`any` rule matches if any sub rule matches. +`any` rule matches if any sub-rule matches. ```yaml rule: any: @@ -124,7 +124,7 @@ rule: **not** -`not` applies negation to a sub rule. It matches if the sub rule does not match. +`not` applies negation to a sub-rule. It matches if the sub-rule does not match. ```yaml rule: @@ -203,7 +203,7 @@ rule: CodeRabbit supports multiple programming languages for defining AST-Grep rules. -- Javascript +- JavaScript - Typescript - C# - Golang @@ -215,7 +215,7 @@ CodeRabbit supports multiple programming languages for defining AST-Grep rules. Below are examples of AST-Grep rules in different languages: -### **Javascript** +### **JavaScript** #### Importing files without an extension is not allowed ```yaml id: find-import-file From 7d868b19ae4e359b7c45940f74fd60f0ec539a72 Mon Sep 17 00:00:00 2001 From: petrisorcoderabbit Date: Tue, 12 Mar 2024 19:46:53 +0200 Subject: [PATCH 4/5] Update coderabbit.yaml schema for ast-grep tool naming --- docs/integrations/ast-grep.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/integrations/ast-grep.md b/docs/integrations/ast-grep.md index 61e56331..51c23ad7 100644 --- a/docs/integrations/ast-grep.md +++ b/docs/integrations/ast-grep.md @@ -31,13 +31,13 @@ By default, users can add AST-Grep rules by following these steps: 2. Add individual `.yaml` files for each AST-Grep rule within the `custom-name` folder. 3. Ensure each `.yaml` file contains the necessary AST-Grep rule configurations. 4. Ensure that all rules contains a `message` property, that will be used in the review process. -5. Add the `custom-name` folder to the `.code-rabbit.yml` file under `tools.ast_grep` configuration. +5. Add the `custom-name` folder to the `.code-rabbit.yml` file under `tools.ast-grep` configuration. ```yaml #... reviews: #... tools: - ast_grep: + ast-grep: rules_folder: "custom-name" #... ``` @@ -169,14 +169,14 @@ my-awesome-project # project root | |- is-literal.yml ``` ->Also, you need to add the `rules` and `utils` folders to the `.code-rabbit.yml` file under `tools.ast_grep` configuration. +>Also, you need to add the `rules` and `utils` folders to the `.code-rabbit.yml` file under `tools.ast-grep` configuration. ```yaml #... reviews: #... tools: - ast_grep: + ast-grep: rules_folder: "rules" utils_folder: "utils" #... From 72c2da72fb9e8f90e9c403452211e3e766ef1320 Mon Sep 17 00:00:00 2001 From: petrisorcoderabbit Date: Tue, 12 Mar 2024 22:15:39 +0200 Subject: [PATCH 5/5] Update coderabbig guide page with new coderabbit.yaml changes --- docs/guides/customize-coderabbit.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/docs/guides/customize-coderabbit.md b/docs/guides/customize-coderabbit.md index 9d6c951f..14b74215 100644 --- a/docs/guides/customize-coderabbit.md +++ b/docs/guides/customize-coderabbit.md @@ -69,6 +69,10 @@ reviews: base_branches: - "develop" - "feat/.*" + tools: + ast-grep: + rules_folder: "custom-rules-folder-name" + utils_folder: "custom-utils-folder-name" chat: auto_reply: true ``` @@ -110,6 +114,12 @@ Yaml settings: `true`). - **base_branches**: A list of base branches where the reviews will occur apart from the default branch. Accepts regex pattern. + - **tools**: Configurations for the tools used in the review. + - **ast-grep**: Configurations for the ast-grep tool. + - **rules_folder**: The folder name where the custom ast-grep rules are + stored. + - **utils_folder**: The folder name where the custom ast-grep utils are + stored. 4. **chat**: Defines the behavior of CodeRabbit's bot in conversations. - **auto_reply**: The bot automatically replies without the need of the user tagging it ( default: `true` ). @@ -154,6 +164,12 @@ settings: collapse_walkthrough_comment: true # Disable automatic code reviews for this repository. disable_review: false + # External tools configurations + tools: + # tools configuration for ast-grep + ast-grep: + rules_folder: "custom-rules-folder-name" + utils_folder: "custom-utils-folder-name" ``` @@ -187,6 +203,12 @@ This configuration file consists of the following settings: be posted. 13. **collapse_walkthrough_comment**: Specifies whether to collapse walkthrough comments on the review. +14. **tools**: Configurations for the tools used in the review. + - **ast-grep**: Configurations for the ast-grep tool. + - **rules_folder**: The folder name where the custom ast-grep rules are + stored. + - **utils_folder**: The folder name where the custom ast-grep utils are + stored. Refer: [CodeRabbit configuration schema](https://coderabbit.ai/integrations/coderabbit-overrides.json).