Skip to content

Commit 0b8dab1

Browse files
doc: add string style transformation
1 parent 07efa4c commit 0b8dab1

File tree

3 files changed

+74
-16
lines changed

3 files changed

+74
-16
lines changed

website/guide/rewrite/transform.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ transform:
2626
endChar: -1
2727
```
2828
29+
ast-grep 0.38.3+ supports string style transformations to simplify rule writing.
30+
The above example can be simplified to one-line style like:
31+
32+
```yaml
33+
transfrom:
34+
NEW_VAR: replace($VAR_NAME, replace=regex, by=replacement)
35+
ANOTHER_NEW_VAR: substring($NEW_VAR, startChar=1, endChar=-1)
36+
```
2937
3038
## Example of Converting Generator in Python
3139
@@ -164,6 +172,32 @@ If `$$$ARGS` does match nodes, then the replacement regular expression will repl
164172
This method is invented by [Surma](https://surma.dev/) in a [tweet](https://twitter.com/DasSurma/status/1706086320051794217), so the useful trick is named after him.
165173
:::
166174

175+
## String Style Transformations
176+
177+
To simplify the syntax of transformations, ast-grep 0.38.3+ supports a new string style transformation syntax. This allows us to write transformations in a more concise and readable way.
178+
179+
The string style transformation syntax is similar to the CSS function call syntax
180+
181+
```yaml
182+
# illustration of string style transformation syntax
183+
NEW_VAR: transform($SOURCE_VAR, option1=value1, option2=value2)
184+
```
185+
186+
The transformation name is followed by parentheses containing the arguments. The first argument is always the source meta-variable, and the rest are the transformation options in the form of key-value pairs.
187+
188+
For example, the transformation examples above can be written as:
189+
190+
```yaml
191+
transform:
192+
LIST: substring($GEN, startChar=1, endChar=-1)
193+
KEBABED: convert($OLD_FN, toCase=kebabCase)
194+
MAYBE_COMMA: replace($$$ARGS, replace='^.+', by=', ')
195+
```
196+
197+
:::warning
198+
The string style transformation syntax is only available in ast-grep 0.38.3 and later versions. If you are using an older version, please use the original object style syntax.
199+
:::
200+
167201
## Even More Advanced Transformations
168202

169203
We can use rewriters in the [`rewrite`](/guide/rewrite/rewriter.html) transformation to apply dynamic transformations to the AST. We will cover it in next section.

website/reference/yaml.md

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ An ast-grep rule is a YAML object with the following keys:
2121

2222
Unique, descriptive identifier, e.g., `no-unused-variable`.
2323

24-
Example:
24+
**Example:**
2525
```yaml
2626
id: no-console-log
2727
```
@@ -35,7 +35,7 @@ Specify the language to parse and the file extension to include in matching.
3535

3636
Valid values are: `C`, `Cpp`, `CSharp`, `Css`, `Go`, `Html`, `Java`, `JavaScript`, `Kotlin`, `Lua`, `Python`, `Rust`, `Scala`, `Swift`, `Thrift`, `Tsx`, `TypeScript`
3737

38-
Example:
38+
**Example:**
3939
```yaml
4040
language: JavaScript
4141
```
@@ -64,7 +64,7 @@ Additional meta variables pattern to filter matches. The key is matched meta var
6464
**Note, constraints only applies to the single meta variable like `$ARG`,** not multiple meta variable like `$$$ARGS`.
6565
So the key name must only refer to a single meta variable.
6666

67-
Example:
67+
**Example:**
6868

6969
```yaml
7070
rule:
@@ -91,7 +91,7 @@ A dictionary of utility rules that can be used in `matches` locally.
9191
The dictionary key is the utility rule id and the value is the rule object.
9292
See [utility rule guide](/guide/rule-config/utility-rule).
9393

94-
Example:
94+
**Example:**
9595

9696
```yaml
9797
utils:
@@ -110,18 +110,22 @@ utils:
110110
* required: false
111111

112112
A dictionary to manipulate meta-variables. The dictionary key is the new variable name.
113-
The dictionary value is a transformation object that specifies how meta var is processed.
113+
The dictionary value is a transformation object or transformation string that specifies how meta var is processed.
114114

115115
Please also see [transformation reference](/reference/yaml/transformation) for details.
116116

117-
Example:
117+
**Example:**
118118
```yaml
119119
transform:
120120
NEW_VAR_NAME: # new variable name
121121
replace: # transform operation
122122
source: $ARGS
123123
replace: '^.+'
124124
by: ', '
125+
126+
# string style for ast-grep 0.38.3+
127+
transform:
128+
NEW_VAR_NAME: replace($ARGS, replace='^.+', by=', ')
125129
```
126130

127131
### `fix`
@@ -133,15 +137,16 @@ A pattern or a `FixConfig` object to auto fix the issue. See details in [fix obj
133137

134138
It can reference meta variables that appeared in the rule.
135139

136-
Example:
140+
**Example:**
141+
137142
```yaml
138143
fix: logger.log($$$ARGS)
139144
140145
# you can also use empty string to delete match
141146
fix: ""
142147
```
143148

144-
### `rewriters` <Badge type="warning" text="Experimental" />
149+
### `rewriters`
145150
* type: `Array<Rewriter>`
146151
* required: false
147152

@@ -151,7 +156,7 @@ A rewriter rule is similar to ordinary YAML rule, but it ony contains _finding_
151156

152157
Please also see [rewriter reference](/reference/yaml/rewriter.html) for details.
153158

154-
Example:
159+
**Example:**
155160
```yaml
156161
rewriters:
157162
- id: stringify
@@ -172,7 +177,7 @@ Specify the level of matched result. Available choice: `hint`, `info`, `warning`
172177

