|
| 1 | +--- |
| 2 | +description: Code quality conventions the linters do not check |
| 3 | +paths: |
| 4 | + - "**/*.{ts,tsx}" |
| 5 | +globs: "**/*.ts,**/*.tsx" |
| 6 | +alwaysApply: false |
| 7 | +--- |
| 8 | +# Code Quality |
| 9 | + |
| 10 | +This rule holds the code conventions that are true in any repository and that the linters do not check. |
| 11 | +What the active child adds to it or does differently is in the child's |
| 12 | +`.agents/rules/code-quality-local.md`, when it has one, and the child's rule wins where the two disagree. |
| 13 | +The child's lint commands and quality gates are there too. |
| 14 | + |
| 15 | +## Code Style and Structure |
| 16 | + |
| 17 | +- Structure components logically: exports, subcomponents, helpers, types |
| 18 | + |
| 19 | +### Naming Conventions |
| 20 | + |
| 21 | +- Prefer descriptive names with auxiliary verbs (isLoading, hasError) |
| 22 | +- Prefix event handlers with "handle" (handleClick, handleSubmit) |
| 23 | + |
| 24 | +Names should be specific and self-documenting. Vague names hide intent and make the codebase harder to navigate. |
| 25 | + |
| 26 | + |
| 27 | +```ts |
| 28 | +// BAD |
| 29 | +const useWidgets = () => { ... }; |
| 30 | + |
| 31 | +// GOOD |
| 32 | +const useAddress3rdPartyWidgets = () => { ... }; |
| 33 | +``` |
| 34 | + |
| 35 | +This applies to hooks, components, functions, and variables alike. |
| 36 | + |
| 37 | +## Specific rules |
| 38 | + |
| 39 | +### Magic numbers |
| 40 | + |
| 41 | +Name a literal only when a reader would still ask why this value. The name is `UPPER_SNAKE_CASE`, sits above the component or function, and carries a domain rule the surrounding identifiers do not: a limit, threshold, interval, or similar choice. |
| 42 | + |
| 43 | +```ts |
| 44 | +// BAD: 4 is a product limit with no name |
| 45 | +const visibleItems = items.slice(0, 4); |
| 46 | + |
| 47 | +// GOOD |
| 48 | +const MAX_VISIBLE_ITEMS = 4; |
| 49 | +const visibleItems = items.slice(0, MAX_VISIBLE_ITEMS); |
| 50 | +``` |
| 51 | + |
| 52 | +Leave the literal when it is the meaning. A constant that restates the value, a nearby identifier, or a universal convention is noise. `1` is the first page, `0` is the first index, a URL is a URL. |
| 53 | + |
| 54 | +```ts |
| 55 | +// BAD: the name restates href and page |
| 56 | +const URL = 'https://example.com'; |
| 57 | +const link = <a href={ URL }/>; |
| 58 | + |
| 59 | +const FIRST_PAGE = 1; |
| 60 | +if (page !== FIRST_PAGE) return null; |
| 61 | + |
| 62 | +// GOOD |
| 63 | +const link = <a href="https://example.com"/>; |
| 64 | + |
| 65 | +if (page !== 1) return null; |
| 66 | +``` |
| 67 | + |
| 68 | +Apply the same test to strings used as discriminators, keys, or thresholds, and to values in tests. CSS-style props (margin, padding, font weight, color) are already named by the prop. |
| 69 | + |
| 70 | +If the same unexplained value must stay in sync at more than one call site, one constant is the share point. |
| 71 | + |
| 72 | +### Static empty defaults |
| 73 | + |
| 74 | +In React code, never define empty arrays or objects inline as default values — a new reference is created on every render, causing unnecessary re-renders or stale hook dependencies. |
| 75 | + |
| 76 | +```ts |
| 77 | +// BAD |
| 78 | +const items = data ?? []; |
| 79 | + |
| 80 | +// GOOD |
| 81 | +const EMPTY_ITEMS: Array<Item> = []; |
| 82 | +const items = data ?? EMPTY_ITEMS; |
| 83 | +``` |
| 84 | + |
| 85 | +Define the constant outside the component or hook. |
| 86 | + |
| 87 | +### useMemo for derived arrays |
| 88 | + |
| 89 | +In React code, wrap `.filter()`, `.map()`, or `.reduce()` results in `useMemo` when the result is passed as a prop or used as a hook dependency. Without memoisation, a new array reference is produced on every render. |
| 90 | + |
| 91 | +```ts |
| 92 | +// BAD |
| 93 | +const filtered = items.filter(isActive); |
| 94 | +return <List items={filtered} />; |
| 95 | + |
| 96 | +// GOOD |
| 97 | +const filtered = useMemo(() => items.filter(isActive), [items]); |
| 98 | +return <List items={filtered} />; |
| 99 | +``` |
| 100 | + |
| 101 | +### eslint-disable comments |
| 102 | + |
| 103 | +Every `eslint-disable` comment must include an explanation. Without one, the reason for the suppression is lost and the comment becomes a maintenance hazard. |
| 104 | + |
| 105 | +```ts |
| 106 | +// BAD |
| 107 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 108 | + |
| 109 | +// GOOD |
| 110 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any -- API response shape is dynamic and validated at runtime |
| 111 | +``` |
| 112 | + |
| 113 | +### File path string interpolation |
| 114 | + |
| 115 | +Prefer explicit file names over template literals in asset paths. Explicit names are easier to locate with search tools and eliminate accidental runtime errors. |
| 116 | + |
| 117 | +```ts |
| 118 | +// BAD |
| 119 | +const src = `streak_${days}.png`; |
| 120 | + |
| 121 | +// GOOD — map variants explicitly |
| 122 | +const STREAK_IMAGES: Record<number, string> = { |
| 123 | + 30: 'streak_30.png', |
| 124 | + 60: 'streak_60.png', |
| 125 | +}; |
| 126 | +``` |
| 127 | + |
| 128 | +### Prefer es-toolkit utilities |
| 129 | + |
| 130 | +`es-toolkit` is the utility library of every frontend repository. Before writing custom utility logic (clamping, deep cloning, grouping, etc.), check whether `es-toolkit` already provides it. Prefer the library function over a manual implementation. Documentation: https://es-toolkit.dev/llms-full.txt |
| 131 | + |
| 132 | +Use no other utility library, `lodash` included. If the repository does not depend on `es-toolkit` yet, raise adding it with the developer before you write the utility by hand. |
| 133 | + |
| 134 | +### Comments |
| 135 | + |
| 136 | +**Do not write comments. Never. Period.** |
| 137 | +Prefer a name or a structure change that makes the comment unnecessary. |
| 138 | + |
| 139 | +The ban has exactly three exceptions: the why-comment, the header comment and the source pointer. |
| 140 | + |
| 141 | +Never write *diff-narration* (what this change replaces). That belongs in the commit and the PR. Test: if the comment would confuse someone who never saw the previous file, delete it. |
| 142 | + |
| 143 | +Do not write JSDoc (`/** */`). The one exception is a single-line `/** @type … */` in a JavaScript file, which TypeScript reads as the declared type. A header comment is an ordinary comment like any other. Do not rewrite comments you did not have to touch. Delete commented-out code; git keeps it. TODOs only with an issue or a concrete follow-up. |
| 144 | + |
| 145 | +#### Why-comments |
| 146 | + |
| 147 | +A why-comment explains *why* the obvious code is wrong — a quirk, workaround, or invariant git cannot show — optionally with a link to the issue. Required mechanical comments (`eslint-disable`, unavoidable double-cast) still explain *why*, in their own sections. |
| 148 | + |
| 149 | +```ts |
| 150 | +// BAD — narrates the change and restates the next line |
| 151 | +// Replaces the old useEffect; memoize the filtered list |
| 152 | +const active = useMemo(() => items.filter(isActive), [ items ]); |
| 153 | + |
| 154 | +// GOOD — why the obvious approach fails |
| 155 | +// Firefox: drag outside the window swallows mouse events until re-entry; see onMouseLeave(). |
| 156 | +const onMouseMove = useCallback(() => {}, [ ]); |
| 157 | +``` |
| 158 | + |
| 159 | +#### Header comments |
| 160 | + |
| 161 | +A header comment tells a **caller** what the name and the signature cannot. |
| 162 | + |
| 163 | +An export whose name and signature tell the caller everything gets no header comment. This is the usual case. |
| 164 | + |
| 165 | +**The locality test.** A header comment states only what can become false through an edit to the same file. A statement about the file's own contract breaks in the diff that edits the file, so the author sees it. A statement about another file breaks through an edit elsewhere, and nobody sees it. Examples of the test: |
| 166 | + |
| 167 | +| Allowed | Not allowed | |
| 168 | +| --- | --- | |
| 169 | +| When the module applies, and when it does not | What the name, the types or the parameters already say | |
| 170 | +| What the caller must do around the call: the order of calls, what a required argument means, a requirement on the calling file such as its name or its test project | A walk through the implementation | |
| 171 | +| What the call changes outside its return value, and what undoes it | Who calls it today, or how it used to work | |
| 172 | +| What the module does that the obvious alternative cannot | How another module or the subsystem works | |
| 173 | +| A bare pointer to that alternative, or to the area's `CONTEXT.md` | What the alternative does, or what the `CONTEXT.md` says | |
| 174 | +| A call sample, under the test below | A call sample that fails the test below | |
| 175 | + |
| 176 | +**Which modules.** A header comment is allowed only on a module that is imported from outside its own directory: a shared util, a test helper, a setup file, a script. A component or a page gets none. |
| 177 | + |
| 178 | +**Placement.** Write at most one header comment per exported declaration, above it. Write at most one more at the top of the file, for a contract of the module as a whole. |
| 179 | + |
| 180 | +**Pointers.** A header comment may name an alternative module or the area's `CONTEXT.md` as a bare pointer. It does not describe what that file does. |
| 181 | + |
| 182 | +**The call-sample test.** A sample stays only when a reader holding the prose and the signature would still get the call wrong: the setup spans several statements whose order matters, or it goes through a mock factory or a dynamic import. A single call with plain arguments needs none. A sample that repeats a sentence above it is deleted, or the sentence is. |
| 183 | + |
| 184 | +#### Source pointers |
| 185 | + |
| 186 | +A source pointer names where code, types, data or a format came from. It is one line, labeled with what the source is. It names the source; it does not retell it. |
| 187 | + |
| 188 | +```ts |
| 189 | +// BAD — an unlabeled link, and a retelling of the source |
| 190 | +// https://eips.ethereum.org/EIPS/eip-6963 |
| 191 | +// A wallet announces itself with an `eip6963:announceProvider` event that carries its info and provider. |
| 192 | + |
| 193 | +// GOOD |
| 194 | +// The EIP-6963 spec - https://eips.ethereum.org/EIPS/eip-6963 |
| 195 | +// The types source - node_modules/@wagmi/core/src/connectors/injected.ts |
| 196 | +``` |
0 commit comments