Goal
ใช้งาน ast-grep สำหรับ structural code search, linting และ rewriting ด้วย AST patterns บน multiple languages
Scope
ใช้สำหรับค้นหา code patterns ด้วย AST-based matching, สร้าง custom lint rules, ทำ code transformation และ refactoring อัตโนมัติ, scan codebase สำหรับ code smells และ anti-patterns, และ integrate กับ editor ผ่าน LSP
Execute
- ติดตั้ง ast-grep ตาม
workflows/update-installation.md
- เรียนรู้ pattern syntax จาก
learn/guide/key-concept.md
- ค้นหา patterns ด้วย
ast-grep -p '$PATTERN' หรือ sg -p '$PATTERN'
- แก้ไข code ด้วย
ast-grep -p '$PATTERN' --rewrite '$REWRITE'
- สร้าง lint rules ตาม
workflows/update-create-rule.md
- Scan codebase ตาม
workflows/update-scan-codebase.md
- Test rules ตาม
workflows/update-test-rule.md
- Configure project ตาม
workflows/update-configuration.md
- อ่าน
learn/key-concepts/ สำหรับ pattern matching, metavariables, rule types
- อ่าน
learn/principles/ สำหรับ atomic-first, compose rules, test-driven
- อ่าน
references/ สำหรับ API docs, CLI, และ configuration
Rules
Pattern Syntax
- ใช้ metavariables
$VAR สำหรับ match any single AST node (เช่น $PROP, $MATCH)
- ใช้ multi-metavariables
$$$VARS สำหรับ match zero or more AST nodes
- เขียน patterns เหมือนเขียน code ตามปกติ (pattern ต้องเป็น valid code)
- ใช้ single quotes
' สำหรับ patterns เพื่อป้องกัน shell expansion
- Pattern ต้อง parse ได้ด้วย tree-sitter (ใช้ playground เพื่อ debug)
Rule Configuration
- ใช้ YAML configuration สำหรับ lint rules
- Atomic rules:
pattern, kind, regex, nthChild, range
- Relational rules:
inside, has, follows, precedes
- Composite rules:
all, any, not, matches
- Test rules ก่อนใช้งานจริงด้วย
ast-grep test
Command Line Usage
- ใช้
--lang หรือ -l สำหรับระบุภาษา (ถ้าจำเป็น)
- ใช้
--rule สำหรับ run rules จาก YAML files
- ใช้
--inline-rules สำหรับ run rules โดยตรงจาก command line
- ใช้
--interactive หรือ -i สำหรับ interactive rewriting
- ใช้
--json สำหรับ JSON output
- บน Linux ใช้
ast-grep แทน sg (conflict กับ setgroups command)
Best Practices
- เริ่มจาก atomic patterns ก่อน compose rules
- Test rules ด้วย snapshot tests
- ใช้ playground สำหรับ debug patterns
- ใช้ code blocks สำหรับ examples และ patterns
- ทำตาม
learn/principles/atomic-first.md สำหรับ rule design
Expected Outcome
- Code search ที่ accurate ด้วย AST matching
- Custom lint rules ที่ powerful และ reusable
- Code transformation ที่ automated และ safe
- Refactoring ที่ efficient และ scalable
- Integration กับ editor ผ่าน LSP
1---2name: ast-grep3description: AST-based structural search, lint และ rewriting tool สำหรับ polyglot code transformation4---5## Goal67ใช้งาน ast-grep สำหรับ structural code search, linting และ rewriting ด้วย AST patterns บน multiple languages8910## Scope1112ใช้สำหรับค้นหา code patterns ด้วย AST-based matching, สร้าง custom lint rules, ทำ code transformation และ refactoring อัตโนมัติ, scan codebase สำหรับ code smells และ anti-patterns, และ integrate กับ editor ผ่าน LSP131415## Execute1617- ติดตั้ง ast-grep ตาม `workflows/update-installation.md`18- เรียนรู้ pattern syntax จาก `learn/guide/key-concept.md`19- ค้นหา patterns ด้วย `ast-grep -p '$PATTERN'` หรือ `sg -p '$PATTERN'`20- แก้ไข code ด้วย `ast-grep -p '$PATTERN' --rewrite '$REWRITE'`21- สร้าง lint rules ตาม `workflows/update-create-rule.md`22- Scan codebase ตาม `workflows/update-scan-codebase.md`23- Test rules ตาม `workflows/update-test-rule.md`24- Configure project ตาม `workflows/update-configuration.md`25- อ่าน `learn/key-concepts/` สำหรับ pattern matching, metavariables, rule types26- อ่าน `learn/principles/` สำหรับ atomic-first, compose rules, test-driven27- อ่าน `references/` สำหรับ API docs, CLI, และ configuration282930## Rules3132### Pattern Syntax3334- ใช้ metavariables `$VAR` สำหรับ match any single AST node (เช่น `$PROP`, `$MATCH`)35- ใช้ multi-metavariables `$$$VARS` สำหรับ match zero or more AST nodes36- เขียน patterns เหมือนเขียน code ตามปกติ (pattern ต้องเป็น valid code)37- ใช้ single quotes `'` สำหรับ patterns เพื่อป้องกัน shell expansion38- Pattern ต้อง parse ได้ด้วย tree-sitter (ใช้ playground เพื่อ debug)3940### Rule Configuration4142- ใช้ YAML configuration สำหรับ lint rules43- Atomic rules: `pattern`, `kind`, `regex`, `nthChild`, `range`44- Relational rules: `inside`, `has`, `follows`, `precedes`45- Composite rules: `all`, `any`, `not`, `matches`46- Test rules ก่อนใช้งานจริงด้วย `ast-grep test`4748### Command Line Usage4950- ใช้ `--lang` หรือ `-l` สำหรับระบุภาษา (ถ้าจำเป็น)51- ใช้ `--rule` สำหรับ run rules จาก YAML files52- ใช้ `--inline-rules` สำหรับ run rules โดยตรงจาก command line53- ใช้ `--interactive` หรือ `-i` สำหรับ interactive rewriting54- ใช้ `--json` สำหรับ JSON output55- บน Linux ใช้ `ast-grep` แทน `sg` (conflict กับ setgroups command)5657### Best Practices5859- เริ่มจาก atomic patterns ก่อน compose rules60- Test rules ด้วย snapshot tests61- ใช้ playground สำหรับ debug patterns62- ใช้ code blocks สำหรับ examples และ patterns63- ทำตาม `learn/principles/atomic-first.md` สำหรับ rule design646566## Expected Outcome6768- Code search ที่ accurate ด้วย AST matching69- Custom lint rules ที่ powerful และ reusable70- Code transformation ที่ automated และ safe71- Refactoring ที่ efficient และ scalable72- Integration กับ editor ผ่าน LSP