173178
When `severity` is `off`, ast-grep will disable the rule in scanning.
174179

175-
Example:
180+
**Example:**
176181
```yaml
177182
severity: warning
178183
```
@@ -187,7 +192,7 @@ but specific enough to be understood without additional context.
187192

188193
It can reference meta-variables that appeared in the rule.
189194

190-
Example:
195+
**Example:**
191196
```yaml
192197
message: "console.log should not be used in production code"
193198
```
@@ -199,7 +204,9 @@ message: "console.log should not be used in production code"
199204

200205
Additional notes to elaborate the message and provide potential fix to the issue.
201206

202-
Example:
207+
`note` can contains markdown syntax, but it _cannot_ reference meta-variables.
208+
209+
**Example:**
203210
```yaml
204211
note: "Use a logger instead"
205212
```
@@ -213,7 +220,7 @@ A dictionary of labels to customize highlighting. The dictionary key is the meta
213220
* `style`: (required) the style of the label. Available choice: `primary`, `secondary`.
214221
* `message`: (optional) the message to be displayed in the editor extension.
215222

216-
Example:
223+
**Example:**
217224
```yaml
218225
labels:
219226
ARG:
@@ -251,6 +258,7 @@ Be sure to remove `./` to the beginning of your rules. ast-grep will not recogni
251258
* type: `Array<String>`
252259
* required: false
253260

261+
**Example:**
254262
```yaml
255263
ignores:
256264
- test/**/*.js
@@ -281,7 +289,7 @@ To disable this behavior, use [`--no-ignore`](/reference/cli.html#scan) in CLI.
281289

282290
Documentation link to this rule. It will be displayed in editor extension if supported.
283291

284-
Example:
292+
**Example:**
285293

286294
```yaml
287295
url: 'https://ast-grep.github.io/catalog/python/#migrate-openai-sdk'
@@ -295,7 +303,7 @@ Extra information for the rule. This section can include custom data for externa
295303

296304
ast-grep will output `metadata` with matches in [`--json`](/reference/cli/scan.html#json-style) mode if [`--include-metadata`](/reference/cli/scan.html#include-metadata) is on.
297305

298-
Example:
306+
**Example:**
299307

300308
```yaml
301309
metadata:

website/reference/yaml/transformation.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ transform:
4949
replace: regex
5050
by: replacement
5151
source: $VAR
52+
53+
# string style for ast-grep 0.38.3+
54+
transform:
55+
NEW_VAR: replace($VAR, replace=regex, by=replacement)
5256
```
5357
5458
:::tip Pro tip
@@ -93,6 +97,10 @@ transform:
9397
startChar: 1
9498
endChar: -1
9599
source: $VAR
100+
101+
# string style for ast-grep 0.38.3+
102+
transform:
103+
NEW_VAR: substring($VAR, startChar=1, endChar=-1)
96104
```
97105

98106
:::tip Pro Tip
@@ -189,6 +197,10 @@ transform:
189197
toCase: kebabCase
190198
separatedBy: [underscore]
191199
source: $VAR
200+
201+
# string style for ast-grep 0.38.3+
202+
transform:
203+
NEW_VAR: convert($VAR, toCase=kebabCase, separatedBy=[underscore])
192204
```
193205

194206
Suppose we have a string `ast_Grep` as the input `$VAR`, The example above will convert the string as following:
@@ -198,7 +210,7 @@ Suppose we have a string `ast_Grep` as the input `$VAR`, The example above will
198210

199211
Thank [Aarni Koskela](https://github.com/akx) for proposing and implementing the first version of this feature!
200212

201-
## `rewrite` <Badge type="warning" text="Experimental" />
213+
## `rewrite`
202214

203215
`rewrite` is an experimental transformation that allows you to selectively transform a meta variable by `rewriter` rules.
204216
Instead of rewriting the single target node which matches the rule, `rewrite` can rewrite a subset of AST captured by a meta-variable.
@@ -250,6 +262,10 @@ transform:
250262
source: $VAR
251263
rewriters: [rule1, rule2]
252264
joinBy: "\n"
265+
266+
# string style for ast-grep 0.38.3+
267+
transform:
268+
NEW_VAR: rewrite($VAR, rewriters=[rule1, rule2], joinBy='\n')
253269
```
254270
255271
Thank [Eitan Mosenkis](https://github.com/emosenkis) for proposing this idea!

0 commit comments

Comments
 (0)