react-doctor icon indicating copy to clipboard operation
react-doctor copied to clipboard

feat: port the React Compiler's HIR + validators into react-doctor

Open aidenybai opened this issue 6 months ago • 2 comments

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: flags setState calls inside useEffect bodies, including when the setter is propagated through const x = setX aliasing or wrapped in a useEffectEvent / inner FunctionExpression.
  • hir-no-derived-computations-in-effects: flags effects whose captures are entirely deps + setters and whose body has at least one intermediate const (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-5 StateTuple), with originNode on Place for diagnostic precision.
  • Lowering (plugin/hir/lower.ts): ESTree → HIR, parent-chained scope environments, FunctionExpression.capturedPlaces, full statement coverage (round-5 added for/while/do-while/for-of/for-in/switch/try/throw/labeled), SpreadElement unwrapping in call args.
  • Type inference (plugin/hir/infer-types.ts): hook recognition via LoadGlobal name → ReactType, propagation through LoadLocal/StoreLocal/PropertyLoad. Round-5 introduces StateTuple so only useState returns get the indexed StateValue/StateSetter tagging — useMemo/useContext returns 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-component WeakMap<EsTreeNode, HIRFunction> cache + originNode-based diagnostic anchoring so reports point at the offending setX(...) 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 lint clean
  • pnpm typecheck clean
  • pnpm format clean

Known v1 limitations (documented in code)

  • let reassignments 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 on LoadGlobal.
  • hir-no-set-state-in-effect overlaps noDerivedStateEffect on the simplest shape; users can disable either via react-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.

Open in Web Open in Cursor 

aidenybai avatar May 08 '26 02:05 aidenybai

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
react-doctor-website Ready Ready Preview, Comment May 17, 2026 3:28am

vercel[bot] avatar May 08 '26 02:05 vercel[bot]

🔴 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.

react-doctor[bot] avatar May 10 '26 05:05 react-doctor[bot]