-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Add Next.js + TypeScript rules (annotated, with enforcement hooks) #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
iPythoning
wants to merge
3
commits into
PatrickJS:main
Choose a base branch
from
iPythoning:add-nextjs-typescript-annotated-enforced
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
rules/nextjs-typescript-annotated-enforced-cursorrules-prompt-file.mdc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --- | ||
| description: "Next.js (App Router) + React 19 + TypeScript rules where every rule states WHY it exists, paired with Claude Code enforcement hooks that format/lint/type-check automatically." | ||
| globs: **/*.{ts,tsx} | ||
| alwaysApply: false | ||
| --- | ||
| // Next.js + TypeScript .cursorrules — annotated + enforced | ||
| // Each rule carries the reason it exists (models follow rules they understand). | ||
| // Pairs with a synced Claude Code CLAUDE.md + .claude/settings.json hooks that run | ||
| // Prettier/ESLint on edit and `tsc --noEmit` on session stop — so the rules RUN, not just suggest. | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| // Source / full multi-stack pack (Node/Express, FastAPI, Go): https://github.com/iPythoning/claude-cursor-config-nextjs | ||
|
|
||
| You are working in a Next.js (App Router) + React 19 + TypeScript (strict) project. npm is the package manager. | ||
|
|
||
| ## Scope discipline | ||
| - Change only what the task requires. Do NOT refactor, rename, or reformat unrelated code. Why: unrequested edits bury the real diff and break unrelated work. | ||
| - Do NOT add a new dependency, pattern, or abstraction without asking. Prefer what the codebase already uses. Why: a second state library or HTTP client is a tax forever. | ||
| - No speculative generality — build for the current requirement only (YAGNI). | ||
|
|
||
| ## TypeScript | ||
| - strict mode. NEVER use `any` — use `unknown` and narrow. Why: `any` silently disables the type system exactly where bugs hide; `unknown` forces a deliberate check. | ||
| - Prefer `type` for unions/objects; `interface` only for declaration merging. | ||
| - PascalCase type names, no `I` prefix. | ||
| - Derive types from one source of truth (z.infer, ReturnType, `as const`) — never maintain parallel shapes by hand. | ||
|
|
||
| ## React / Next.js | ||
| - Server Components by default. Add "use client" ONLY for state, effects, or browser APIs. Why: shipping client JS you don't need is the most common Next.js performance regression. | ||
| - Fetch data in Server Components or route handlers, NOT in useEffect. Why: useEffect fetching causes request waterfalls and loading-flash; server fetching is parallel and cache-aware. | ||
| - PascalCase components, useCamelCase hooks, one component per file. | ||
| - Co-locate by feature, not by file type. Why: feature folders keep related code together and make deletion safe. | ||
| - Keep server-only secrets out of any module a Client Component imports (use `server-only`). | ||
|
|
||
| ## State | ||
| - Server data: RSC or TanStack Query. Shared client state: a small store, only when prop-passing hurts. URL state: search params. Forms: React Hook Form + schema validation. | ||
| - NEVER copy server data into a client store. Why: two sources of truth drift; derive instead of duplicate. | ||
|
|
||
| ## Error handling | ||
| - Handle errors explicitly at every boundary. NEVER swallow them with empty catch blocks. Why: a silent `catch {}` turns a clear failure into a mystery bug three screens away. | ||
| - Validate all external input (form data, params, API responses) with a schema at the edge. Why: trusting unvalidated external data is the root of most runtime crashes and injection bugs. | ||
| - User surfaces show friendly messages; the server logs full context. | ||
|
|
||
| ## Code style | ||
| - Immutable by default — return new objects, don't mutate inputs. Why: hidden mutation is the hardest class of bug to trace. | ||
| - Early returns over deep nesting (max ~3 levels). Named constants over magic numbers. | ||
| - Functions < ~50 lines, files < ~400 (hard cap 800). Why: small units are reviewable, testable, and reusable. | ||
| - No console.log, commented-out code, or context-free TODOs in committed code. | ||
|
|
||
| ## Security | ||
| - No hardcoded secrets (env vars; validate at startup). No unsanitized HTML / raw dangerouslySetInnerHTML. | ||
| - Parameterize every DB query. CSRF + rate limiting on state-changing routes. | ||
|
|
||
| ## Testing & done | ||
| - New logic ships with AAA tests covering edge cases. Descriptive test names. | ||
| - Before declaring done: `npm run build`, `npm run lint`, and tests must pass. No `any`, no swallowed errors, no hardcoded secrets, no stray console.log. Diff contains only the required change. Why: "it compiles in my head" is not verification. | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.