Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | 6x 4x 6x 3x 3x 4x 4x 3x 3x 3x 3x 1x 1x 3x 3x 3x 57x 4x 1x 1x 1x 3x 5x 2x 5x 5x 5x 7x 7x 7x 7x 8x 8x 8x 5x 5x 5x 5x 5x 5x 45x 8x 8x 90x 7x 8x 8x 5x 5x 5x 2x 2x 2x 2x 2x 2x 2x 4x 4x 4x 2x 11x 9x 9x 5x 9x 9x 9x 8x 5x 5x 3x 5x 6x 3x 6x 6x 3x 1x 2x 3x 6x 5x 3x 5x 5x 6x 6x 4x 1x 1x 3x 3x 3x 3x 2x 2x 2x 2x 71x 6x 5x 4x 1x 4x 4x 2x 4x 4x 1x 4x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x | import path from 'node:path';
import * as ts from 'typescript';
import type { SemanticContext, SemanticProfile } from './semantic.js';
import type { Finding } from './types.js';
type FindingDraft = Omit<Finding, 'id'>;
// ─── Architecture Risk ──────────────────────────────────────────────────────
export function detectSemanticDeadExports(
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const [name, info] of profile.referenceCountByExport) {
if (info.count === 0) {
findings.push({
severity: 'high',
category: 'semantic-dead-export',
file: profile.file,
lineStart: info.lineStart,
lineEnd: info.lineEnd,
title: `Semantically dead export: ${name}`,
reason: `Exported symbol "${name}" has zero semantic references across the entire program (confirmed via TypeChecker, not just import matching).`,
files: [profile.file],
suggestedFix: {
strategy: 'Remove the export or delete the symbol if unused internally.',
steps: [
'Verify the symbol is not used via dynamic imports or runtime reflection.',
'Remove the export keyword, or delete the symbol entirely if also unused locally.',
'Re-run scan to confirm finding is resolved.',
],
},
impact: 'Dead exports bloat the public API surface and confuse contributors.',
tags: ['architecture', 'dead-code', 'semantic'],
lspHints: [{
tool: 'lspFindReferences',
symbolName: name,
lineHint: info.lineStart,
file: profile.file,
expectedResult: 'zero references confirms dead export',
}],
});
}
}
}
return findings;
}
export function detectOverAbstraction(
ctx: SemanticContext,
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
const interfaceImplCounts = new Map<string, { files: Set<string>; line: number; file: string }>();
for (const profile of profiles) {
const sourceFile = ctx.program.getSourceFile(path.resolve(ctx.root, profile.file));
Iif (!sourceFile) continue;
const visit = (node: ts.Node): void => {
if (ts.isInterfaceDeclaration(node) && node.name) {
const name = node.name.text;
const line = sourceFile!.getLineAndCharacterOfPosition(node.getStart(sourceFile!)).line + 1;
const impls = ctx.service.getImplementationAtPosition(
path.resolve(ctx.root, profile.file),
node.name.getStart(sourceFile!),
);
const implFiles = new Set<string>();
if (impls) {
for (const impl of impls) {
const implFile = impl.fileName;
if (implFile !== path.resolve(ctx.root, profile.file) || impl.textSpan.start !== node.getStart(sourceFile!)) {
implFiles.add(implFile);
}
}
}
if (!interfaceImplCounts.has(name)) {
interfaceImplCounts.set(name, { files: new Set(), line, file: profile.file });
}
const entry = interfaceImplCounts.get(name)!;
for (const f of implFiles) entry.files.add(f);
}
ts.forEachChild(node, visit);
};
ts.forEachChild(sourceFile, visit);
}
for (const [name, info] of interfaceImplCounts) {
if (info.files.size === 1) {
const implFile = [...info.files][0];
const relImpl = path.relative(ctx.root, implFile);
findings.push({
severity: 'medium',
category: 'over-abstraction',
file: info.file,
lineStart: info.line,
lineEnd: info.line,
title: `Over-abstraction: interface ${name} has exactly 1 implementor`,
reason: `Interface "${name}" is implemented only by one class in "${relImpl}". The abstraction layer adds complexity without enabling polymorphism.`,
files: [info.file, relImpl],
suggestedFix: {
strategy: 'Inline the interface into the concrete class or keep it only if future implementors are planned.',
steps: [
'Evaluate whether the interface is needed for testing (mocking) or future extensibility.',
'If not, merge the interface declaration into the concrete class.',
'Update consumers to depend on the concrete class directly.',
],
},
impact: 'Over-abstraction adds indirection without polymorphic benefit, increasing cognitive load.',
tags: ['architecture', 'abstraction', 'semantic'],
lspHints: [{
tool: 'lspFindReferences',
symbolName: name,
lineHint: info.line,
file: info.file,
expectedResult: 'exactly 1 implementation confirms over-abstraction',
}],
});
}
}
return findings;
}
export function detectConcreteDependency(
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const imp of profile.concreteImports) {
findings.push({
severity: 'medium',
category: 'concrete-dependency',
file: profile.file,
lineStart: imp.lineStart,
lineEnd: imp.lineStart,
title: `Concrete dependency: ${profile.file} imports class ${imp.name}`,
reason: `Module imports concrete class "${imp.name}" from "${imp.targetFile}" instead of an interface or abstract class. This violates the Dependency Inversion Principle (DIP).`,
files: [profile.file, imp.targetFile],
suggestedFix: {
strategy: 'Depend on an interface or abstract class instead of the concrete implementation.',
steps: [
'Extract an interface from the concrete class covering the methods used by this module.',
'Update imports to reference the interface instead of the concrete class.',
'Use dependency injection to provide the concrete implementation at runtime.',
],
},
impact: 'Concrete dependencies make modules harder to test and tightly coupled to implementation details.',
tags: ['architecture', 'dip', 'coupling', 'semantic'],
lspHints: [{
tool: 'lspGotoDefinition',
symbolName: imp.name,
lineHint: imp.lineStart,
file: profile.file,
expectedResult: 'resolves to concrete class (not interface/abstract)',
}],
});
}
}
return findings;
}
export function detectCircularTypeDependency(
ctx: SemanticContext,
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
const typeGraph = new Map<string, Set<string>>();
for (const profile of profiles) {
const sourceFile = ctx.program.getSourceFile(path.resolve(ctx.root, profile.file));
Iif (!sourceFile) continue;
const fileTypes = new Set<string>();
const fileTypeRefs = new Map<string, Set<string>>();
const visit = (node: ts.Node): void => {
if ((ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) && node.name) {
const typeName = `${profile.file}::${node.name.text}`;
fileTypes.add(typeName);
const refs = new Set<string>();
const collectRefs = (child: ts.Node): void => {
if (ts.isTypeReferenceNode(child) && ts.isIdentifier(child.typeName)) {
const refName = child.typeName.text;
const sym = ctx.checker.getSymbolAtLocation(child.typeName);
if (sym) {
const decl = sym.getDeclarations?.()?.[0];
if (decl) {
const declFile = decl.getSourceFile().fileName;
const relFile = path.relative(ctx.root, declFile);
refs.add(`${relFile}::${refName}`);
}
}
}
ts.forEachChild(child, collectRefs);
};
ts.forEachChild(node, collectRefs);
fileTypeRefs.set(typeName, refs);
}
ts.forEachChild(node, visit);
};
ts.forEachChild(sourceFile, visit);
for (const [typeName, refs] of fileTypeRefs) {
Eif (!typeGraph.has(typeName)) typeGraph.set(typeName, new Set());
for (const ref of refs) typeGraph.get(typeName)!.add(ref);
}
}
const visited = new Set<string>();
const inStack = new Set<string>();
const reportedCycles = new Set<string>();
const dfs = (node: string, stackPath: string[]): void => {
if (inStack.has(node)) {
const cycleStart = stackPath.indexOf(node);
if (cycleStart >= 0) {
const cycle = stackPath.slice(cycleStart);
const key = [...cycle].sort().join('→');
if (!reportedCycles.has(key) && cycle.length >= 2) {
reportedCycles.add(key);
const first = cycle[0];
const [file] = first.split('::');
findings.push({
severity: 'high',
category: 'circular-type-dependency',
file,
lineStart: 1,
lineEnd: 1,
title: `Circular type dependency: ${cycle.map((c) => c.split('::')[1]).join(' → ')}`,
reason: `Type-level circular dependency detected: ${cycle.map((c) => c.split('::')[1]).join(' → ')} → ${cycle[0].split('::')[1]}. Types reference each other creating a cycle.`,
files: [...new Set(cycle.map((c) => c.split('::')[0]))],
suggestedFix: {
strategy: 'Break the type cycle by extracting shared type definitions.',
steps: [
'Identify the minimal set of type properties causing the cycle.',
'Extract shared types to a dedicated types file that both sides can import.',
'Replace direct type references with the shared type.',
],
},
impact: 'Circular type dependencies make types harder to understand, refactor, and can cause issues with type inference.',
tags: ['architecture', 'types', 'cycle', 'semantic'],
});
}
}
return;
}
if (visited.has(node)) return;
inStack.add(node);
stackPath.push(node);
for (const neighbor of typeGraph.get(node) ?? []) {
dfs(neighbor, stackPath);
}
stackPath.pop();
inStack.delete(node);
visited.add(node);
};
for (const node of typeGraph.keys()) {
dfs(node, []);
}
return findings;
}
// ─── Code Quality ───────────────────────────────────────────────────────────
export function detectUnusedParameters(
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const param of profile.unusedParams) {
findings.push({
severity: 'medium',
category: 'unused-parameter',
file: profile.file,
lineStart: param.lineStart,
lineEnd: param.lineEnd,
title: `Unused parameter: ${param.paramName} in ${param.functionName}`,
reason: `Parameter "${param.paramName}" in function "${param.functionName}" is never referenced in the function body (confirmed via semantic analysis).`,
files: [profile.file],
suggestedFix: {
strategy: 'Remove the parameter or prefix with underscore to indicate intentional non-use.',
steps: [
'Check if the parameter is required by an interface or callback signature.',
'If not required, remove it and update all call sites.',
'If required by contract, prefix with _ (e.g. _unused) to signal intent.',
],
},
impact: 'Unused parameters add noise to function signatures and confuse callers about what the function actually needs.',
tags: ['code-quality', 'parameters', 'semantic'],
lspHints: [{
tool: 'lspFindReferences',
symbolName: param.paramName,
lineHint: param.lineStart,
file: profile.file,
expectedResult: 'zero non-declaration references confirms unused',
}],
});
}
}
return findings;
}
export function detectDeepOverrideChain(
profiles: SemanticProfile[],
threshold: number = 3,
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const chain of profile.overrideChains) {
if (chain.depth > threshold) {
findings.push({
severity: chain.depth > 4 ? 'high' : 'medium',
category: 'deep-override-chain',
file: profile.file,
lineStart: chain.lineStart,
lineEnd: chain.lineStart,
title: `Deep override chain: ${chain.className}.${chain.methodName} (depth ${chain.depth})`,
reason: `Method "${chain.methodName}" in class "${chain.className}" overrides a method ${chain.depth} levels up in the inheritance chain (threshold: ${threshold}).`,
files: [profile.file],
suggestedFix: {
strategy: 'Reduce override depth by flattening the class hierarchy or using the template method pattern.',
steps: [
'Identify if intermediate overrides are necessary or if they just pass through.',
'Consider extracting the behavior into a strategy or template method.',
'Flatten unnecessary intermediate classes.',
],
},
impact: 'Deep override chains make method behavior unpredictable — understanding what runs requires tracing through many classes.',
tags: ['code-quality', 'inheritance', 'override', 'semantic'],
});
}
}
}
return findings;
}
export function detectInterfaceCompliance(
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const impl of profile.interfaceImpls) {
const issues: string[] = [];
if (impl.missingMembers.length > 0) {
issues.push(`missing members: ${impl.missingMembers.join(', ')}`);
}
if (impl.anycastMembers.length > 0) {
issues.push(`any-cast members: ${impl.anycastMembers.join(', ')}`);
}
if (issues.length > 0) {
findings.push({
severity: impl.missingMembers.length > 0 ? 'high' : 'medium',
category: 'interface-compliance',
file: impl.classFile,
lineStart: impl.classLine,
lineEnd: impl.classLine,
title: `Fragile interface compliance: ${impl.className} implements ${impl.interfaceName}`,
reason: `Class "${impl.className}" implements "${impl.interfaceName}" with issues: ${issues.join('; ')}.`,
files: [impl.classFile],
suggestedFix: {
strategy: 'Fix the implementation to fully satisfy the interface contract.',
steps: [
...(impl.missingMembers.length > 0 ? [`Implement missing members: ${impl.missingMembers.join(', ')}.`] : []),
...(impl.anycastMembers.length > 0 ? [`Replace \`any\` types with proper types for: ${impl.anycastMembers.join(', ')}.`] : []),
'Enable strict type checking to catch these at compile time.',
],
},
impact: 'Incomplete interface implementations create runtime surprises and defeat the purpose of type contracts.',
tags: ['code-quality', 'types', 'interface', 'semantic'],
lspHints: [{
tool: 'lspGotoDefinition',
symbolName: impl.interfaceName,
lineHint: impl.classLine,
file: impl.classFile,
expectedResult: 'interface definition showing expected contract',
}],
});
}
}
}
return findings;
}
// ─── Dead Code & Hygiene ────────────────────────────────────────────────────
export function detectUnusedImports(
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const imp of profile.unusedImports) {
findings.push({
severity: 'low',
category: 'unused-import',
file: profile.file,
lineStart: imp.lineStart,
lineEnd: imp.lineStart,
title: `Unused import: ${imp.name}`,
reason: `Imported symbol "${imp.name}" is never referenced in this file (confirmed via semantic analysis, not just text matching).`,
files: [profile.file],
suggestedFix: {
strategy: 'Remove the unused import statement.',
steps: [
'Verify the import is not used for side effects (e.g. polyfills, CSS).',
'Remove the import statement.',
'If part of a multi-import, remove only the unused symbol.',
],
},
impact: 'Unused imports slow down IDE performance, increase bundle size (if not tree-shaken), and add noise.',
tags: ['dead-code', 'imports', 'semantic'],
lspHints: [{
tool: 'lspFindReferences',
symbolName: imp.name,
lineHint: imp.lineStart,
file: profile.file,
expectedResult: 'zero usage references confirms unused import',
}],
});
}
}
return findings;
}
export function detectOrphanImplementation(
ctx: SemanticContext,
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
const sourceFile = ctx.program.getSourceFile(path.resolve(ctx.root, profile.file));
Iif (!sourceFile) continue;
const visit = (node: ts.Node): void => {
if (ts.isClassDeclaration(node) && node.name) {
const hasHeritage = node.heritageClauses && node.heritageClauses.length > 0;
if (hasHeritage) {
ts.forEachChild(node, visit);
return;
}
const isExported = node.modifiers?.some(
(m) => m.kind === ts.SyntaxKind.ExportKeyword,
);
if (!isExported) {
ts.forEachChild(node, visit);
return;
}
const refs = ctx.service.findReferences(
path.resolve(ctx.root, profile.file),
node.name.getStart(sourceFile!),
);
let externalUsage = 0;
if (refs) {
for (const group of refs) {
for (const ref of group.references) {
if (!ref.isDefinition) {
const refFile = ref.fileName;
if (refFile !== path.resolve(ctx.root, profile.file)) {
externalUsage++;
}
}
}
}
}
if (externalUsage === 0) {
const line = sourceFile!.getLineAndCharacterOfPosition(node.getStart(sourceFile!)).line + 1;
findings.push({
severity: 'medium',
category: 'orphan-implementation',
file: profile.file,
lineStart: line,
lineEnd: line,
title: `Orphan implementation: class ${node.name.text}`,
reason: `Exported class "${node.name.text}" has no external references and does not implement any interface or extend any base class. It may be unreachable dead code.`,
files: [profile.file],
suggestedFix: {
strategy: 'Verify the class is needed and wire it in, or remove it.',
steps: [
'Check if the class is used via dynamic imports, reflection, or DI containers.',
'If unused, remove the class and its export.',
'If needed, wire it into the dependency graph via an interface or direct import.',
],
},
impact: 'Orphan implementations waste maintenance effort and bloat the codebase.',
tags: ['dead-code', 'class', 'orphan', 'semantic'],
lspHints: [{
tool: 'lspFindReferences',
symbolName: node.name.text,
lineHint: line,
file: profile.file,
expectedResult: 'zero external references confirms orphan',
}],
});
}
}
ts.forEachChild(node, visit);
};
ts.forEachChild(sourceFile, visit);
}
return findings;
}
// ─── Shotgun Surgery ────────────────────────────────────────────────────────
export function detectShotgunSurgery(
profiles: SemanticProfile[],
threshold: number = 8,
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const [name, info] of profile.referenceCountByExport) {
if (info.uniqueFiles >= threshold) {
findings.push({
severity: info.uniqueFiles > 12 ? 'high' : 'medium',
category: 'shotgun-surgery',
file: profile.file,
lineStart: info.lineStart,
lineEnd: info.lineEnd,
title: `Shotgun surgery risk: ${name} used in ${info.uniqueFiles} files`,
reason: `Exported symbol "${name}" is referenced from ${info.uniqueFiles} unique files (threshold: ${threshold}). Any change to this symbol forces coordinated edits across all consumers.`,
files: [profile.file],
suggestedFix: {
strategy: 'Reduce coupling by introducing a facade, adapter, or event-based decoupling.',
steps: [
'Identify the consumers and group them by usage pattern.',
'Extract a stable interface that consumers depend on instead of the implementation.',
'Consider the Mediator or Facade pattern to reduce direct dependencies.',
'If the symbol is a utility, ensure it has a single, well-defined responsibility.',
],
},
impact: 'High fan-out symbols are the #1 source of cascading changes during refactoring.',
tags: ['architecture', 'coupling', 'change-risk', 'semantic'],
lspHints: [{
tool: 'lspFindReferences',
symbolName: name,
lineHint: info.lineStart,
file: profile.file,
expectedResult: `${info.uniqueFiles}+ unique referencing files confirms shotgun surgery risk`,
}],
});
}
}
}
return findings;
}
// ─── Move-to-Caller (single-consumer export) ────────────────────────────────
export function detectMoveToCaller(
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const [name, info] of profile.referenceCountByExport) {
if (info.uniqueFiles === 1 && info.count > 0) {
findings.push({
severity: 'low',
category: 'move-to-caller',
file: profile.file,
lineStart: info.lineStart,
lineEnd: info.lineEnd,
title: `Single-consumer export: ${name} (used by 1 file)`,
reason: `Exported symbol "${name}" is consumed by exactly 1 file. Consider moving it to the consumer or inlining it to reduce module surface.`,
files: [profile.file],
suggestedFix: {
strategy: 'Move the symbol to its only consumer or inline it.',
steps: [
'Verify no dynamic or reflection-based usage exists.',
'Move the function/class/constant to the consumer file.',
'Remove the export and the import from the consumer.',
'If the symbol is large, keep it but remove the export keyword.',
],
},
impact: 'Single-consumer exports add unnecessary module surface and indirection.',
tags: ['dead-code', 'module-surface', 'refactoring', 'semantic'],
lspHints: [{
tool: 'lspFindReferences',
symbolName: name,
lineHint: info.lineStart,
file: profile.file,
expectedResult: 'exactly 1 referencing file confirms single-consumer',
}],
});
}
}
}
return findings;
}
// ─── Narrowable Type ────────────────────────────────────────────────────────
export function detectNarrowableType(
profiles: SemanticProfile[],
): FindingDraft[] {
const findings: FindingDraft[] = [];
for (const profile of profiles) {
for (const param of profile.narrowableParams) {
findings.push({
severity: 'low',
category: 'narrowable-type',
file: profile.file,
lineStart: param.lineStart,
lineEnd: param.lineEnd,
title: `Narrowable param: ${param.functionName}(${param.paramName}: ${param.declaredType}) → ${param.narrowedType}`,
reason: `Parameter "${param.paramName}" in "${param.functionName}" is declared as \`${param.declaredType}\` but all call sites pass \`${param.narrowedType}\`. The type can be safely narrowed.`,
files: [profile.file],
suggestedFix: {
strategy: 'Narrow the parameter type to match actual usage.',
steps: [
`Change the parameter type from \`${param.declaredType}\` to \`${param.narrowedType}\`.`,
'Verify no future callers need the broader type.',
'If the function is part of a public API, consider keeping the broad type with a narrower overload.',
],
},
impact: 'Overly broad parameter types weaken type checking — narrowing catches bugs at compile time.',
tags: ['code-quality', 'types', 'refactoring', 'semantic'],
lspHints: [{
tool: 'lspCallHierarchy',
symbolName: param.functionName,
lineHint: param.lineStart,
file: profile.file,
expectedResult: `all incoming calls pass ${param.narrowedType}`,
}],
});
}
}
return findings;
}
// ─── Orchestrator ───────────────────────────────────────────────────────────
export function runSemanticDetectors(
ctx: SemanticContext,
profiles: SemanticProfile[],
options: { overrideChainThreshold?: number; shotgunThreshold?: number } = {},
): FindingDraft[] {
const all: FindingDraft[] = [];
all.push(...detectOverAbstraction(ctx, profiles));
all.push(...detectConcreteDependency(profiles));
all.push(...detectCircularTypeDependency(ctx, profiles));
all.push(...detectUnusedParameters(profiles));
all.push(...detectDeepOverrideChain(profiles, options.overrideChainThreshold ?? 3));
all.push(...detectInterfaceCompliance(profiles));
all.push(...detectUnusedImports(profiles));
all.push(...detectOrphanImplementation(ctx, profiles));
all.push(...detectShotgunSurgery(profiles, options.shotgunThreshold ?? 8));
all.push(...detectMoveToCaller(profiles));
all.push(...detectNarrowableType(profiles));
return all;
}
|