sagrep 0.9.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sagrep-0.9.0/LICENSE +21 -0
- sagrep-0.9.0/MANIFEST.in +3 -0
- sagrep-0.9.0/PKG-INFO +503 -0
- sagrep-0.9.0/README.md +448 -0
- sagrep-0.9.0/docs/assets/sagrep-ast-scan.png +0 -0
- sagrep-0.9.0/docs/assets/sagrep-color-severities.png +0 -0
- sagrep-0.9.0/docs/assets/sagrep-direct-text.png +0 -0
- sagrep-0.9.0/docs/assets/sagrep-html-findings.png +0 -0
- sagrep-0.9.0/docs/assets/sagrep-html-report.png +0 -0
- sagrep-0.9.0/docs/assets/sagrep-yaml-annotation.png +0 -0
- sagrep-0.9.0/docs/assets/sagrep_logo.png +0 -0
- sagrep-0.9.0/pyproject.toml +95 -0
- sagrep-0.9.0/setup.cfg +4 -0
- sagrep-0.9.0/src/sagrep/__init__.py +34 -0
- sagrep-0.9.0/src/sagrep/__main__.py +4 -0
- sagrep-0.9.0/src/sagrep/annotation/__init__.py +25 -0
- sagrep-0.9.0/src/sagrep/annotation/comment_annotation.py +96 -0
- sagrep-0.9.0/src/sagrep/annotation/comment_markers.py +139 -0
- sagrep-0.9.0/src/sagrep/annotation/directive_interpreter.py +71 -0
- sagrep-0.9.0/src/sagrep/annotation/directive_syntax.py +398 -0
- sagrep-0.9.0/src/sagrep/annotation/languages.py +245 -0
- sagrep-0.9.0/src/sagrep/annotation/matcher.py +331 -0
- sagrep-0.9.0/src/sagrep/annotation/messages.py +50 -0
- sagrep-0.9.0/src/sagrep/annotation/models.py +97 -0
- sagrep-0.9.0/src/sagrep/annotation/scope.py +143 -0
- sagrep-0.9.0/src/sagrep/annotation/search.py +71 -0
- sagrep-0.9.0/src/sagrep/annotation/source.py +112 -0
- sagrep-0.9.0/src/sagrep/arguments.py +270 -0
- sagrep-0.9.0/src/sagrep/cli.py +170 -0
- sagrep-0.9.0/src/sagrep/command/__init__.py +12 -0
- sagrep-0.9.0/src/sagrep/command/ast_grep_command.py +254 -0
- sagrep-0.9.0/src/sagrep/command/find_builder.py +81 -0
- sagrep-0.9.0/src/sagrep/command/find_command.py +55 -0
- sagrep-0.9.0/src/sagrep/config_rule/__init__.py +50 -0
- sagrep-0.9.0/src/sagrep/config_rule/config_loader.py +390 -0
- sagrep-0.9.0/src/sagrep/config_rule/config_models.py +82 -0
- sagrep-0.9.0/src/sagrep/config_rule/config_setup.py +326 -0
- sagrep-0.9.0/src/sagrep/config_rule/rule_converter.py +676 -0
- sagrep-0.9.0/src/sagrep/config_rule/rule_generator.py +469 -0
- sagrep-0.9.0/src/sagrep/config_rule/rule_loader.py +504 -0
- sagrep-0.9.0/src/sagrep/config_rule/rule_models.py +169 -0
- sagrep-0.9.0/src/sagrep/config_rule/rule_preparation.py +112 -0
- sagrep-0.9.0/src/sagrep/config_rule/rule_scope_filter.py +55 -0
- sagrep-0.9.0/src/sagrep/config_rule/rule_selection.py +71 -0
- sagrep-0.9.0/src/sagrep/config_rule/string_rule_converter.py +123 -0
- sagrep-0.9.0/src/sagrep/config_rule/yaml_dynamics.py +114 -0
- sagrep-0.9.0/src/sagrep/constant.py +84 -0
- sagrep-0.9.0/src/sagrep/diagnostics.py +261 -0
- sagrep-0.9.0/src/sagrep/extended_directive/comment_apply.py +114 -0
- sagrep-0.9.0/src/sagrep/extended_directive/component.py +164 -0
- sagrep-0.9.0/src/sagrep/extended_directive/directives.py +346 -0
- sagrep-0.9.0/src/sagrep/extended_directive/occurs.py +240 -0
- sagrep-0.9.0/src/sagrep/extended_directive/occurs_evaluator.py +59 -0
- sagrep-0.9.0/src/sagrep/languages.py +64 -0
- sagrep-0.9.0/src/sagrep/output_build/__init__.py +0 -0
- sagrep-0.9.0/src/sagrep/output_build/cli_output.py +20 -0
- sagrep-0.9.0/src/sagrep/output_build/models.py +75 -0
- sagrep-0.9.0/src/sagrep/output_build/renderers.py +278 -0
- sagrep-0.9.0/src/sagrep/output_build/report_builder.py +78 -0
- sagrep-0.9.0/src/sagrep/output_build/transformer.py +559 -0
- sagrep-0.9.0/src/sagrep/process/__init__.py +1 -0
- sagrep-0.9.0/src/sagrep/process/ast_search.py +84 -0
- sagrep-0.9.0/src/sagrep/process/direct_run.py +110 -0
- sagrep-0.9.0/src/sagrep/process/rule_scan.py +250 -0
- sagrep-0.9.0/src/sagrep/process/runner.py +111 -0
- sagrep-0.9.0/src/sagrep/process/source_scanner.py +113 -0
- sagrep-0.9.0/src/sagrep/shared/__init__.py +1 -0
- sagrep-0.9.0/src/sagrep/shared/exceptions.py +36 -0
- sagrep-0.9.0/src/sagrep/shared/module.py +56 -0
- sagrep-0.9.0/src/sagrep/shared/parallel.py +75 -0
- sagrep-0.9.0/src/sagrep/shared/types.py +6 -0
- sagrep-0.9.0/src/sagrep/string_search/__init__.py +18 -0
- sagrep-0.9.0/src/sagrep/string_search/gitignore.py +51 -0
- sagrep-0.9.0/src/sagrep/string_search/matching.py +238 -0
- sagrep-0.9.0/src/sagrep/string_search/models.py +130 -0
- sagrep-0.9.0/src/sagrep/string_search/native_scope.py +54 -0
- sagrep-0.9.0/src/sagrep/string_search/preparation.py +371 -0
- sagrep-0.9.0/src/sagrep/string_search/result_assembly.py +64 -0
- sagrep-0.9.0/src/sagrep/string_search/searcher.py +60 -0
- sagrep-0.9.0/src/sagrep.egg-info/PKG-INFO +503 -0
- sagrep-0.9.0/src/sagrep.egg-info/SOURCES.txt +90 -0
- sagrep-0.9.0/src/sagrep.egg-info/dependency_links.txt +1 -0
- sagrep-0.9.0/src/sagrep.egg-info/entry_points.txt +3 -0
- sagrep-0.9.0/src/sagrep.egg-info/requires.txt +33 -0
- sagrep-0.9.0/src/sagrep.egg-info/top_level.txt +2 -0
- sagrep-0.9.0/src/sagrep_report/__init__.py +1 -0
- sagrep-0.9.0/src/sagrep_report/__main__.py +4 -0
- sagrep-0.9.0/src/sagrep_report/cli.py +88 -0
- sagrep-0.9.0/src/sagrep_report/loader.py +76 -0
- sagrep-0.9.0/src/sagrep_report/model.py +168 -0
- sagrep-0.9.0/src/sagrep_report/renderer.py +59 -0
- sagrep-0.9.0/src/sagrep_report/templates/report.html +371 -0
sagrep-0.9.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) sagrep contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
sagrep-0.9.0/MANIFEST.in
ADDED
sagrep-0.9.0/PKG-INFO
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sagrep
|
|
3
|
+
Version: 0.9.0
|
|
4
|
+
Summary: Lint and validate source code with AST rules, text-search rules, and annotations.
|
|
5
|
+
License-Expression: MIT
|
|
6
|
+
Project-URL: Repository, https://github.com/deerhnt/sagrep
|
|
7
|
+
Project-URL: Documentation, https://sagrep.deerh.workers.dev/
|
|
8
|
+
Keywords: ast-grep,lint,validation,static-analysis,annotations,tree-sitter,ast,code-quality,code-review,source-code,html-report,code-analysis,source-code-analysis,ai-assisted-development,ai-coding-assistant
|
|
9
|
+
Classifier: Environment :: Console
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
18
|
+
Classifier: Topic :: Software Development :: Quality Assurance
|
|
19
|
+
Classifier: Topic :: Text Processing
|
|
20
|
+
Requires-Python: >=3.10
|
|
21
|
+
Description-Content-Type: text/markdown
|
|
22
|
+
License-File: LICENSE
|
|
23
|
+
Requires-Dist: typing-extensions>=4.0
|
|
24
|
+
Requires-Dist: pathspec>=0.12
|
|
25
|
+
Requires-Dist: PyYAML>=6.0
|
|
26
|
+
Requires-Dist: regex>=2024.11.6
|
|
27
|
+
Requires-Dist: tree-sitter==0.25.2
|
|
28
|
+
Requires-Dist: tree-sitter-bash==0.25.1
|
|
29
|
+
Requires-Dist: tree-sitter-c==0.24.2
|
|
30
|
+
Requires-Dist: tree-sitter-cpp==0.23.4
|
|
31
|
+
Requires-Dist: tree-sitter-c-sharp==0.23.5
|
|
32
|
+
Requires-Dist: tree-sitter-css==0.25.0
|
|
33
|
+
Requires-Dist: tree-sitter-elixir==0.3.5
|
|
34
|
+
Requires-Dist: tree-sitter-go==0.25.0
|
|
35
|
+
Requires-Dist: tree-sitter-html==0.23.2
|
|
36
|
+
Requires-Dist: tree-sitter-java==0.23.5
|
|
37
|
+
Requires-Dist: tree-sitter-javascript==0.25.0
|
|
38
|
+
Requires-Dist: tree-sitter-kotlin==1.1.0
|
|
39
|
+
Requires-Dist: tree-sitter-lua==0.5.0
|
|
40
|
+
Requires-Dist: tree-sitter-php==0.25.1
|
|
41
|
+
Requires-Dist: tree-sitter-python==0.25.0
|
|
42
|
+
Requires-Dist: tree-sitter-ruby==0.23.1
|
|
43
|
+
Requires-Dist: tree-sitter-rust==0.24.2
|
|
44
|
+
Requires-Dist: tree-sitter-scala==0.26.2
|
|
45
|
+
Requires-Dist: tree-sitter-scss==1.0.0
|
|
46
|
+
Requires-Dist: tree-sitter-swift==0.7.4
|
|
47
|
+
Requires-Dist: tree-sitter-typescript==0.23.2
|
|
48
|
+
Provides-Extra: test
|
|
49
|
+
Requires-Dist: pytest>=8; extra == "test"
|
|
50
|
+
Requires-Dist: setuptools>=77.0.3; extra == "test"
|
|
51
|
+
Provides-Extra: release
|
|
52
|
+
Requires-Dist: build>=1.2; extra == "release"
|
|
53
|
+
Requires-Dist: twine>=6; extra == "release"
|
|
54
|
+
Dynamic: license-file
|
|
55
|
+
|
|
56
|
+
# sagrep
|
|
57
|
+
|
|
58
|
+

