|
| 1 | +/** |
| 2 | + * ObjectUI ESLint rule: no-try-catch-around-hook |
| 3 | + * |
| 4 | + * Bans wrapping a React hook call in `try`/`catch`. |
| 5 | + * |
| 6 | + * A `catch` that swallows a throw from a hook desyncs hook order on the next |
| 7 | + * render: the hooks that already ran before the throw were registered, the |
| 8 | + * ones after it were not, and React matches hooks positionally. The result is |
| 9 | + * the classic "Rendered fewer hooks than expected" crash, or silently reading |
| 10 | + * another hook's state. |
| 11 | + * |
| 12 | + * This exists because the pattern kept getting copy-pasted. objectui#2595 / |
| 13 | + * #2596 fixed it in `@object-ui/i18n`'s `createSafeTranslation`, but nine |
| 14 | + * plugin-local re-implementations kept their own `try { useObjectTranslation() } |
| 15 | + * catch {}` (objectui#2879) — every one of them written to make a hook |
| 16 | + * "provider-safe", which the hook already is. |
| 17 | + * |
| 18 | + * The correct pattern for "no provider mounted" is a VALUE probe after the |
| 19 | + * hook has run (e.g. `t(testKey) === testKey` → use English defaults), not a |
| 20 | + * `catch`. See `packages/i18n/src/useSafeTranslation.ts`. |
| 21 | + * |
| 22 | + * Scope, deliberately narrow to stay false-positive free: |
| 23 | + * - Only `use*` names, per React's own convention. |
| 24 | + * - Bare identifiers and `React.useX` only. `vi.useRealTimers()` and |
| 25 | + * `jest.useFakeTimers()` are test utilities, not hooks. |
| 26 | + * - The try-depth RESETS inside a nested function. A hook called in a |
| 27 | + * callback that merely happens to be written inside a try — |
| 28 | + * `renderHook(() => useCondition(...))` — runs during React's render of |
| 29 | + * that component, not in the try's synchronous flow, and is fine. |
| 30 | + * |
| 31 | + * @type {import('eslint').Rule.RuleModule} |
| 32 | + */ |
| 33 | + |
| 34 | +/** React's own convention: a hook is `use` followed by a non-lowercase char. */ |
| 35 | +function isHookName(name) { |
| 36 | + return typeof name === 'string' && /^use[A-Z0-9]/.test(name); |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Resolve the called name, but only for shapes that can be a React hook. |
| 41 | + * `vi.useRealTimers()` / `jest.useFakeTimers()` are namespaced test helpers |
| 42 | + * that match the `use*` spelling by coincidence, so member calls are accepted |
| 43 | + * only on `React`. |
| 44 | + */ |
| 45 | +function hookCalleeName(callee) { |
| 46 | + if (callee.type === 'Identifier') return callee.name; |
| 47 | + if ( |
| 48 | + callee.type === 'MemberExpression' && |
| 49 | + callee.object.type === 'Identifier' && |
| 50 | + callee.object.name === 'React' && |
| 51 | + callee.property.type === 'Identifier' |
| 52 | + ) { |
| 53 | + return callee.property.name; |
| 54 | + } |
| 55 | + return undefined; |
| 56 | +} |
| 57 | + |
| 58 | +export default { |
| 59 | + meta: { |
| 60 | + type: 'problem', |
| 61 | + docs: { |
| 62 | + description: |
| 63 | + 'Disallow calling a React hook inside try/catch — a caught throw desyncs hook order on the next render (objectui#2595/#2596/#2879).', |
| 64 | + recommended: true, |
| 65 | + }, |
| 66 | + schema: [], |
| 67 | + messages: { |
| 68 | + banned: |
| 69 | + "Do not call the hook '{{name}}' inside try/catch — a caught throw desyncs hook order on the next render (objectui#2879). Call the hook unconditionally and probe its RETURN VALUE for the degraded case; see createSafeTranslation in packages/i18n/src/useSafeTranslation.ts.", |
| 70 | + }, |
| 71 | + }, |
| 72 | + create(context) { |
| 73 | + // Stack of try-depths, one frame per enclosing function. Entering a |
| 74 | + // function pushes a fresh 0 so a callback written inside a try starts |
| 75 | + // clean; leaving pops back to the outer count. |
| 76 | + const frames = [0]; |
| 77 | + const bump = (n) => { frames[frames.length - 1] += n; }; |
| 78 | + |
| 79 | + const enterFn = () => { frames.push(0); }; |
| 80 | + const exitFn = () => { frames.pop(); }; |
| 81 | + |
| 82 | + return { |
| 83 | + ':function': enterFn, |
| 84 | + ':function:exit': exitFn, |
| 85 | + |
| 86 | + 'TryStatement > BlockStatement': () => bump(1), |
| 87 | + 'TryStatement > BlockStatement:exit': () => bump(-1), |
| 88 | + 'CatchClause > BlockStatement': () => bump(1), |
| 89 | + 'CatchClause > BlockStatement:exit': () => bump(-1), |
| 90 | + |
| 91 | + CallExpression(node) { |
| 92 | + if (frames[frames.length - 1] === 0) return; |
| 93 | + const name = hookCalleeName(node.callee); |
| 94 | + if (!isHookName(name)) return; |
| 95 | + context.report({ node, messageId: 'banned', data: { name } }); |
| 96 | + }, |
| 97 | + }; |
| 98 | + }, |
| 99 | +}; |
0 commit comments