chore: refactor hardcoded values to constants (v0.5.1)

Major internal refactoring to eliminate hardcoded values and improve
maintainability. Guardian now fully passes its own quality checks!

Changes:
- Extract all RepositoryViolation messages to domain constants
- Extract all framework leak template strings to centralized constants
- Extract all layer paths to infrastructure constants
- Extract all regex patterns to IMPORT_PATTERNS constant
- Add 30+ new constants for better maintainability

New files:
- src/infrastructure/constants/paths.ts (layer paths, patterns)
- src/domain/constants/Messages.ts (25+ repository messages)
- src/domain/constants/FrameworkCategories.ts (framework categories)
- src/shared/constants/layers.ts (layer names)

Impact:
- Reduced hardcoded values from 37 to 1 (97% improvement)
- Guardian passes its own src/ directory checks with 0 violations
- All 292 tests still passing (100% pass rate)
- No breaking changes - fully backwards compatible

Test results:
- 292 tests passing (100% pass rate)
- 96.77% statement coverage
- 83.82% branch coverage
This commit is contained in:
imfozilbek
2025-11-24 20:12:08 +05:00
parent 0534fdf1bd
commit a34ca85241
19 changed files with 416 additions and 96 deletions

View File

@@ -1,6 +1,7 @@
import { IDependencyDirectionDetector } from "../../domain/services/IDependencyDirectionDetector"
import { DependencyViolation } from "../../domain/value-objects/DependencyViolation"
import { LAYERS } from "../../shared/constants/rules"
import { IMPORT_PATTERNS, LAYER_PATHS } from "../constants/paths"
/**
* Detects dependency direction violations between architectural layers
@@ -118,13 +119,13 @@ export class DependencyDirectionDetector implements IDependencyDirectionDetector
* @returns The layer name if detected, undefined otherwise
*/
public extractLayerFromImport(importPath: string): string | undefined {
const normalizedPath = importPath.replace(/['"]/g, "").toLowerCase()
const normalizedPath = importPath.replace(IMPORT_PATTERNS.QUOTE, "").toLowerCase()
const layerPatterns: Array<[string, string]> = [
[LAYERS.DOMAIN, "/domain/"],
[LAYERS.APPLICATION, "/application/"],
[LAYERS.INFRASTRUCTURE, "/infrastructure/"],
[LAYERS.SHARED, "/shared/"],
const layerPatterns: [string, string][] = [
[LAYERS.DOMAIN, LAYER_PATHS.DOMAIN],
[LAYERS.APPLICATION, LAYER_PATHS.APPLICATION],
[LAYERS.INFRASTRUCTURE, LAYER_PATHS.INFRASTRUCTURE],
[LAYERS.SHARED, LAYER_PATHS.SHARED],
]
for (const [layer, pattern] of layerPatterns) {
@@ -163,19 +164,16 @@ export class DependencyDirectionDetector implements IDependencyDirectionDetector
private extractImports(line: string): string[] {
const imports: string[] = []
const esImportRegex =
/import\s+(?:{[^}]*}|[\w*]+(?:\s+as\s+\w+)?|\*\s+as\s+\w+)\s+from\s+['"]([^'"]+)['"]/g
let match = esImportRegex.exec(line)
let match = IMPORT_PATTERNS.ES_IMPORT.exec(line)
while (match) {
imports.push(match[1])
match = esImportRegex.exec(line)
match = IMPORT_PATTERNS.ES_IMPORT.exec(line)
}
const requireRegex = /require\s*\(\s*['"]([^'"]+)['"]\s*\)/g
match = requireRegex.exec(line)
match = IMPORT_PATTERNS.REQUIRE.exec(line)
while (match) {
imports.push(match[1])
match = requireRegex.exec(line)
match = IMPORT_PATTERNS.REQUIRE.exec(line)
}
return imports