← Back to Pull requests

feat: add `no-self-package-import` rule #597

Closed
unknown closed this pull request without merging on Nov 12, 2026 09:29 UTC
View on GitHub
unknown unknown commented on Nov 07, 2026 19:27 UTC
Author

Closes #557

Adds import-x/no-self-package-import, the opt-in counterpart to #309: it reports a bare specifier that imports the linted package through its own exports map.

// package.json: { "name": "my-package", "exports": { ".": "./dist/index.js", "./utils": "./dist/utils.js" } }
import { member } from 'my-package'        // reported
import { helper } from 'my-package/utils'  // reported

Scope, following the points listed in the issue:

  • Checks static imports, export ... from, dynamic import() and require() with a literal specifier, including subpaths (my-package/sub). Only paths the exports map actually exposes are reported (exact keys, patterns and null exclusions, matched as Node.js does), since an unexported path is not a self-reference. Targets are validated as Node.js does too: a path whose only targets don't start with ./ or contain a ., .. or node_modules segment, or whose match contains such a segment, is not reported. A fallback array counts when any of its targets is valid. A key ending in / matches nothing, and an exports object mixing subpath and condition keys (which Node.js rejects with ERR_INVALID_PACKAGE_CONFIG) exposes nothing.

  • The package identity comes from the package.json closest to the file, the same lookup Node.js uses for self-references and no-extraneous-dependencies uses since #309. Packages without exports (or with exports: null) are skipped, since Node.js self-references need exports. Such a package can still import itself through node_modules (e.g. a workspace link), which no-extraneous-dependencies may report (selfImport) unless the import is classed as internal or whitelisted.

  • When the specifier resolves, the resolved file's package name (getFilePackageName) must match, so a resolver alias that points the package's own name at another package is not reported. A specifier that resolves to a Node.js built-in (e.g. a package named events, or a name listed in import-x/core-modules) is not reported either.

  • Type-only imports/exports are reported by default. ignoreTypeImports: true skips them (same option name as max-dependencies).

  • allowInFiles takes globs for files that may self-import, such as tests. The globs are matched the same way as the devDependencies globs in no-extraneous-dependencies.

  • imports (#internal) specifiers are out of scope. The rule is not added to any config.
  • Also includes a docs/rules/no-self-package-import.md page, the README table entry generated by update:eslint-docs, and a minor changeset.

    Tests

    test/rules/no-self-package-import.spec.ts uses a new test/fixtures/no-self-package-import package (scoped name, exports with a subpath, plus a nested package without exports). It covers each import form, subpaths, unexported subpaths, patterns and null exclusions, invalid targets and matches, trailing-slash keys and mixed-key exports (checked against require.resolve on Node 24), fallback arrays, exports: null, built-in names, similarly named packages, a webpack-alias case, allowInFiles (absolute and cwd-relative globs) and type-only imports with and without ignoreTypeImports.

  • yarn test test/rules/no-self-package-import.spec.ts: 55 passed. Before adding the rule the suite failed to load (rule missing). With the alias guard removed, the alias case fails.

  • yarn test: 3899 passed, 1 skipped. yarn test-compiled: 3900 passed.

  • yarn lint (eslint, tsc, update:eslint-docs --check): passes.
  • The pre-commit tsc -p tsconfig.staged.json step fails on master with 4 errors in existing specs (export.spec.ts, no-duplicates.spec.ts, trailing-comments.spec.ts). The files added here produce no errors.

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

    Summary by CodeRabbit

    New Features
    Added a rule that flags imports where a package references itself through a path exposed in its exports map.
    The rule supports configurable file exclusions and optional skipping of type-only imports.
    Documentation
    * Added guidance and examples covering the rule’s behavior, options, and limitations.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 537 lines (+402 / -135) across 6 files Reviewer: thekhegay Review duration: 6601m
    All Vouch review confidence checks passed
    Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.48 · 537 lines diff across 6 files
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.376
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #597"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 47.9% (MEDIUM) · Higher = larger blast radius
    47.9%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 18.9% · Safe to merge
    18.9%