Skip to content

Commit f08cdd6

Browse files
doc: update using meta var
1 parent e2e54d3 commit f08cdd6

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

website/guide/rewrite-code.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ This allows you to group related rules together!
8585

8686
As we saw in the previous example, we can use [meta variables](/guide/pattern-syntax.html#meta-variable-capturing) in both the pattern and the fix parts of a YAML rule. They are like regular expression [capture groups](https://regexone.com/lesson/capturing_groups). Meta variables are identifiers that start with `$`, and they can match any syntactic element in the code, such as expressions, statements, types, etc. When we use a meta variable in the fix part of a rule, it will be replaced by whatever it matched in the pattern part.
8787

88-
:::warning
89-
non-matched meta-variable will be replaced by empty string in the `fix`.
90-
:::
91-
9288
For example, if we have a [rule](https://ast-grep.github.io/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6InB5dGhvbiIsInF1ZXJ5IjoiZGVmIGZvbygkWCk6XG4gICRTIiwicmV3cml0ZSI6ImxvZ2dlci5sb2coJE1BVENIKSIsImNvbmZpZyI6ImlkOiBzd2FwXG5sYW5ndWFnZTogUHl0aG9uXG5ydWxlOlxuICBwYXR0ZXJuOiAkWCA9ICRZXG5maXg6ICRZID0gJFgiLCJzb3VyY2UiOiJhID0gYlxuYyA9IGQgKyBlXG5mID0gZyAqIGgifQ==) like this:
9389

9490
```yaml
@@ -119,6 +115,16 @@ g * h = f
119115

120116
Note that this may **not** be a valid or sensible code transformation, but it illustrates how meta variables work.
121117

118+
:::warning Append Uppercase String to Meta Variable
119+
It will not work if you want to append a string starting with uppercase letters to a meta variable because it will be parsed as an undefined meta variable.
120+
:::
121+
122+
Suppose we want to append `Name` to the meta variable `$VAR`, the fix string `$VARName` will be parsed as `$VARN` + `ame` instead. You can instead use [replace transformation](/guide/rewrite/transform.html#rewrite-with-regex-capture-groups) to create a new variable whose content is `$VAR` plus `Name`.
123+
124+
:::danger Non-matched meta-variable
125+
non-matched meta-variable will be replaced by empty string in the `fix`.
126+
:::
127+
122128
### Rewrite is Indentation Sensitive
123129

124130
ast-grep's rewrite is indentation sensitive. That is, the indentation level of a meta-variable in the fix string is preserved in the rewritten code.

0 commit comments

Comments
 (0)