Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Rel1cx opened this issue Mar 24, 2025 · 3 comments

Comments

@Rel1cx
Copy link

Rel1cx commented Mar 24, 2025

Context
This rule was previously proposed here Rel1cx/eslint-react#803. In the eslint-react's context only includes "react" and "react-dom" 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";  
@JounQin
Copy link
Member

JounQin commented Mar 24, 2025

I like this personally!

@JounQin
Copy link
Member

JounQin commented Apr 13, 2025

@Rel1cx Are you going to work on this or should I do the porting instead?

@Rel1cx
Copy link
Author

Rel1cx commented Apr 13, 2025

@JounQin I'll start working on this tomorrow.

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

No branches or pull requests

2 participants