ast-search-python 0.1.0 → 1.0.1
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.
- package/README.md +107 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
# ast-search-python
|
|
2
|
+
|
|
3
|
+
Python language plugin for [ast-search](../../README.md). Adds `.py` / `.pyw` file support using [tree-sitter](https://tree-sitter.github.io/tree-sitter/) S-expression queries.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install -g ast-search
|
|
9
|
+
npm install -g ast-search-python
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
Pass `--plugin ast-search-python` to enable Python file support:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
ast-search <query> --plugin ast-search-python [--dir <path>] [--format <fmt>] [--lang python]
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
| Argument | Description | Default |
|
|
21
|
+
| -------------- | -------------------------------------------------------------- | ----------- |
|
|
22
|
+
| `<query>` | Shorthand or tree-sitter S-expression (see Query Syntax below) | required |
|
|
23
|
+
| `-d, --dir` | Root directory to search | current dir |
|
|
24
|
+
| `-f, --format` | Output format: `text`, `json`, or `files` | `text` |
|
|
25
|
+
| `-l, --lang` | Restrict search to `python` only (useful in mixed-language repos) | all languages |
|
|
26
|
+
| `-p, --plugin` | `ast-search-python` | required |
|
|
27
|
+
|
|
28
|
+
## Example
|
|
29
|
+
|
|
30
|
+
Find all functions that `raise` an exception:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
ast-search '(function_definition body: (block (raise_statement) @r) @b) @fn' --plugin ast-search-python
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Using the shorthand:
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
ast-search 'raise' --plugin ast-search-python
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Query syntax
|
|
43
|
+
|
|
44
|
+
Python queries use tree-sitter S-expression syntax. A few examples:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
# Find all function definitions (shorthand)
|
|
48
|
+
ast-search 'fn' --plugin ast-search-python
|
|
49
|
+
|
|
50
|
+
# Find all class definitions (raw S-expression)
|
|
51
|
+
ast-search '(class_definition) @cls' --plugin ast-search-python
|
|
52
|
+
|
|
53
|
+
# Find all calls to a specific function by name
|
|
54
|
+
ast-search '(call function: (identifier) @n (#eq? @n "my_func")) @c' --plugin ast-search-python
|
|
55
|
+
|
|
56
|
+
# Restrict to only Python files in a mixed-language repo
|
|
57
|
+
ast-search 'fn' --lang python --plugin ast-search-python
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Raw S-expression queries must include at least one `@capture_name` — tree-sitter's `captures()` requires it to return results. Shorthands include `@_` automatically.
|
|
61
|
+
|
|
62
|
+
### Shorthands
|
|
63
|
+
|
|
64
|
+
| Shorthand | Expands to |
|
|
65
|
+
| ----------- | ----------------------------------- |
|
|
66
|
+
| `fn` | `(function_definition) @_` |
|
|
67
|
+
| `call` | `(call) @_` |
|
|
68
|
+
| `class` | `(class_definition) @_` |
|
|
69
|
+
| `assign` | `(assignment) @_` |
|
|
70
|
+
| `return` | `(return_statement) @_` |
|
|
71
|
+
| `await` | `(await) @_` |
|
|
72
|
+
| `yield` | `(yield) @_` |
|
|
73
|
+
| `import` | `(import_statement) @_` |
|
|
74
|
+
| `from` | `(import_from_statement) @_` |
|
|
75
|
+
| `if` | `(if_statement) @_` |
|
|
76
|
+
| `for` | `(for_statement) @_` |
|
|
77
|
+
| `while` | `(while_statement) @_` |
|
|
78
|
+
| `raise` | `(raise_statement) @_` |
|
|
79
|
+
| `with` | `(with_statement) @_` |
|
|
80
|
+
| `lambda` | `(lambda) @_` |
|
|
81
|
+
| `decorator` | `(decorator) @_` |
|
|
82
|
+
| `augassign` | `(augmented_assignment) @_` |
|
|
83
|
+
| `comp` | `(list_comprehension) @_` |
|
|
84
|
+
| `dictcomp` | `(dictionary_comprehension) @_` |
|
|
85
|
+
| `setcomp` | `(set_comprehension) @_` |
|
|
86
|
+
| `genexp` | `(generator_expression) @_` |
|
|
87
|
+
| `assert` | `(assert_statement) @_` |
|
|
88
|
+
| `delete` | `(delete_statement) @_` |
|
|
89
|
+
| `global` | `(global_statement) @_` |
|
|
90
|
+
| `nonlocal` | `(nonlocal_statement) @_` |
|
|
91
|
+
| `decorated` | `(decorated_definition) @_` |
|
|
92
|
+
|
|
93
|
+
> **Note:** In tree-sitter-python 0.21+, `async def` functions are typed as `function_definition` (no separate `async_function_definition` node). The `fn` shorthand matches both sync and async functions. To match only async functions, use a predicate query.
|
|
94
|
+
|
|
95
|
+
## Supported file types
|
|
96
|
+
|
|
97
|
+
`.py` `.pyw`
|
|
98
|
+
|
|
99
|
+
## Plugin API
|
|
100
|
+
|
|
101
|
+
This package implements the `LanguageBackend` interface from `ast-search/plugin`. It registers the `python` language backend automatically when loaded via `--plugin ast-search-python`, or programmatically:
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
import { defaultRegistry } from 'ast-search/plugin';
|
|
105
|
+
const { register } = await import('ast-search-python');
|
|
106
|
+
register(defaultRegistry);
|
|
107
|
+
```
|