← Back to Pull requests

feat(no-useless-path-segments): report useless index in aliased imports #596

Open
unknown opened this pull request with 328 lines diff across 4 files
View on GitHub
unknown unknown commented on Nov 07, 2026 08:40 UTC
Author

Fixes #589

With noUselessIndex: true, import foo from '@/test/index.js' was never reported, while ./test/index.js was. The rule returns early for every specifier that doesn't start with ., so aliased paths never reach the index check.

Upstream (import-js/eslint-plugin-import#2939) this was turned down because a bare specifier usually maps to a package, and a package's exports may not allow the shorter path. So this change only handles that narrow case and stays out of packages:

  • The import has to be internal according to importType, meaning it resolves to a file inside the project and not under node_modules (or another external module folder). a/index and an alias that points into node_modules are left alone.

  • The rule only proposes the parent path (@/test, or @/test/ as a second try) if the resolver maps it to the same file as the original import. If neither does (for example @/bar resolves to bar.js instead of bar/index.js), nothing is reported, so the autofix can't change which file is imported.
  • Relative imports go through the same code as before. The docs mention the alias case, and there's a changeset.

    Tests: valid and invalid cases for an aliased index and index.js, an ambiguous @/bar/index.js, an unresolved alias, an external package and an alias into node_modules, using createNodeResolver({ alias }) on the existing fixtures.

    How I verified: I couldn't run the jest suite locally, so CI is the first run of the spec. I ran the same cases through ESLint's Linter with the published 4.18.0 build, once as released and once with this change ported into its compiled rule, against this repo's test/fixtures and the same alias config. 4.18.0 reports nothing for @/importType/index and @/importType/index.js. With the change, both are reported and fixed to @/importType, and every valid case above stays clean. Relative imports like ./importType/index give the same result as before.

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->
    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    When noUselessIndex is enabled, no-useless-path-segments can flag unnecessary /index segments in aliased imports of local files when shortening the path resolves to the same file. Alias roots, external packages, unresolved paths, and Node subpath imports are excluded.
    Documentation
    Clarified which aliased imports are checked, how resolution is determined, and how differing resolver settings may affect paths.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 328 lines (+246 / -82) across 4 files Reviewer: thekhegay Review duration: 10319m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.42) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 328 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.347
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #596"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.1% (MEDIUM) · Higher = larger blast radius
    42.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.4% · Safe to merge
    22.4%