Skip to content

Conversation

@hectahertz
Copy link
Contributor

@hectahertz hectahertz commented Oct 28, 2025

Closes https://github.com/github/primer/issues/5283

Adds a new rule that disallows usage of the useResponsiveValue hook from @primer/react or local imports.

The rule aims to enforce SSR-safe responsive patterns by preventing usage of a hook known to cause hydration mismatches, and is included in the recommended configuration.

Documentation and tests for the rule are also provided.

@hectahertz hectahertz requested a review from a team as a code owner October 28, 2025 16:48
@changeset-bot
Copy link

changeset-bot bot commented Oct 28, 2025

🦋 Changeset detected

Latest commit: ccb95f5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-primer-react Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a new ESLint rule no-use-responsive-value that prevents the use of the useResponsiveValue hook from @primer/react and local imports. The rule enforces SSR-safe patterns by disallowing this hook which relies on useMediaUnsafeSSR without a default state.

Key changes:

  • Implements a new ESLint rule to detect and flag imports of useResponsiveValue
  • Adds comprehensive test coverage for various import scenarios
  • Includes the rule in the recommended configuration preset

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/rules/no-use-responsive-value.js Implements the rule logic to detect useResponsiveValue imports from @primer/react and local paths
src/rules/__tests__/no-use-responsive-value.test.js Provides comprehensive test cases covering valid and invalid import scenarios
src/index.js Exports the new rule from the plugin
src/configs/recommended.js Adds the rule to the recommended configuration as an error
docs/rules/no-use-responsive-value.md Documents the rule's purpose, examples, and usage

// Check for import declarations
ImportDeclaration(node) {
// Check for @primer/react imports
const isPrimerImport = /@primer\/react/.test(node.source.value)
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

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

The regex /@primer\/react/ will match any path containing '@primer/react', including imports like @primer/react-native or some-@primer/react-utils. Use anchored regex /^@primer\/react(\/|$)/ to match only @primer/react and its sub-paths (e.g., /experimental).

Suggested change
const isPrimerImport = /@primer\/react/.test(node.source.value)
const isPrimerImport = /^@primer\/react(\/|$)/.test(node.source.value)

Copilot uses AI. Check for mistakes.
const isPrimerImport = /@primer\/react/.test(node.source.value)
// Check for local imports that might be useResponsiveValue hook
const isLocalUseResponsiveValueImport =
node.source.value.includes('useResponsiveValue') || node.source.value.includes('/hooks/useResponsiveValue')
Copy link

Copilot AI Oct 28, 2025

Choose a reason for hiding this comment

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

The condition node.source.value.includes('/hooks/useResponsiveValue') is redundant because it's already covered by the first condition node.source.value.includes('useResponsiveValue'). Additionally, this logic will create false positives for paths like '../hooks/useResponsiveValueHelper'. Consider using a more precise check that verifies the path ends with useResponsiveValue (e.g., /useResponsiveValue(\.js)?$/ or checking the last path segment).

Suggested change
node.source.value.includes('useResponsiveValue') || node.source.value.includes('/hooks/useResponsiveValue')
/(?:^|\/)useResponsiveValue(\.[jt]sx?)?$/.test(node.source.value)

Copilot uses AI. Check for mistakes.
@hectahertz hectahertz changed the title Add no-use-responsive-value Add no-use-responsive-value rule Oct 29, 2025
@hectahertz hectahertz enabled auto-merge (squash) October 31, 2025 09:45
@hectahertz hectahertz disabled auto-merge October 31, 2025 09:45
@hectahertz hectahertz enabled auto-merge (squash) October 31, 2025 09:45
@hectahertz hectahertz merged commit 8865e3a into main Oct 31, 2025
8 checks passed
@hectahertz hectahertz deleted the hectahertz/no-use-responsive-value branch October 31, 2025 09:46
@primer-css primer-css mentioned this pull request Oct 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants