react-doctor
react-doctor copied to clipboard
feat: port the React Compiler's HIR + validators into react-doctor
Summary
Ports the React Compiler's High-level IR (HIR) infrastructure into react-doctor as a foundation for more precise rule analysis, plus two initial validators ported from the compiler:
hir-no-set-state-in-effect: flagssetStatecalls insideuseEffectbodies, including when the setter is propagated throughconst x = setXaliasing or wrapped in auseEffectEvent/ innerFunctionExpression.hir-no-derived-computations-in-effects: flags effects whose captures are entirely deps + setters and whose body has at least one intermediateconst(the unique path the AST walker can't see through).
What got ported
- HIR data model (
plugin/hir/types.ts):HIRFunction,BasicBlock,Instruction,Place,Identifier,ReactType(≈20 React-aware tags including the round-5StateTuple), withoriginNodeonPlacefor diagnostic precision. - Lowering (
plugin/hir/lower.ts): ESTree → HIR, parent-chained scope environments,FunctionExpression.capturedPlaces, full statement coverage (round-5 addedfor/while/do-while/for-of/for-in/switch/try/throw/labeled),SpreadElementunwrapping in call args. - Type inference (
plugin/hir/infer-types.ts): hook recognition viaLoadGlobalname → ReactType, propagation throughLoadLocal/StoreLocal/PropertyLoad. Round-5 introducesStateTupleso onlyuseStatereturns get the indexedStateValue/StateSettertagging —useMemo/useContextreturns no longer leak into that branch. - Validators (
plugin/hir/validators/): two ports, with shape-defer logic that lets the existing AST walker handle the simple cases. - Runner (
plugin/hir/runner.ts): per-componentWeakMap<EsTreeNode, HIRFunction>cache +originNode-based diagnostic anchoring so reports point at the offendingsetX(...)call site.
Round 5 — deep code review fixes
Five issues found on a fresh review of the round-4 code:
| # | Issue | Fix |
|---|---|---|
| 1 | inferTypes conflated useState/useMemo/useContext returns as Object; the indexed-PropertyLoad branch tagged [a, b] = useMemo(...) as [StateValue, StateSetter], leading to false-positive setState detection |
New StateTuple ReactType gates the indexed-PropertyLoad branch to useState only; regression test for useMemo tuple destructure |
| 2 | hir-no-derived-computations-in-effects duplicated noDerivedStateEffect on the article §1 fullName example |
Validator now defers when the inner HIR has no StoreLocal (single-setter-call shape) — keeps the multi-statement-with-locals path that uniquely needs HIR. hir-no-set-state-in-effect left at warn with a known-overlap note in oxlint-config.ts, since reliably distinguishing direct vs aliased setters at v1 isn't workable |
| 3 | lowerExpression silently dropped SpreadElement arguments |
New lowerCallArguments unwraps the spread's argument so operand identity is preserved through setState propagation |
| 4 | lowerStatement ignored for/while/do-while/for-of/for-in/switch/try/throw/labeled — hooks inside any of these blocks were invisible |
Added recursive descent for all of them |
| 5 | hir-unit.test.ts article-§1 case used the single-setter-call shape that fix #2 now defers on |
Updated to the multi-statement-with-locals form to exercise the validator's unique path |
Validation
- 490 / 490 tests pass (4 new regressions added across the 5 fixes)
pnpm lintcleanpnpm typecheckcleanpnpm formatclean
Known v1 limitations (documented in code)
letreassignments are not modeled as non-reactive (HIR is non-SSA).- JSX expression contents aren't lowered as identifier reads (renders are summarized as a placeholder).
import { useState as useS }is not recognized — name-based heuristic onLoadGlobal.hir-no-set-state-in-effectoverlapsnoDerivedStateEffecton the simplest shape; users can disable either viareact-doctor.config.json.
Architecture summary
ESTree (oxlint visitor)
↓ lowerFunction
HIR (basic blocks + instructions + Place.originNode)
↓ inferTypes (per-identifier ReactType tags)
Typed HIR
↓ validateNo* (per-rule analyses)
Findings (with originNode for precise reporting)
↓ runner reports through ruleContext
oxlint diagnostics anchored at the offending call site
Future ports (no/lazy-init-in-render, no-create-ref-during-render, deeper effect-event analyses) plug in as additional validators reading the same HIR.
The latest updates on your projects. Learn more about Vercel for GitHub.
| Project | Deployment | Actions | Updated (UTC) |
|---|---|---|---|
| react-doctor-website | Preview, Comment | May 17, 2026 3:28am |
🔴 React Review — 0/100 (unchanged) · 0 ❌ errors · 10 ⚠️ warnings
Copy prompt for agent
Check if these React Review issues are valid. If so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
Run this before and after your changes to verify the result:
npx react-doctor@latest --verbose --diff
Do not modify the react-doctor configuration unless explicitly asked.
Fix the underlying code issues instead of changing or suppressing the rules.
React Review found 0 errors and 10 warnings. This PR leaves the React health score unchanged.
<file name="packages/oxlint-plugin-react-doctor/src/plugin/hir/validators/validate-no-set-state-in-effect.ts">
<violation number="1" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/validators/validate-no-set-state-in-effect.ts:42">
Severity: Warning
instr.lvalue.identifier.id is read 8 times inside this loop — hoist into a const at the top of the loop body
Hoist the deep member access into a const at the top of the loop body: `const { x, y } = obj.deeply.nested`
Rule: `js-cache-property-access`
</violation>
<violation number="2" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/validators/validate-no-set-state-in-effect.ts:42">
Severity: Warning
instr.lvalue.identifier.id is read 8 times inside this loop — hoist into a const at the top of the loop body
Hoist the deep member access into a const at the top of the loop body: `const { x, y } = obj.deeply.nested`
Rule: `js-cache-property-access`
</violation>
</file>
<file name="packages/oxlint-plugin-react-doctor/src/plugin/hir/validators/validate-no-derived-computations-in-effects.ts">
<violation number="1" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/validators/validate-no-derived-computations-in-effects.ts:36">
Severity: Warning
instr.value.kind is read 6 times inside this loop — hoist into a const at the top of the loop body
Hoist the deep member access into a const at the top of the loop body: `const { x, y } = obj.deeply.nested`
Rule: `js-cache-property-access`
</violation>
<violation number="2" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/validators/validate-no-derived-computations-in-effects.ts:36">
Severity: Warning
instr.value.kind is read 6 times inside this loop — hoist into a const at the top of the loop body
Hoist the deep member access into a const at the top of the loop body: `const { x, y } = obj.deeply.nested`
Rule: `js-cache-property-access`
</violation>
<violation number="3" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/validators/validate-no-derived-computations-in-effects.ts:90">
Severity: Warning
array.includes() in a loop is O(n) per call — convert to a Set for O(1) lookups
Use a `Set` or `Map` for repeated membership tests / keyed lookups — `Array.includes`/`find` is O(n) per call
Rule: `js-set-map-lookups`
</violation>
</file>
<file name="packages/oxlint-plugin-react-doctor/src/plugin/hir/lower.ts">
<violation number="1" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/lower.ts:436">
Severity: Warning
place.identifier.id is read 3 times inside this loop — hoist into a const at the top of the loop body
Hoist the deep member access into a const at the top of the loop body: `const { x, y } = obj.deeply.nested`
Rule: `js-cache-property-access`
</violation>
<violation number="2" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/lower.ts:436">
Severity: Warning
place.identifier.id is read 3 times inside this loop — hoist into a const at the top of the loop body
Hoist the deep member access into a const at the top of the loop body: `const { x, y } = obj.deeply.nested`
Rule: `js-cache-property-access`
</violation>
</file>
<file name="packages/oxlint-plugin-react-doctor/src/plugin/hir/infer-types.ts">
<violation number="1" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/infer-types.ts:76">
Severity: Warning
instr.value.property is read 3 times inside this loop — hoist into a const at the top of the loop body
Hoist the deep member access into a const at the top of the loop body: `const { x, y } = obj.deeply.nested`
Rule: `js-cache-property-access`
</violation>
<violation number="2" location="packages/oxlint-plugin-react-doctor/src/plugin/hir/infer-types.ts:76">
Severity: Warning
instr.value.property is read 3 times inside this loop — hoist into a const at the top of the loop body
Hoist the deep member access into a const at the top of the loop body: `const { x, y } = obj.deeply.nested`
Rule: `js-cache-property-access`
</violation>
</file>
<file name="packages/react-doctor/tests/regressions/hir-port.test.ts">
<violation number="1" location="packages/react-doctor/tests/regressions/hir-port.test.ts:39">
Severity: Warning
.filter().map() iterates the array twice — combine into a single loop with .reduce() or for...of
Combine `.map().filter()` (or similar chains) into a single pass with `.reduce()` or a `for...of` loop to avoid iterating the array twice
Rule: `js-combine-iterations`
</violation>
</file>
Reviewed by react-review for commit 0fd8cf2d411c0adf2e3a8c05f74749b4bc3a3c3e. Configure here.

