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

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

Open Rel1cx opened this issue 9 months ago • 0 comments

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

Rel1cx avatar Mar 24 '25 08:03 Rel1cx