You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,10 +20,11 @@ parallel copies under `docs/` or `scripts/notes/`. At cut time: rename
20
20
### Changed
21
21
22
22
- Restore the scribe skill 1:1 with GaaS. ask_operator mapping stays on native-integration. Slash /scribe remains.
23
+
- Restore the ast-grep skill 1:1 with GaaS. run_shell mapping stays on native-integration. Slash /ast-grep remains.
23
24
- Restore the opsh skill 1:1 with GaaS. Tool/shell mapping stays on native-integration. user-invocable: false stays so it remains use_skill-only.
24
25
- Restore the pull-request-review skill 1:1 with GaaS. ask_operator, /review mapping, and GitHub posting stay on native-integration. Slash /pull-request-review remains.
25
26
- Restore the refactor skill 1:1 with GaaS. ask_operator mapping stays on native-integration. Slash /refactor remains.
26
-
- Ignore GaaS opsh, refactor, and scribe SKILL.md in prettier so table/list alignment stays 1:1.
27
+
- Ignore GaaS opsh, refactor, scribe, and ast-grep SKILL.md in prettier so table/list alignment stays 1:1.
27
28
- Restore the git-rebase skill body 1:1 with GaaS. Intern execution recipe stays on native-integration. user-invocable: false stays so it remains use_skill-only.
28
29
- Restore the linear-issue-workflow skill body 1:1 with GaaS. Claim-first, In Review, and git-worktrees extras stay on native-integration. user-invocable: false stays so it remains use_skill-only.
29
30
- Restore the interview skill body 1:1 with GaaS (AskUserQuestion). Operator-ask mapping stays on native-integration. Slash /interview remains.
Copy file name to clipboardExpand all lines: plugins/corbits-skills/skills/ast-grep/SKILL.md
+43-52Lines changed: 43 additions & 52 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ description: Bulk code refactoring using AST patterns instead of manual read-edi
5
5
6
6
# ast-grep
7
7
8
-
Use `ast-grep` (CLI: `sg`) for structural code search and rewriting. Invoke `sg` via `run_shell`. It matches and transforms code using Abstract Syntax Tree patterns rather than text, so it understands code structure and handles formatting, whitespace, and nesting correctly.
8
+
Use `ast-grep` (CLI: `sg`) for structural code search and rewriting. It matches and transforms code using Abstract Syntax Tree patterns rather than text, so it understands code structure and handles formatting, whitespace, and nesting correctly.
9
9
10
10
If you can describe the change as "rename X to Y" or "change all A-shaped code to B-shaped code," use ast-grep — even if you already know some of the locations. Knowing where the definitions are does not mean you know where all the access sites are.
11
11
@@ -36,12 +36,12 @@ Patterns are code snippets in the target language with metavariable placeholders
|`$NAME`| Matches exactly one AST node, captured as `NAME`|
42
-
|`$_`| Matches one node, not captured|
39
+
| Syntax | Meaning |
40
+
|---|---|
41
+
|`$NAME`| Matches exactly one AST node, captured as `NAME`|
42
+
|`$_`| Matches one node, not captured |
43
43
|`$$$NAME`| Matches zero or more sibling nodes, captured as `NAME`|
44
-
|`$$$`| Matches zero or more siblings, not captured|
44
+
|`$$$`| Matches zero or more siblings, not captured |
45
45
46
46
**Same-name constraint:** Two occurrences of the same metavariable in one pattern must match identical text. `foo($X, $X)` matches `foo(a, a)` but not `foo(a, b)`.
47
47
@@ -79,24 +79,22 @@ The `-U` (`--update-all`) flag applies changes to files without prompting. Witho
sg run -p 'oldName($$$ARGS)' -r 'newName($$$ARGS)' -l js -U src/
98
97
```
99
-
100
98
This pattern only matches `identifier` nodes in call-expression position. It will not catch the name where it appears as a type annotation (`type_identifier`), an interface or object field (`property_identifier`), a destructured binding (`shorthand_property_identifier_pattern`), or an object literal shorthand (`shorthand_property_identifier`). For a name that appears in more than one syntactic position, use the multi-kind rename recipe below.
101
99
102
100
**Rename an identifier across all syntactic positions (TypeScript):**
@@ -126,35 +124,29 @@ fix: NewName
126
124
This is the default approach for renaming a type, class, interface, or any identifier that may surface in more than just call-site position. The inline `sg run -p` form is the shortcut for call-site-only renames.
127
125
128
126
**Change an import source:**
129
-
130
127
```bash
131
128
sg run -p 'import $$$ITEMS from "old-package"' -r 'import $$$ITEMS from "new-package"' -l ts -U src/
132
129
```
133
-
134
130
Use `$$$ITEMS` (not `$ITEMS`) because `import type` inserts an extra `type` node as a sibling before the import clause. `$ITEMS` expects exactly one node in that position and fails when two are present.
135
131
136
132
The symmetric export form does not work inline. `sg run -p 'export $$$ITEMS from "old-package"' -r '...'` fails with "Multiple AST nodes are detected" — the re-export does not parse as a single AST node. For re-export source rewrites, use a YAML rule keyed on `kind: export_statement` with a `has` constraint on the source string, or fall back to manual edits when the file count is small.
| `inside` | Node is a descendant of a matching ancestor |
233
-
| `has` | Node has a descendant matching this |
234
-
| `follows` | Node is preceded by a matching sibling |
235
-
| `precedes` | Node is followed by a matching sibling |
220
+
| Rule | Meaning |
221
+
|---|---|
222
+
| `inside` | Node is a descendant of a matching ancestor |
223
+
| `has` | Node has a descendant matching this |
224
+
| `follows` | Node is preceded by a matching sibling |
225
+
| `precedes` | Node is followed by a matching sibling |
236
226
237
227
All accept `stopBy` with three valid forms: `neighbor`(only check adjacent — the default when omitted), `end` (traverse all the way to the root), or a rule object (e.g., `stopBy: { kind: function_declaration }` to stop at a specific node type).
| `substring` | Extract a substring by char index |
320
+
| `replace` | String find-and-replace within a metavar |
321
+
| `rewrite` | Apply sub-rewriters to a metavar (for nested transformations) |
332
322
333
323
## Debugging Non-Matching Patterns
334
324
335
325
When a pattern does not match what you expect:
336
326
337
327
1. **Inspect your pattern's AST.** Use `--debug-query=pattern` to see how ast-grep parses your pattern:
338
-
339
328
```bash
340
329
sg run --pattern 'your_pattern($X)' --lang js --debug-query=pattern
341
330
```
342
331
343
332
2. **Inspect the source code's AST.** Use the target code itself as the pattern to see its tree structure:
344
-
345
333
```bash
346
334
sg run --pattern 'myFunc(arg1, arg2)' --lang js --debug-query=ast
347
335
```
348
-
349
336
This shows you the node kinds in the source, which tells you what your real pattern needs to match against. Compare the AST of your pattern (step 1) with the AST of the source to find the mismatch.
350
337
351
338
3. **Common causes of non-matches:**
@@ -387,7 +374,7 @@ These steps are mandatory, not advisory. Skipping them is the single most common
387
374
### After applying rewrites
388
375
389
376
5. **Format.** ast-grep rewrites can collapse multi-line formatting to single lines. Run the project's formatter (prettier, rustfmt, gofmt, etc.) after applying rewrites.
390
-
6. **Check for stragglers.** Use `grep` for the old name across all file types — including comments, strings, docs, and test fixtures. ast-grep only matches code structure; occurrences in prose, JSDoc, string literals, and non-code files will be missed.
377
+
6. **Check for stragglers.** Grep for the old name across all file types — including comments, strings, docs, and test fixtures. ast-grep only matches code structure; occurrences in prose, JSDoc, string literals, and non-code files will be missed.
391
378
7. **Run the type checker.** ast-grep matches on syntax, not semantics — it cannot guarantee that every reference to a name has been caught across every syntactic context, and it cannot see scope. In typed languages, run the type checker before the test suite. It is the safety net that surfaces both kinds of miss: occurrences of the old name that the pattern did not anticipate (e.g., type annotations missed by a call-site-only rename), and scope collisions where the new name shadows an existing binding. Without a type checker, these gaps are silent and only show up at runtime.
392
379
8. **Run the full build.** Run the project's build and test suite to catch anything ast-grep's structural matching could not anticipate.
393
380
@@ -404,14 +391,14 @@ ast-grep handles code; prose requires separate attention. Skipping this pass lea
| Call-site-only rename or argument change | `sg run -p ... -r ...` |
394
+
| Situation | Use |
395
+
|---|---|
396
+
| Call-site-only rename or argument change | `sg run -p ... -r ...` |
410
397
| Renaming an identifier that may appear in type annotations, fields, or destructuring | YAML rule with `any:` over the relevant node kinds (see "Rename an identifier across all syntactic positions" above) |
411
-
| Need to exclude certain matches | YAML rule with `not` or `constraints` |
412
-
| Need positional context (inside a function, after an import) | YAML rule with `inside`/`follows`/`precedes` |
413
-
| Need case conversion or string manipulation in the replacement | YAML rule with `transform` |
414
-
| Applying multiple related transformations | Multiple `sg run` commands in sequence, or multiple YAML rules |
398
+
| Need to exclude certain matches | YAML rule with `not` or `constraints` |
399
+
| Need positional context (inside a function, after an import) | YAML rule with `inside`/`follows`/`precedes` |
400
+
| Need case conversion or string manipulation in the replacement | YAML rule with `transform` |
401
+
| Applying multiple related transformations | Multiple `sg run` commands in sequence, or multiple YAML rules |
415
402
416
403
### Combining with manual edits
417
404
@@ -422,3 +409,7 @@ ast-grep handles the bulk structural transformation. Use manual edits for:
422
409
- One-off fixups after a bulk rewrite (e.g., adjusting a special case that the pattern caught incorrectly)
423
410
424
411
The ideal workflow for a large refactor: ast-grep for the mechanical bulk, manual edits for the exceptions, build verification to confirm everything holds together.
412
+
413
+
## Acknowledgment
414
+
415
+
After reviewing this skill, state: "I have reviewed the ast-grep skill."
0 commit comments