pi-lens-write-tree-sitter-rule
Skill0 starsChanged 14 days agoISCView on GitHub
Use when writing a new pi-lens tree-sitter query rule YAML file — covers schema, S-expression syntax, capture names, predicates, and gotchas
1 other project uses a version of this.
Adopted by
Most recently taken up by
- apmantza/pi-lensword for word, first seen Oct 8, 2026
Other versions1 project uses a version of this.
Word for word · 1
- apmantza/pi-lens★ 521100% same
Edited · 0
None found.
Wrote their own version · 0
None found.
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository18 other files it gives its agents.
- AGENTS.mdAGENTS.md
- apex-performance-auditorSkill
- checkSkill
- context7-mcpSkill
- design-craftSkill
- framework-engine-stacksSkill
- guardrailsSkill
- hypermedia-engine-stacksSkill
- learning-curatorSkill
- okf-docsSkill
- pi-lens-ast-grepSkill
- pi-lens-lsp-navigationSkill
- pi-lens-write-ast-grep-ruleSkill
- rust-systemsSkill
- scalabilitySkill
- select-modelSkill
- skill-authoring-visionSkill
- systems-auditorSkill
The file
Files
Writing a pi-lens tree-sitter Rule
Drop path: rules/tree-sitter-queries/<language>/<id>.yml
Language dir is lowercase: typescript javascript tsx python go rust java csharp kotlin php ruby cpp c css
Project rules merge with built-ins (both run). To disable a language's built-ins: rename dir to <lang>-disabled/.
Minimal template
id: no-eval
severity: error
inline_tier: blocking # blocking | warning | review
language: typescript # lowercase, inferred from dir if omitted
message: "eval() is dangerous — use a safer alternative"
query: |
(call_expression
function: (identifier) @FN
(#eq? @FN "eval"))
metavars: [FN]
has_fix: falseS-expression query syntax
; Node with field
(call_expression
function: (identifier) @NAME
arguments: (arguments) @ARGS)
; Alternatives — ALL branches must use the SAME capture names
[
(function_declaration name: (identifier) @FN)
(arrow_function) @FN
]
; Predicates (inline)
(#eq? @NAME "fetch") ; exact match
(#match? @NAME "^on[A-Z]") ; regex matchPredicates via YAML (faster — runs in WASM)
predicates:
- type: eq # eq | match | any-of
var: "@FN"
value: "dangerousMethod"
- type: match
var: "@NAME"
value: "^(get|set)[A-Z]"inline_tier
| Value | Effect |
blocking | Blocks agent turn — 🔴 injected immediately |
warning | Advisory only |
review | Low-priority suggestion |
Gotchas
❌ Mixed capture names in [...] alternatives — zero matches, no error
[ (fn_decl name: (id) @NAME) (method body: (block) @BODY) ]
→ split into two separate [...] blocks
❌ Field value as alternative
right: [(identifier) (call_expression)] ← INVALID
→ use separate query blocks instead
❌ @lowercase captures — convention is @UPPER_SNAKE
@fn → @FN
✅ Find node kinds: use --debug-query CST in ast-grep playground
or https://tree-sitter.github.io/tree-sitter/playground
✅ Schema + autocomplete: rules/tree-sitter-queries/rule-schema.json
✅ Docs: docs/custom-rules.mdHard-won gotchas (verified, not theoretical)
❌ `_` matches NAMED nodes only — never anonymous tokens (operators, punctuation)
To match/capture an operator, use the literal token string:
(binary_expression operator: _ @OP) ← captures NOTHING for `==`
(binary_expression ["==" "!="] @OP) ← correct (anonymous tokens)
Anonymous-token alternation `["==" "!="]` works as a CHILD; as a FIELD value
it may not compile under the WASM grammar — prefer the child form.
❌ The bundled WASM grammar ≠ the playground / @ast-grep/napi grammar (version skew)
Real example: WASM tree-sitter-typescript wraps the for-loop condition in an
`expression_statement` and exposes NO `condition:` field:
(for_statement condition: (binary_expression ...)) ← 0 matches (no such field)
(for_statement (expression_statement (binary_expression ...))) ← correct
Always verify field names / nesting against the WASM grammar, not the playground.
❌ A query that fails to compile returns 0 matches SILENTLY (no error thrown).
So "0 matches" can mean "wrong query", not "nothing to find".
→ Probe incrementally: start `(target_kind) @X`, confirm >0, then add one
layer (field, child, predicate) at a time until it narrows correctly.
✅ Verify with the production path, not just the playground:
client.runQueryOnFile(queryDef, filePath, "typescript")
(TreeSitterClient compiles via the same WASM the runner uses.)
✅ For rule-bug fixes, also add a real-runner regression test through
treeSitterRunner.run() via makeRealRunnerCtx (tests/support/real-runner-ctx.ts) —
runQueryOnFile bypasses dispatch (skip_test_files, tiers, delta, rule cache).
Template: tests/clients/dispatch/runners/tree-sitter-skip-test-files.test.ts.
(These test paths exist only in a pi-lens source checkout — the installed
npm package ships no tests/ directory.)
✅ JS files also run typescript/ rules (shared grammar) — one rule in
rules/tree-sitter-queries/typescript/ covers BOTH .ts and .js. No -js copy needed.
✅ Match a header-field child precisely without a field name by relying on direct
children: for_statement's children are lexical_declaration (init),
expression_statement (condition), update_expression (increment), statement_block
(body) — so a binary_expression nested in the condition's expression_statement is
unambiguous, and the body's nodes are inside statement_block (not matched).Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

