feat: add `no-self-package-import` rule #597
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
thekhegay
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 ownexportsmap.Scope, following the points listed in the issue:
export ... from, dynamicimport()andrequire()with a literal specifier, including subpaths (my-package/sub). Only paths theexportsmap actually exposes are reported (exact keys,patterns andnullexclusions, 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.,..ornode_modulessegment, or whosematch 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 anexportsobject mixing subpath and condition keys (which Node.js rejects withERR_INVALID_PACKAGE_CONFIG) exposes nothing.package.jsonclosest to the file, the same lookup Node.js uses for self-references andno-extraneous-dependenciesuses since #309. Packages withoutexports(or withexports: null) are skipped, since Node.js self-references needexports. Such a package can still import itself throughnode_modules(e.g. a workspace link), whichno-extraneous-dependenciesmay report (selfImport) unless the import is classed as internal or whitelisted.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 namedevents, or a name listed inimport-x/core-modules) is not reported either.ignoreTypeImports: trueskips them (same option name asmax-dependencies).allowInFilestakes globs for files that may self-import, such as tests. The globs are matched the same way as thedevDependenciesglobs inno-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.mdpage, the README table entry generated byupdate:eslint-docs, and a minor changeset.Tests
test/rules/no-self-package-import.spec.tsuses a newtest/fixtures/no-self-package-importpackage (scoped name,exportswith a subpath, plus a nested package withoutexports). It covers each import form, subpaths, unexported subpaths,patterns andnullexclusions, invalid targets andmatches, trailing-slash keys and mixed-keyexports(checked againstrequire.resolveon 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 withoutignoreTypeImports.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.jsonstep fails onmasterwith 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 -->