|
|
59
|
+
|
|
60
|
+
**sagrep is a lint tool for your team's own rules.** It supports `Python`, `JavaScript`, `PHP`, `Go`, and other languages. Use AST patterns, text searches, or annotations to find unwanted code, check for required code, and set limits on matches. For example:
|
|
61
|
+
|
|
62
|
+
> "This method must call `validate()`."
|
|
63
|
+
> "Don't leave `print("debug")` in the code."
|
|
64
|
+
> "`emit()` may be called at most twice."
|
|
65
|
+
|
|
66
|
+
Save the requirement as a YAML rule or declare it in an annotation, then run a Scan on every change. For a one-off search, pass an AST or text condition on the command line. sagrep shows **what it found and where**, in the terminal, in CI, or in a browser report.
|
|
67
|
+
|
|
68
|
+
## Requirements and installation
|
|
69
|
+
|
|
70
|
+
Install **Python 3.10 or later** and put the **[ast-grep](https://ast-grep.github.io/) executable** on `PATH`. sagrep also requires a compatible SCSS grammar wheel. See the [platform requirements](docs/getting-started.md#install).
|
|
71
|
+
|
|
72
|
+
Install sagrep from the `sagrep` project directory of a source checkout, then check the version:
|
|
73
|
+
|
|
74
|
+
```console
|
|
75
|
+
$ python -m pip install .
|
|
76
|
+
$ sagrep --version
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Install ast-grep separately. One option is the Python CLI distribution:
|
|
80
|
+
|
|
81
|
+
```console
|
|
82
|
+
$ python -m pip install ast-grep-cli
|
|
83
|
+
$ ast-grep --version
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Table of Contents
|
|
87
|
+
|
|
88
|
+
- [sagrep](#sagrep)
|
|
89
|
+
- [Requirements and installation](#requirements-and-installation)
|
|
90
|
+
- [Table of Contents](#table-of-contents)
|
|
91
|
+
- [Supported languages](#supported-languages)
|
|
92
|
+
- [How is this different from ast-grep?](#how-is-this-different-from-ast-grep)
|
|
93
|
+
- [Try it](#try-it)
|
|
94
|
+
- [How sagrep works](#how-sagrep-works)
|
|
95
|
+
- [Run checks and search code](#run-checks-and-search-code)
|
|
96
|
+
- [Scan: run saved checks](#scan-run-saved-checks)
|
|
97
|
+
- [Direct Run: search from the command line](#direct-run-search-from-the-command-line)
|
|
98
|
+
- [Filter: choose which rules run](#filter-choose-which-rules-run)
|
|
99
|
+
- [Find: choose which source files are checked](#find-choose-which-source-files-are-checked)
|
|
100
|
+
- [Rules and annotations](#rules-and-annotations)
|
|
101
|
+
- [YAML rules](#yaml-rules)
|
|
102
|
+
- [Annotations](#annotations)
|
|
103
|
+
- [YAML Annotations](#yaml-annotations)
|
|
104
|
+
- [Comment Annotations](#comment-annotations)
|
|
105
|
+
- [YAML extensions](#yaml-extensions)
|
|
106
|
+
- [Result output](#result-output)
|
|
107
|
+
- [Save a text report](#save-a-text-report)
|
|
108
|
+
- [Colored text output](#colored-text-output)
|
|
109
|
+
- [JSON for automation](#json-for-automation)
|
|
110
|
+
- [HTML reports with `sagrep-report`](#html-reports-with-sagrep-report)
|
|
111
|
+
- [Verbose mode](#verbose-mode)
|
|
112
|
+
- [Documentation](#documentation)
|
|
113
|
+
- [Examples](#examples)
|
|
114
|
+
- [Skills](#skills)
|
|
115
|
+
- [Project layout](#project-layout)
|
|
116
|
+
- [License](#license)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
## Supported languages
|
|
120
|
+
|
|
121
|
+
sagrep supports these languages:
|
|
122
|
+
|
|
123
|
+
`Bash`, `C`, `C++`, `C#`, `CSS`/`SCSS`, `Elixir`, `Go`, `HTML`, `Java`, `JavaScript`, `Kotlin`, `Lua`, `PHP`, `Python`, `Ruby`, `Rust`, `Scala`, `Swift`, `TypeScript`, and `TSX`.
|
|
124
|
+
|
|
125
|
+
Check a mixed-language project in one Scan and review its findings in one report. When the same code pattern is valid in several languages, use `languages` to share an AST rule instead of writing separate rules. Use separate rules when syntax differs.
|
|
126
|
+
|
|
127
|
+
Language support varies by feature. AST language support depends on the installed ast-grep, and text searches work on text files without selecting a language. See [language names, extensions, and scope](docs/languages.md) for details.
|
|
128
|
+
|
|
129
|
+
## How is this different from ast-grep?
|
|
130
|
+
|
|
131
|
+
- **Match code with ast-grep.** For example, `emit($A)` finds calls with one argument.
|
|
132
|
+
- **Check counts with sagrep.** Set `gt: 2` under `occurs` to report three or more matches across the files selected by the rule.
|
|
133
|
+
- **Reuse YAML rules and settings.** Use `base` to share settings. Mark an AST rule with `component: true` so a YAML or Comment Annotation can select it with `apply`.
|
|
134
|
+
- **Check file text in the same Scan.** A YAML Text rule can flag a literal `TODO`.
|
|
135
|
+
|
|
136
|
+
See [How sagrep uses ast-grep](docs/why-sagrep.md#how-sagrep-uses-ast-grep) for the workflow and [YAML extensions](#yaml-extensions) for the settings.
|
|
137
|
+
|
|
138
|
+
## Try it
|
|
139
|
+
|
|
140
|
+
Run the included Python example from the `sagrep` project directory. The sample already includes the settings and a YAML AST rule:
|
|
141
|
+
|
|
142
|
+
```console
|
|
143
|
+
$ cd examples/basics/ast/
|
|
144
|
+
$ sagrep
|
|
145
|
+
|
|
146
|
+
// or specifying config
|
|
147
|
+
$ sagrep --config=examples/basics/ast/
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+

|
|
151
|
+
|
|
152
|
+
The complete source file is:
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
print("debug")
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The YAML rule matches `print($A)`, where `$A` stands for one argument. The Scan exits `0`:
|
|
159
|
+
|
|
160
|
+
```text
|
|
161
|
+
Result: PASSED (0 errors, 1 warning)
|
|
162
|
+
|
|
163
|
+
Summary: 1 finding in 1 file
|
|
164
|
+
Error: 0 / Warning: 1 / Info: 0 / Hint: 0
|
|
165
|
+
|
|
166
|
+
---
|
|
167
|
+
[Warning] no-debug-print
|
|
168
|
+
Remove debug logging
|
|
169
|
+
at examples/basics/ast/source/example.py:1:1
|
|
170
|
+
|
|
171
|
+
1 | print("debug")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
Completed.
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
More examples: [Linting examples](docs/why-sagrep.md).
|
|
178
|
+
|
|
179
|
+
## How sagrep works
|
|
180
|
+
|
|
181
|
+
- **Scan** runs saved YAML AST and Text rules and evaluates YAML and Comment Annotations.
|
|
182
|
+
- **Direct Run** takes one search condition from the command line. Use `--run=ast` for a code pattern or `--run=text` for a text search.
|
|
183
|
+
|
|
184
|
+
Both modes use ast-grep for AST matching and sagrep's Text engine for file text. Annotations are evaluated during Scan.
|
|
185
|
+
|
|
186
|
+
```mermaid
|
|
187
|
+
flowchart LR
|
|
188
|
+
Scan["Scan"] -->|YAML AST rule| AST["AST engine (ast-grep)"]
|
|
189
|
+
Scan -->|YAML Text rule| Text["Text engine"]
|
|
190
|
+
Scan --> YamlAnn["YAML Annotation"]
|
|
191
|
+
Scan --> CommentAnn["Comment Annotation"]
|
|
192
|
+
YamlAnn --> Ann["Annotation evaluation"]
|
|
193
|
+
CommentAnn --> Ann
|
|
194
|
+
Direct["Direct Run"] -->|--run=ast| AST
|
|
195
|
+
Direct -->|--run=text| Text
|
|
196
|
+
AST --> Report["Findings report"]
|
|
197
|
+
Text --> Report
|
|
198
|
+
Ann --> Report
|
|
199
|
+
|
|
200
|
+
classDef evaluation fill:#DBEAFE,stroke:#2563EB,color:#172554
|
|
201
|
+
classDef result fill:#DCFCE7,stroke:#15803D,color:#14532D
|
|
202
|
+
classDef workflow fill:#F3E8FF,stroke:#9333EA,color:#3B0764
|
|
203
|
+
classDef annotation fill:#FEF3C7,stroke:#B45309,color:#451A03
|
|
204
|
+
|
|
205
|
+
class Ann,AST,Text evaluation
|
|
206
|
+
class Report result
|
|
207
|
+
class Scan,Direct workflow
|
|
208
|
+
class YamlAnn,CommentAnn annotation
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
See [Workflows and engines](docs/search/scan.md#workflows-and-engines) for what each check reads, how annotations choose a scope, and which workflow to start with.
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
## Run checks and search code
|
|
215
|
+
|
|
216
|
+
Run the examples from the `sagrep` project directory after [installing sagrep](#requirements-and-installation). Choose [Scan](#scan-run-saved-checks) to enforce saved checks or [Direct Run](#direct-run-search-from-the-command-line) to test a single condition.
|
|
217
|
+
|
|
218
|
+
### Scan: run saved checks
|
|
219
|
+
|
|
220
|
+
Run Scan for repeatable checks in code review or CI. Scan is the default workflow. It reads project settings, selects source files, runs the applicable YAML AST rules, YAML Text rules, and annotations, then combines their findings in one report.
|
|
221
|
+
|
|
222
|
+
The [Comment Annotation example](examples/basics/comment/) includes this source file:
|
|
223
|
+
|
|
224
|
+
```javascript
|
|
225
|
+
// @todo: Handle errors
|
|
226
|
+
function run() {
|
|
227
|
+
return 1;
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
Run the saved project:
|
|
232
|
+
|
|
233
|
+
```console
|
|
234
|
+
$ sagrep --config=examples/basics/comment/
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
The scan exits `0` and reports one warning at the annotation's declaration:
|
|
238
|
+
|
|
239
|
+
```text
|
|
240
|
+
Result: PASSED (0 errors, 1 warning)
|
|
241
|
+
|
|
242
|
+
Summary: 1 finding in 1 file
|
|
243
|
+
Error: 0 / Warning: 1 / Info: 0 / Hint: 0
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
[Warning] Message(@todo)
|
|
247
|
+
Handle errors
|
|
248
|
+
declared at examples/basics/comment/source/example.js:1:1
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
Completed.
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
`--config` accepts a settings file or its directory.
|
|
255
|
+
|
|
256
|
+
For project setup and scanning without JSON settings, see the [Scan guide](docs/search/scan.md).
|
|
257
|
+
|
|
258
|
+
### Direct Run: search from the command line
|
|
259
|
+
|
|
260
|
+
Use Direct Run to test a condition against one or more paths. For example, this command finds JavaScript calls to `emit` with one argument:
|
|
261
|
+
|
|
262
|
+
```console
|
|
263
|
+
$ sagrep --run=ast --run-lang=JavaScript --run-pattern='emit($A)' examples/engines/ast/source/calls.js
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
Use `--run=text` with a literal pattern or a regular expression to search file contents. The [Direct Run guide](docs/search/run.md) shows the inputs and full reports for both modes, including how to narrow AST matches with a regex.
|
|
267
|
+
|
|
268
|
+
### Filter: choose which rules run
|
|
269
|
+
|
|
270
|
+
Use `--rule-filter` to select saved rules by ID, filename, or annotation directive. The [rule filter guide](docs/search/filtering.md#run-a-filtered-scan) shows the command and complete report for selecting a single AST rule.
|
|
271
|
+
|
|
272
|
+
### Find: choose which source files are checked
|
|
273
|
+
|
|
274
|
+
Use `--find` to select source files by name, path, time, or size. The [file selection guide](docs/search/filtering.md#negation-combinations-and-complete-commands) shows how to include JavaScript files, exclude one by name, and read the resulting report.
|
|
275
|
+
|
|
276
|
+
## Rules and annotations
|
|
277
|
+
|
|
278
|
+
A rule defines a reusable check: what to match, where it applies, and what message and severity to report. Use a YAML AST rule when the shape of the code matters. Use a YAML Text rule when the characters themselves are the condition.
|
|
279
|
+
|
|
280
|
+
An annotation declares a requirement for a particular source scope or set of files. It can check source content, opt in to a named AST rule, or add a note to the Scan report. For example, a rule can flag debug logging across a project, while an annotation can require a validation step in one method.
|
|
281
|
+
|
|
282
|
+
| Feature | Where to define it | Use it for |
|
|
283
|
+
| --- | --- | --- |
|
|
284
|
+
| **YAML AST rule** `engine: ast` | A YAML rule with an ast-grep condition | Find function calls or check that a call is inside an `if`. [AST rules](docs/rules/ast.md) |
|
|
285
|
+
| **YAML Text rule** `engine: text` | A YAML rule with a literal or regex | Match comment markers, config values, or text in logs and docs. [Text rules](docs/rules/text.md) |
|
|
286
|
+
| **YAML Annotation** | A `dispatch` declaration in a source-local Scan rule file | Check selected files or add a report message. [YAML guide](docs/annotations/yaml.md) |
|
|
287
|
+
| **Comment Annotation** | A source comment | Check the surrounding code scope or add a report message. [Comment guide](docs/annotations/comments.md) |
|
|
288
|
+
|
|
289
|
+
## YAML rules
|
|
290
|
+
|
|
291
|
+
Authored YAML files define your checks. The native YAML configuration, `sgconfig.yml`, tells ast-grep where to load generated rules. For example, the basic AST sample uses this setting:
|
|
292
|
+
|
|
293
|
+
```yaml
|
|
294
|
+
ruleDirs:
|
|
295
|
+
- .gen
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
The separate JSON file, `sag-config.json`, selects source paths and the rule environment. See [Scan setup](docs/search/scan.md) for the project layout.
|
|
299
|
+
|
|
300
|
+
An ordinary rule defines a check that can run wherever the rule applies. For example, the [first Scan rule](examples/basics/ast/.rules/check.yml) searches Python code for a call to `print` with one argument and reports a warning:
|
|
301
|
+
|
|
302
|
+
```yaml
|
|
303
|
+
id: no-debug-print
|
|
304
|
+
language: Python
|
|
305
|
+
severity: warning
|
|
306
|
+
message: Remove debug logging
|
|
307
|
+
rule:
|
|
308
|
+
pattern: print($A)
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
AST is the default engine. The [basic Text rule](examples/basics/text/.rules/check.yml) sets `engine: text` and uses a literal `pattern`. Text rules also accept `regex`. Choose a guide to[write AST rules](docs/rules/ast.md) or [write Text rules](docs/rules/text.md). For directives that apply to selected files, see [YAML Annotations](#yaml-annotations).
|
|
312
|
+
|
|
313
|
+
## Annotations
|
|
314
|
+
|
|
315
|
+
Both annotation forms run during Scan. `require` and `deny` check for required or forbidden text in parsed source, `apply` selects an AST component, and `todo` and `warning` add messages. The [annotation overview](docs/annotations/overview.md) explains each directive.
|
|
316
|
+
|
|
317
|
+
### YAML Annotations
|
|
318
|
+
|
|
319
|
+
Declare a requirement centrally when it should apply to selected files without adding source comments. Put `dispatch` in a source-local Scan rule file. This [sample declaration](examples/basics/yaml/source/.checks/check.yml) checks all of `example.py` for `validate`:
|
|
320
|
+
|
|
321
|
+
```yaml
|
|
322
|
+
dispatch:
|
|
323
|
+
language: Python
|
|
324
|
+
files: [./example.py]
|
|
325
|
+
items:
|
|
326
|
+
- require: validate
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
```console
|
|
330
|
+
$ sagrep --config=examples/basics/yaml/
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
The complete Python source selected by `files` is:
|
|
334
|
+
|
|
335
|
+
```python
|
|
336
|
+
def run():
|
|
337
|
+
return 1
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
The Scan exits `1` and points to the YAML declaration because `validate` is missing:
|
|
341
|
+
|
|
342
|
+
```text
|
|
343
|
+
Result: FAILED (1 error to fix)
|
|
344
|
+
|
|
345
|
+
Summary: 1 finding in 1 file
|
|
346
|
+
Error: 1 / Warning: 0 / Info: 0 / Hint: 0
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
[Error] Annotation (@require / yaml)
|
|
350
|
+
Required text "validate" was not found in path "source/".
|
|
351
|
+
declared at examples/basics/yaml/source/.checks/check.yml:5:7
|
|
352
|
+
|
|
353
|
+
- | validate
|
|
354
|
+
|
|
355
|
+
|
|
356
|
+
Completed.
|
|
357
|
+
```
|
|
358
|
+
|
|
359
|
+
The sample uses `"rules_dir": ".checks"` in `sag-config.json` so the report can show the declaration location. YAML Annotations still run when Comment Annotations are disabled. See [YAML Annotations](docs/annotations/yaml.md) for file selection and component reuse.
|
|
360
|
+
|
|
361
|
+
### Comment Annotations
|
|
362
|
+
|
|
363
|
+
Place a requirement in a source comment to check the surrounding method, class, or file. In the [sample file](examples/annotations/comments/common/source/sample.js), this block precedes `run()`:
|
|
364
|
+
|
|
365
|
+
```javascript
|
|
366
|
+
/*
|
|
367
|
+
* @fire: method-value
|
|
368
|
+
* @require: method-missing
|
|
369
|
+
* @deny: method-value
|
|
370
|
+
*/
|
|
371
|
+
run() {
|
|
372
|
+
emit("TOKEN");
|
|
373
|
+
|
|
374
|
+
return "method-value marker";
|
|
375
|
+
}
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Select the `require` directives to see the method and class findings:
|
|
379
|
+
|
|
380
|
+
```console
|
|
381
|
+
$ sagrep --config=examples/annotations/comments/common/ --rule-filter=@require
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
The Scan exits `1` because `run()` has no `method-missing` text. The separate class requirement produces an Info finding. See the [complete report](docs/why-sagrep.md#write-the-requirement-next-to-the-code) and [Comment Annotations](docs/annotations/comments.md) for scope and discovery.
|
|
385
|
+
|
|
386
|
+
## YAML extensions
|
|
387
|
+
|
|
388
|
+
[`occurs`](docs/rules/occurs.md) counts matches across the files a rule selects. To allow at most two matches, report a finding above two:
|
|
389
|
+
|
|
390
|
+
```yaml
|
|
391
|
+
occurs:
|
|
392
|
+
gt: 2
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
Use `base` to share settings, `languages` to reuse a pattern across languages, and `component: true` with annotation `apply` to run an AST rule where requested. The [shared YAML guide](docs/rules/yaml_extend.md) explains file selection and shared settings. See [AST components](docs/rules/ast.md#run-an-ast-rule-where-an-annotation-selects-it) for a reusable check.
|
|
396
|
+
|
|
397
|
+
## Result output
|
|
398
|
+
|
|
399
|
+
sagrep prints a text report by default. `Result` shows whether the check passed, `Summary` shows the finding counts, and each finding shows its message and any available location. Run this Direct Text search from the project directory:
|
|
400
|
+
|
|
401
|
+
```console
|
|
402
|
+
$ sagrep --run=text --run-pattern=TODO examples/basics/text/source/example.txt
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
```text
|
|
406
|
+
Result: PASSED (0 errors)
|
|
407
|
+
|
|
408
|
+
Summary: 1 finding in 1 file
|
|
409
|
+
Error: 0 / Warning: 0 / Info: 1 / Hint: 0
|
|
410
|
+
|
|
411
|
+
---
|
|
412
|
+
[Info]
|
|
413
|
+
Triggered - TODO
|
|
414
|
+
at examples/basics/text/source/example.txt:1:1
|
|
415
|
+
|
|
416
|
+
1 | TODO: handle errors
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
Completed.
|
|
420
|
+
```
|
|
421
|
+
|
|
422
|
+
The search exits `0`. Warnings also pass. An error finding or an execution error produces exit status `1`. See [text report format and exit codes](docs/reports/output.md#text-presentation-and-limits).
|
|
423
|
+
|
|
424
|
+
### Save a text report
|
|
425
|
+
|
|
426
|
+
Use `--write=FILE` to save the report. This command saves the same report to `report.txt` in the current directory:
|
|
427
|
+
|
|
428
|
+
```console
|
|
429
|
+
$ sagrep --run=text --run-pattern=TODO examples/basics/text/source/example.txt --write=report.txt
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
### Colored text output
|
|
433
|
+
|
|
434
|
+
Use `--color` to distinguish status and severity in the terminal. This Scan selects the JavaScript message annotations and requires GNU `find` on `PATH`:
|
|
435
|
+
|
|
436
|
+
```console
|
|
437
|
+
$ sagrep --config=examples/annotations/messages/ --find=name=javascript.js --color
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+

|
|
441
|
+
|
|
442
|
+
The Scan exits `1` because it reports one error. See the [color guide](docs/reports/output.md#colored-text-output) for the severity colors.
|
|
443
|
+
|
|
444
|
+
### JSON for automation
|
|
445
|
+
|
|
446
|
+
For scripts and CI, use `--json`. See [JSON reports](docs/reports/output.md#json-report) for complete examples and field definitions.
|
|
447
|
+
|
|
448
|
+
## HTML reports with `sagrep-report`
|
|
449
|
+
|
|
450
|
+
`sagrep-report` turns a Scan report into an HTML file. In the browser, you can filter findings by severity or rule, group them by file, inspect source excerpts, and copy locations.
|
|
451
|
+
|
|
452
|
+
From the sagrep project directory, pipe the sample Scan into the converter:
|
|
453
|
+
|
|
454
|
+
```console
|
|
455
|
+
$ sagrep --config=examples/configuration/report-metadata/ --json | sagrep-report
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
Open `report.html` in the current directory. The sample is titled **Release checks** and shows one warning for the `DRAFT` marker.
|
|
459
|
+
|
|
460
|
+

|
|
461
|
+
|
|
462
|
+
For a larger example, the [failed Scan walkthrough](docs/reports/html-reports.md#review-a-failed-scan) shows how to filter the report to `@deny` and `@require` findings and group them by rule. You can also open the [example report](docs/reports/example.html) or follow the [HTML report guide](docs/reports/html-reports.md#save-a-report-and-open-it-in-a-browser) to generate HTML from a saved report.
|
|
463
|
+
|
|
464
|
+
## Verbose mode
|
|
465
|
+
|
|
466
|
+
Use `--verbose` to see which files and rules ran. Diagnostics go to stderr. The [verbose guide](docs/configuration/verbose.md#read-the-report-and-diagnostics-together) shows how to read the normal report alongside the diagnostics and choose the right level.
|
|
467
|
+
|
|
468
|
+
## Documentation
|
|
469
|
+
|
|
470
|
+
See the [documentation index](docs/README.md) for a guide by task.
|
|
471
|
+
|
|
472
|
+
| Area | Guides |
|
|
473
|
+
| --- | --- |
|
|
474
|
+
| Start here | [Linting examples](docs/why-sagrep.md), [Getting started](docs/getting-started.md) |
|
|
475
|
+
| Configuration | [CLI options](docs/configuration/cli.md), [JSON settings and paths](docs/configuration/config.md), [verbose logging](docs/configuration/verbose.md) |
|
|
476
|
+
| Search | [Configure a Scan project](docs/search/scan.md), [Direct Run](docs/search/run.md), [filter source files and rules](docs/search/filtering.md), [AST path compatibility](docs/search/ast-path-compatibility.md) |
|
|
477
|
+
| Rules | [Write AST rules](docs/rules/ast.md), [write Text rules](docs/rules/text.md), [select rules and count matches](docs/rules/filters-and-counts.md) |
|
|
478
|
+
| Annotations | [Concepts and directives](docs/annotations/overview.md), [YAML](docs/annotations/yaml.md), [Comment](docs/annotations/comments.md), [language names and extensions](docs/languages.md#annotation-language-names-and-extensions) |
|
|
479
|
+
| Reports | [Report format and exit codes](docs/reports/output.md), [HTML reports](docs/reports/html-reports.md) |
|
|
480
|
+
| Python integration | [Python runner](docs/python/runner.md), [Python string search](docs/python/string-search.md) |
|
|
481
|
+
|
|
482
|
+
## Examples
|
|
483
|
+
|
|
484
|
+
The [example catalog](examples/README.md) groups runnable projects by rules, annotations, configuration, and Python usage. Each sample explains its inputs, command, and expected result.
|
|
485
|
+
|
|
486
|
+
## Skills
|
|
487
|
+
|
|
488
|
+
sagrep includes two skills for AI coding assistants. The skills explain how to use the CLI and verify findings while working in a codebase.
|
|
489
|
+
|
|
490
|
+
- [sagrep](skills/sagrep/SKILL.md) covers running scans, interpreting reports, and configuring projects.
|
|
491
|
+
- [sagrep-rule](skills/sagrep-rule/SKILL.md) helps you write and validate YAML rules and annotations.
|
|
492
|
+
|
|
493
|
+
See the [Skills guide](docs/skills.md) for example requests and when to use each skill.
|
|
494
|
+
|
|
495
|
+
## Project layout
|
|
496
|
+
|
|
497
|
+
- [src](src/) contains the `sagrep` and `sagrep_report` packages.
|
|
498
|
+
- [docs](docs/README.md), [examples](examples/README.md), and [skills](skills/) cover usage.
|
|
499
|
+
- [Testing guide](tests/TESTING.md) covers setup, verification commands, and runnable examples.
|
|
500
|
+
|
|
501
|
+
## License
|
|
502
|
+
|
|
503
|
+
Licensed under the [MIT License](LICENSE).
|