https://github.com/squiddy/libcst_easy_matcher
An experiment to create matchers from source code when working with LibCST
https://github.com/squiddy/libcst_easy_matcher
Last synced: 12 months ago
JSON representation
An experiment to create matchers from source code when working with LibCST
- Host: GitHub
- URL: https://github.com/squiddy/libcst_easy_matcher
- Owner: squiddy
- License: mit
- Created: 2020-04-02T04:27:22.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-04-07T05:05:16.000Z (about 6 years ago)
- Last Synced: 2025-03-15T03:48:07.213Z (over 1 year ago)
- Language: Python
- Size: 13.7 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# libcst_easy_matcher
An experiment to create matchers from source code when working with
[LibCST](https://libcst.readthedocs.io/). A matcher enables you to check
whether a certain node in the tree generated by LibCST matches a certain
pattern.
## Status
```python
a = 3
a = __
__ = 3
b = bar(x=3)
b = bar(x=__)
b = __(x=3)
```
See `tests/test_libcst_easy_matcher.py` for all examples.
## Motivation
If I want to check that a statement matches
```python
client["cookies"] = get_cookie(request)
```
I'd write something like this:
```python
m.Assign(
targets=[
m.AssignTarget(
target=m.Subscript(
value=m.Attribute(
value=m.Name("client"),
attr=m.Name("cookies"),
)
)
)
],
value=m.Call(
func=m.Name(
value='get_cookie'
),
args=[
m.Arg(
value=m.Name(
value='request'
)
)
]
)
)
```
I'm experimenting here to instead do it like this:
```python
create_matcher("client['cookies'] = get_cookie(request)")
```
## Placeholders
Just matching against concrete values only get's you so far, so I'm
evaluating the use of placeholders like this:
```python
__ = get_cookie(request)
client[__] = get_cookie(request)
client['cookies'] = __(request)
client['cookies'] = get_cookie(__)
```
All of which should match the example code. `__` might not be the best choice
in the long run, but I needed a valid identifier to be able to still parse
the code with LibCST.
## License
MIT