Can I use regex groups in the fix? #477
-
I'm trying to replace a part of the variable name, like rule:
pattern: $A
regex: __PART_TO_REPLACE__(.*)
kind: identifier
fix:
replaced$1 |
Beta Was this translation helpful? Give feedback.
Answered by
HerringtonDarkholme
Jun 27, 2023
Replies: 1 comment
-
Thanks for trying out ast-grep! Using regex group directly in But ast-grep does have a (experimental) workaround.
rule:
pattern: $A
regex: __PART_TO_REPLACE__(.*)
kind: identifier
transform:
CHANGED:
replace:
source: $A
replace: __PART_TO_REPLACE__(.*)
by: 'replaced$1'
fix:
$CHANGED |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Lodin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for trying out ast-grep!
Using regex group directly in
fix
is not possible. Because we can have multipleregex
sub-rules in one YAML. It is not clear which group$1
refers to.But ast-grep does have a (experimental) workaround.
transform
is used to manipulate string in a matched variable. Refer to #436 (comment) for more explanation.https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6ImphdmFzY3JpcHQiLCJxdWVyeSI6ImNvbnNvbGUubG9nKCRNQVRDSCkiLCJyZXdyaXRlIjoibG9nZ…