From 300cebb6479f92e69dc2220aa8d7d54ddd8b3c10 Mon Sep 17 00:00:00 2001 From: Morgante Pell Date: Sat, 9 Mar 2024 21:38:52 +0100 Subject: [PATCH] chore: add some basic Grit patterns (#2) Co-authored-by: Lars Kappert --- .gitignore | 1 - .grit/.gitignore | 2 ++ .grit/grit.yaml | 14 ++++++++++++++ .grit/patterns/flushable_unwrap.md | 23 +++++++++++++++++++++++ .grit/patterns/no_panic.md | 28 ++++++++++++++++++++++++++++ README.md | 4 ++-- 6 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 .grit/.gitignore create mode 100644 .grit/grit.yaml create mode 100644 .grit/patterns/flushable_unwrap.md create mode 100644 .grit/patterns/no_panic.md diff --git a/.gitignore b/.gitignore index 4a50ac637..eb5a316cb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1 @@ target -.grit \ No newline at end of file diff --git a/.grit/.gitignore b/.grit/.gitignore new file mode 100644 index 000000000..e4fdfb17c --- /dev/null +++ b/.grit/.gitignore @@ -0,0 +1,2 @@ +.gritmodules* +*.log diff --git a/.grit/grit.yaml b/.grit/grit.yaml new file mode 100644 index 000000000..2914bf0b6 --- /dev/null +++ b/.grit/grit.yaml @@ -0,0 +1,14 @@ +version: 0.0.1 +patterns: + - name: github.com/getgrit/stdlib#* + - name: no_println_in_lsp + description: Don't use println!() in LSP code, it breaks the LSP stdio protocol. + level: error + body: | + engine marzano(0.1) + language rust + + `println!($_)` => . where { + $filename <: not includes "test.rs", + $absolute_filename <: includes "apps/marzano/lsp", + } diff --git a/.grit/patterns/flushable_unwrap.md b/.grit/patterns/flushable_unwrap.md new file mode 100644 index 000000000..8c0908a07 --- /dev/null +++ b/.grit/patterns/flushable_unwrap.md @@ -0,0 +1,23 @@ +# Force flushing + +Prevent returning without flushing the emitter in `apply_pattern.rs`. + +```grit +language rust + +pattern flushable_return() { + `$expression?` where { + $expression <: not includes "flush", + $expression <: not within `let mut emitter: MessengerVariant = $_;`, + $expression => `flushable_unwrap!(emitter, $expression)` + } +} + +file(name=includes "apply_pattern.rs", $body) where { + $body <: contains `pub(crate) async fn run_apply_pattern( + $_ + ) -> Result<()> { $func }` where { + $func <: contains flushable_return() + } +} +``` diff --git a/.grit/patterns/no_panic.md b/.grit/patterns/no_panic.md new file mode 100644 index 000000000..bc445a30a --- /dev/null +++ b/.grit/patterns/no_panic.md @@ -0,0 +1,28 @@ +--- +level: info +--- + +# Avoid panic + +Panics should be avoided in core Grit code. Instead, use `?` to propagate errors. + +```grit +language rust + +file($name, $body) where { + $name <: includes "marzano/lsp", + $body <: contains `$foo.unwrap()` => `$foo?`, +} +``` + +## Sample + +```rust +// @filename: marzano/lsp/foo.rs +let x = bar().unwrap(); +``` + +```rust +// @filename: marzano/lsp/foo.rs +let x = bar()?; +``` diff --git a/README.md b/README.md index de08256c8..5efad9660 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # GritQL -[![CI Status](https://img.shields.io/github/actions/workflow/status/getgrit/tree-sitter-gritql/ci.yml)](https://github.com/getgrit/tree-sitter-gritql/actions/workflows/ci.yml) -[![MIT License](https://img.shields.io/github/license/getgrit/tree-sitter-gritql)](https://github.com/getgrit/tree-sitter-gritql/blob/main/LICENSE) +[![CI Status](https://img.shields.io/github/actions/workflow/status/getgrit/gritql/main.yml)](https://github.com/getgrit/gritql/actions/workflows/main.yml) +[![MIT License](https://img.shields.io/github/license/getgrit/gritql)](https://github.com/getgrit/gritql/blob/main/LICENSE) [![Discord](https://img.shields.io/discord/1063097320771698699?logo=discord&label=discord)](https://docs.grit.io/discord) GritQL is a declarative query language for searching and modifying source code. GritQL focuses on a few areas: