-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmodule_boundaries.go
More file actions
99 lines (88 loc) · 2.8 KB
/
module_boundaries.go
File metadata and controls
99 lines (88 loc) · 2.8 KB
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
package main
import (
"slices"
"strings"
)
// ModuleBoundaryViolation represents a module boundary violation
type ModuleBoundaryViolation struct {
FilePath string
ImportPath string
ImportRequest string
RuleName string
ViolationType string // "denied" or "not_allowed"
}
// CheckModuleBoundariesFromTree checks for module boundary violations using a pre-built dependency tree
func CheckModuleBoundariesFromTree(
minimalTree MinimalDependencyTree,
files []string,
boundaries []BoundaryRule,
cwd string,
) []ModuleBoundaryViolation {
var violations []ModuleBoundaryViolation
// Compile matchers for all boundaries
type CompiledBoundary struct {
Rule BoundaryRule
PatternMatchers []GlobMatcher
AllowMatchers []GlobMatcher
DenyMatchers []GlobMatcher
}
compiledBoundaries := make([]CompiledBoundary, 0, len(boundaries))
for _, boundary := range boundaries {
cb := CompiledBoundary{
Rule: boundary,
PatternMatchers: CreateGlobMatchers([]string{boundary.Pattern}, cwd),
AllowMatchers: CreateGlobMatchers(boundary.Allow, cwd),
DenyMatchers: CreateGlobMatchers(boundary.Deny, cwd),
}
compiledBoundaries = append(compiledBoundaries, cb)
}
// Check violations
for _, filePath := range files {
// Find which boundaries apply to this file
for _, boundary := range compiledBoundaries {
if MatchesAnyGlobMatcher(filePath, boundary.PatternMatchers, false) {
// Check dependencies
fileDeps, ok := minimalTree[filePath]
if !ok {
continue
}
for _, dep := range fileDeps {
if dep.ID != "" && (dep.ResolvedType == UserModule || dep.ResolvedType == MonorepoModule) {
resolvedPath := dep.ID
// Check if denied
if len(boundary.DenyMatchers) > 0 && MatchesAnyGlobMatcher(resolvedPath, boundary.DenyMatchers, false) {
violations = append(violations, ModuleBoundaryViolation{
FilePath: filePath,
ImportPath: resolvedPath,
ImportRequest: dep.Request,
RuleName: boundary.Rule.Name,
ViolationType: "denied",
})
continue
}
// Check if allowed
if len(boundary.AllowMatchers) > 0 {
if !MatchesAnyGlobMatcher(resolvedPath, boundary.AllowMatchers, false) {
violations = append(violations, ModuleBoundaryViolation{
FilePath: filePath,
ImportPath: resolvedPath,
ImportRequest: dep.Request,
RuleName: boundary.Rule.Name,
ViolationType: "not_allowed",
})
}
}
}
}
}
}
}
// Sort violations for consistent output
slices.SortFunc(violations, func(a, b ModuleBoundaryViolation) int {
if a.FilePath != b.FilePath {
return strings.Compare(a.FilePath, b.FilePath)
}
return strings.Compare(a.ImportPath, b.ImportPath)
})
return violations
}