Skip to content

Feature Request: Support wildcard patterns in allowExportNames option #83

@csj

Description

@csj

Description

The react-refresh/only-export-components rule's allowExportNames option currently only supports exact matches. When attempting to use a RegExp pattern (/.*Context$/) or wildcard pattern (*Context), the rule still flags context exports as warnings.

Current Setup

// .eslintrc.js
module.exports = {
  rules: {
    'react-refresh/only-export-components': [
      'warn',
      { 
        allowConstantExport: true,
        allowExportNames: [/.*Context$/]
      }
    ]
  }
}

Current Behavior

Despite configuring the rule with a RegExp pattern to match context exports, the rule still produces warnings for files that export both a context and its provider:

export const ToastContext = createContext<DisplayToast>(defaultDisplayToast)
export const ToastProvider: FunctionComponent<ToastProviderProps> = ({ children }) => {
  // ...
}

Proposed Behavior

Allow pattern matching in allowExportNames using either:

  1. RegExp patterns: [/.*Context$/]
  2. Glob patterns: ["*Context"]

This would make the configuration more maintainable and align with pattern-matching capabilities found in other ESLint rules.

Use Cases

  1. Context exports (*Context)
  2. Framework-specific exports (e.g., Next.js get*Props, Remix *Loader)
  3. Common patterns in component libraries (*Provider, *Consumer)

Impact

This enhancement would:

  1. Reduce configuration boilerplate
  2. Make configurations more maintainable
  3. Better support common React patterns and conventions
  4. Align with pattern-matching capabilities found in other ESLint rules

Environment

  • eslint-plugin-react-refresh: 0.4.20
  • ESLint: 9.25.1
  • Node.js: v18.20.6
  • TypeScript: 5.4.2 (inferred from @typescript-eslint version)

Additional Context

The current workaround is to list each context export individually:

{
  allowExportNames: [
    "ToastContext",
    "ConfirmationContext",
    "LoaderContext",
    // ... and so on
  ]
}

This becomes unwieldy in larger applications with many contexts and makes the configuration harder to maintain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions