Skip to content

[Fix] no-namespace: do not autofix to invalid named imports - #3317

Open
Eliot5566 wants to merge 1 commit into
import-js:mainfrom
Eliot5566:no-namespace-reserved-names
Open

Eliot5566 wants to merge 1 commit into
import-js:mainfrom
Eliot5566:no-namespace-reserved-names

Conversation

@Eliot5566

Copy link
Copy Markdown

Fixes #3315.

The autofix turned every ns.prop and ns['prop'] into a named import with the same name, so ns.default, ns.enum (as in z.enum from zod) and ns['my-key'] became { default }, { enum } and { my-key }, which fail to parse.

What changes:

  • default, reserved words and strict-mode restricted names (eval, arguments) can be imported but not bound, so they now always get a local alias with the existing naming scheme: import { default as ns_default, enum as ns_enum } from './foo'. The usual conflict handling still applies (ns_default_1 if ns_default is taken).
  • A key that is not an identifier (ns['my-key']) has no named import equivalent without arbitrary module namespace names, so the rule still reports the namespace import but offers no fix, the same as when the namespace is used as an object.

Tests: three new cases in tests/src/rules/no-namespace.js (reserved names with aliases, a conflict on the alias, a non-identifier key with no fix). They fail without the change; the fixed output for the reserved-name case parses with espree at ecmaVersion: 2015. I also added the CHANGELOG entry.

I used Claude to help write and test this change.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Hq1ACTsK85bRrY28fUQ7b3

The fix turned `ns.default`, `ns.enum` and `ns['my-key']` into
`{ default }`, `{ enum }` and `{ my-key }`, which do not parse.
Reserved words and restricted names now get a local alias
(`{ default as ns_default }`), and a key that is not an identifier
leaves the import without a fix.

Fixes import-js#3315

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hq1ACTsK85bRrY28fUQ7b3
@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.50000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 79.60%. Comparing base (a60d715) to head (a849330).

Files with missing lines Patch % Lines
src/rules/no-namespace.js 87.50% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3317   +/-   ##
=======================================
  Coverage   79.59%   79.60%           
=======================================
  Files          98       98           
  Lines        4549     4556    +7     
  Branches     1543     1578   +35     
=======================================
+ Hits         3621     3627    +6     
- Misses        928      929    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

[no-namespace] autofix creates invalid named imports for default, reserved words, and non-identifier string keys

1 participant