Skip to content

[feat] migrate react-x/prefer-react-namespace-import to import-x/prefer-namespace-import Rule with Custom Patterns #1015

@Rel1cx

Description

@Rel1cx

Describe the problem

Context
This rule was previously proposed at #803. In the eslint-react's context only includes "react" and "react-dom" or other React renderer packages, which are less sensitive to tree-shaking needs. We propose migrating this rule to a more generalized implementation under import-x to serve broader use cases.

Rationale
While modern bundlers support tree-shaking for both import styles, many projects still rely on import * as mod from "source" to enforce tree-shaking or just for consistent code style. This rule remains necessary for such codebases.

Proposed Rule
Implement a import-x/prefer-namespace-import rule with a patterns configuration to match scoped/prefixed packages (e.g., @scope/*, prefix-*) and auto-fix them to namespace imports.

Configuration Example

{  
  "rules": {  
    "import-x/prefer-namespace-import": [  
      "error",  
      {  
        "patterns": ["foo"],  // Simple string match  
        // OR RegExp patterns  
        "patterns": ["/^@scope/", "/^prefix-/"]  
      }  
    ]  
  }  
}  

Auto-Fix Example

// Before  
import foo from "foo";  
  
// After auto-fix  
import * as foo from "foo";  

Describe the solution you'd like

No response

Alternatives considered

No response

Additional context

No response

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