Skip to content

lint: forbid inline mergeRefs calls - #5269

Merged
nynexman4464 merged 2 commits into
mainfrom
fix/enforce-stable-merged-refs
Aug 24, 2026
Merged

nynexman4464 merged 2 commits into
mainfrom
fix/enforce-stable-merged-refs

Conversation

@nynexman4464

@nynexman4464 nynexman4464 commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

What

Adds @astryx/no-inline-merge-refs as an error for core and updates the one violation that reached main after this branch was created.

The earlier parts of the original stack have landed:

The branch is rebuilt directly on current main, and TouchDateField now uses useMergedRefs for the callsite added in #5350.

Why enforcement is needed

The unsafe code looks natural:

<Component ref={mergeRefs(forwardedRef, internalRef)} />

But mergeRefs(...) is a callback factory. Calling it during render creates a new ref callback every time, forcing React to detach and reattach an unchanged element. #5266 shows that this can escalate into React error #185 when a ref callback schedules state during commit.

Providing useMergedRefs is not enough by itself: the unsafe utility remains available for non-React composition, and the inline JSX form is easy to reintroduce.

There is direct ecosystem precedent. Radix added scripts/check-composed-refs.mjs after the same React 19 “Maximum update depth exceeded” failure (radix-ui/primitives#3963). Its guard rejects the same two patterns enforced here.

What the rule rejects

// Invalid: new callback ref every render
<Component ref={mergeRefs(forwardedRef, internalRef)} />

// Invalid: the inline callback changes every render, so the merged ref does too
useMergedRefs(forwardedRef, node => setNode(node));

What remains valid

// Valid when called unconditionally under the normal Rules of Hooks
<Component ref={useMergedRefs(forwardedRef, internalRef)} />

const ref = useMergedRefs(forwardedRef, internalRef);
<Component ref={ref} />

The rule follows imported bindings, including aliases, and does not flag an unrelated local function that happens to be named mergeRefs.

The rule applies to core only; lab remains outside this migration's scope.

Validation

  • Confirmed current main fails on TouchDateField with exactly one @astryx/no-inline-merge-refs error; the same lint command passes after the fix.
  • Focused rule suite: 10 passed.
  • DateInputTouch.test.tsx: 134 passed.
  • pnpm lint:strict: passed.
  • pnpm build: passed.
  • pnpm test: passed.

@vercel

vercel Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
astryx Ready Ready Preview Aug 24, 2026 2:46pm

Request Review

@github-actions

github-actions Bot commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

DateInput (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 2714 -
Complexity N/A Very High (274) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.8KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@cixzhang

Copy link
Copy Markdown
Contributor

Leaving this one for a teammate to land rather than arming auto-merge, and noting why here so it isn't mistaken for neglect.

The rule itself is right and the PR is clean. The care is in the landing: a new lint rule doesn't only apply to new code — it fails every open branch that hasn't adopted the pattern yet. mergeRefs( currently appears in 52 files on main, and any open PR touching one of them starts failing lint through no fault of its author. With ~200 PRs open that's a lot of red appearing at once, on branches whose owners won't know why.

So this wants a person watching the first CI cycle after it merges, ready to answer "why did my PR go red" — not a bot arming it overnight. Best paired with #5266/#5267 so the rule and the thing it points people toward land together.

Approved and ready whenever someone can babysit it.

[Reviewed by Robohands]

Base automatically changed from fix/migrate-inline-merge-refs to main August 24, 2026 04:21

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — the rule is right, and the two PRs under it landed clean.

Correcting my own earlier note: I said the risk was open branches going red. It's main. TouchDateField landed in #5350 after you branched and carries the exact pattern — ref={mergeRefs(ref, inputRef)} at TouchDateField.tsx:1172. Run the rule against main today and that is the only ESLint error in packages/core/src. CI is green here because it last ran on a three-day-old merge base.

One for later, not this PR: Avatar.tsx:651 and Button.tsx:762 build the same unstable callback with a hoisted const, which your test marks valid.

Merge main and move that one line to useMergedRefs — then it's error-clean and I'd land it.

[Reviewed by Robohands]

@nynexman4464
nynexman4464 force-pushed the fix/enforce-stable-merged-refs branch from d9b991f to 12d798a Compare August 24, 2026 14:41
@github-actions
github-actions Bot requested a review from cixzhang August 24, 2026 14:41
github-actions Bot added a commit that referenced this pull request Aug 24, 2026
@nynexman4464
nynexman4464 dismissed cixzhang’s stale review August 24, 2026 14:59

Addressed on the refreshed head: rebuilt the rule directly on current main, migrated TouchDateField to useMergedRefs, and all checks pass.

@nynexman4464
nynexman4464 merged commit 8a055df into main Aug 24, 2026
19 checks passed
@github-actions
github-actions Bot deleted the fix/enforce-stable-merged-refs branch August 25, 2026 06:12

This branch was successfully deployed

1 active deployment
Preview — 12d798a4 Deployed Aug 24, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants