Match pair including "," #746
-
I am trying to remove an entry with a certain key in dictionaries. While matching the pair works, this leaves the trailing (or preceding) ",", leading to invalid code. Is there a way to match both the pair and the ","? Example: obj = {
"unrelated": val,
"target": "something",
"also": "unrelated"
} Matches pair, not including ",": language: python
rule:
kind: pair
has:
field: key
regex: "target"
fix: "" Does not match at all: language: python
rule:
pattern: "$A: $B, $C: $D"
constraints:
C:
regex: "target"
fix: "$A: $B" Playground link: here |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Hi @boidolr , this is not straightforward to match comma in ast-grep. There is a DasSurma trick we can apply here. The idea of the trick is using regex to conditionally replace/add commas. A Ruby example . However, I find it hard to apply the trick in Python. Since |
Beta Was this translation helpful? Give feedback.
-
Ok, that's a pity. Thank you for the clarification. |
Beta Was this translation helpful? Give feedback.
-
Hi @boidolr , #843 will fix this! |
Beta Was this translation helpful? Give feedback.
-
This is released in 0.17.0! |
Beta Was this translation helpful? Give feedback.
This is released in 0.17.0!
https://x.com/hd_nvim/status/1745594815583318120