Generate documentation from code with structured analysis and quality gates
Generate documentation by reading actual code — not by guessing what it does. Every claim traces to a file and line number. Every example is extracted from real usage, not invented.
/write-docs
/write-docs src/lib/auth.ts
/write-docs --scope api
/write-docs # Document the most-changed files this week
/write-docs src/components/Button.tsx # Document a specific file
/write-docs --scope api # Document all files in a directory
/write-docs --update # Update existing docs with code changes
If no target is specified, find the files that need docs most:
git log --since="7 days ago" --name-only --pretty=format:"" to find recently changed filessrc/auth.ts -> docs/auth.md)For each target file:
grep -r "import.*functionName" src/For each documented item, produce:
Before writing the doc file, verify:
source: file:line referenceIf any check fails, fix it before writing.
Write the documentation to the appropriate location:
docs/api/<module>.mddocs/components/<component>.mddocs/guides/<topic>.mdShow the user a summary of what was documented and ask for review.
Most documentation fails because it describes what the code DOES instead of what the developer NEEDS TO KNOW. The function signature already tells you the parameters. The documentation should tell you:
A doc that says "takes a userId string and returns a User object" is useless — the types already say that. A doc that says "call this after authentication but before session hydration — calling it before auth throws UNAUTHORIZED, calling it after hydration wastes a DB round-trip" is invaluable.
Document the WHEN, WHY, and WHAT-BREAKS. The WHAT is already in the code.
# Module Name
> One-sentence description derived from code behavior.
## `functionName(param: Type): ReturnType`
What the developer needs to know — when to use it, why it exists, what breaks.
**Parameters:**
| Name | Type | Description |
|------|------|-------------|
| param | Type | What it controls and valid ranges |
**Returns:** What and when it varies.
**Example:** (from `src/routes/auth.ts:42`)
\`\`\`typescript
// Actual usage from the codebase
const user = await getUser(session.userId);
\`\`\`
**Edge cases:**
- Throws `AuthError` if session expired (see `src/lib/errors.ts:15`)
- Returns `null` if user was soft-deleted (check `user.deletedAt`)
---
*Source: `src/lib/auth.ts` | Last updated: auto-generated*