@@ -18,6 +18,23 @@ const isEffectHookCall = (node: ESTree.CallExpression): boolean => { |
18 | 18 | return false; |
19 | 19 | }; |
20 | 20 |
|
| 21 | +// Agents read this message and act on it, so it walks the alternatives before it shows the way out. |
| 22 | +const MESSAGE = |
| 23 | + "Stop and reconsider before keeping this effect. " + |
| 24 | + "Is it computable from props or state? Compute it during render; the compiler memoizes it. " + |
| 25 | + "Does it respond to a user action? Do it in that handler. " + |
| 26 | + "Does it reset state when a prop changes? Key the component on that prop. " + |
| 27 | + "Does it tell a parent or store that a value changed? Do it where the change happens. " + |
| 28 | + "Does it load app data? Read it from an atom. " + |
| 29 | + "Does it read a value from outside React? Use an atom or useSyncExternalStore. " + |
| 30 | + "An effect that sets state another effect reads is one handler or one derivation. " + |
| 31 | + "An effect is right only when it keeps something outside React in step with this component while mounted: " + |
| 32 | + "a DOM node, listener, timer, native module, connection, router, imperative toast, " + |
| 33 | + "or a registry entry or resource held until unmount. " + |
| 34 | + "Moving the same logic into a ref or store to dodge this rule is not a fix. " + |
| 35 | + "If the effect does that, name what it keeps in step on the line above: " + |
| 36 | + "`// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- <what it syncs with and why>`."; |
| 37 | + |
21 | 38 | // Every kept effect carries its reason in the suppression above it; t3code/require-suppression-reason |
22 | 39 | // rejects a suppression that does not say why. |
23 | 40 | export default defineRule({ |
@@ -37,10 +54,7 @@ export default defineRule({ |
37 | 54 | if (!isEffectHookCall(node)) return; |
38 | 55 | context.report({ |
39 | 56 | node, |
40 | | - message: |
41 | | - "First ask whether this is synchronization that belongs in render or in the handler that causes it. " + |
42 | | - "If an effect is the honest model, say why on the line above it: " + |
43 | | - "`// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- <why>`.", |
| 57 | + message: MESSAGE, |
44 | 58 | }); |
45 | 59 | }, |
46 | 60 | }; |
|