@md-code/entities-uniqueness
ESLint rule and CLI that find semantically close entities — classes, interfaces, type
aliases, object literals, factory functions, and framework shapes (zod schemas, mobx stores,
redux/zustand state, class-validator models) — across a set of files.
Unlike a "duplicate a canonical component" tool, this analyzes the whole included set
against itself: every entity is compared with every other, so you surface near-duplicates
and extraction opportunities wherever they live.
flowchart LR
A["Included files<br/>classes · interfaces · types · zod · stores"] --> N["Normalize<br/>canonical types + framework shapes"]
N --> M["Match<br/>properties · methods · signature · name · inheritance"]
M --> D["Decide<br/>weighted confidence + tier"]
D --> F["Filter<br/>usages · inheritance · framework residual"]
F --> C["Cluster + Suggest<br/>shared base / shared type / subset"]
C --> R["Report + Catalog"]
Install
pnpm add -D @md-code/entities-uniqueness
| Package |
Where it comes from |
Why |
@md-code/utils |
dependencies |
the shared file index and the repo-root helpers (installed for you) |
typescript |
dependencies, ^5.5.0 |
every entity is parsed with the TypeScript compiler API |
eslint >=8.40.0 |
peerDependencies |
the rule is an ESLint rule; bring your own ESLint |
@md-code/sections |
optional peer ^0.3.0 |
only the --overview page / overviewPath needs it (see below) |
Installed for you as well (dependencies): @reduxjs/toolkit (redux state shapes),
minimatch (the globs) and ts-morph (the TypeScript AST).
Node >=18. The prebuilt overview bundle ships inside dist/, so a published install needs
no bundler, React or UI kit at all.
Minimal configuration — ESLint flat config plus one rule:
// eslint.config.js
const plugin = require("@md-code/entities-uniqueness");
module.exports = [
{
files: ["src/**/*.{ts,tsx}"],
plugins: { "md-code": plugin },
rules: {
// error tier → severity 2 (red).
"md-code/entities-uniqueness": ["error", { roots: ["src"] }],
// warn tier → severity 1 (yellow). ESLint fixes severity per RULE, so the
// warn tier is a second rule; enable it to see the yellow findings.
"md-code/entities-uniqueness-warn": ["warn", { roots: ["src"] }],
},
},
];
Quick start
# 1. build the catalog and see what it finds (writes reports/entities-catalog.json).
# Optional: with `cache: true` the rule refreshes this catalog itself on lint.
npx md-code-entities-uniqueness --roots src
# 2. lint — the rule reads that catalog and reports duplicates in the buffer
npx eslint src
Generate the catalog again whenever the scanned sources change; it is a build artifact and
should be gitignored. A plain npx md-code-entities-uniqueness with no flags uses the rule
options found in eslint.config.js (see Configuration discovery).
From this repository, build the package once and drive the CLI on its dist/ output:
pnpm build
node dist/main.js --roots packages/cli/src --report reports/entities-similarity.md
node dist/main.js --roots packages/cli/src --overview reports/entities-overview.html
node dist/main.js --help prints the authoritative flag list;
the table below is that list with the parser's types and defaults (plus --overview, which
the parser accepts and the usage line forgets).
How confidence is scored
Each pair of entities is scored on five signals, each in 0..1:
| Signal |
Weight |
What it measures |
properties |
0.25 |
Jaccard + overlap of property names, blended with type agreement |
methods |
0.30 |
Jaccard + overlap of method names |
signature |
0.20 |
Of shared methods, how many agree on canonical params + return type |
name |
0.15 |
Levenshtein + token overlap of the entity names |
inheritance |
0.10 |
Jaccard of extends/implements heritage |
Two-pool model. A signal that carries no information for a pair (e.g. two entities with
no methods) is not a neutral 1.0 — it reports hasEvidence: false and scores 0. The name
signal is always on (names always exist); the other four participate only when they have
evidence. The identity budget (the error threshold, 0.85 by default) is re-normalized over
the evidencing signals only, so a pair with no methods is judged on properties + name alone
instead of getting a free 0.30 from an empty methods set.
Properties signal details. The raw name overlap (0.6·jaccard + 0.4·overlap) is blended
with type agreement on the shared names: when shared keys disagree on canonical type
(Record<K, string> vs Record<K, number>), at most half of the name-overlap score is kept.
A small-shape discount then pulls scores just under 1.00 when the union of property names
is tiny — two unrelated {id, name} seed records sharing two generic fields is not a 1.00
match. The evidence object exposes jaccard, overlap, typeAgreement, aCount, bCount,
union.
Shape extraction. The normalizer surfaces the real shape of an entity:
- constructor parameter properties (
constructor(readonly from: string)) are promoted to
properties;
- numeric (
{1: ...}) and computed keys are not semantic properties and are skipped;
- an object whose members are factory functions (
queryOptions: () => ({...})) is scored on
the returned object's members, not on a single queryOptions property — otherwise any
two such objects would match at 1.00 on that one shared name.
Severity tiers:
| Tier |
Range |
Severity |
Meaning |
error |
≥ 0.85 |
error (2, red) |
Near-duplicate — strong candidate to delete/merge |
extract |
0.50 – 0.85 |
error (2, red) |
Strong extraction candidate |
warn |
0.35 – 0.50 |
warn (1, yellow) |
Weak similarity, informational |
Thresholds and weights are both configurable (see Rule options). Threshold
keys are error, extract and warn; an unknown/old key is a hard error listing the
accepted keys and what each means.
What it reports
The ESLint rule fires on each entity that participates in a matched pair:
| messageId |
Fires when |
Severity |
duplicateEntity |
The entity is a near-duplicate (tier error) |
error (2) |
nearDuplicateEntity |
The entity is an extraction candidate (tier extract) or weak similarity (tier warn) |
extract → error (2); warn → warn (1) |
indexNotBuilt |
The shared index is cold (see Shared file index) |
warn (1) |
The rule does not report a pair twice: an entity that matches several others is reported
once per matched partner, and the report and catalog carry the full picture — every pair with
all five signal scores, the clusters, and refactor suggestions.
Rule options
The rule option object is validated against the schema in index.ts (additionalProperties: false). Every key below is accepted; unknown keys are a configuration error.