BuildDirectiveSet creates a DirectiveSet from a slice of directives for efficient lookup.
(directives []Directive)
| 138 | |
| 139 | // BuildDirectiveSet creates a DirectiveSet from a slice of directives for efficient lookup. |
| 140 | func BuildDirectiveSet(directives []Directive) *DirectiveSet { |
| 141 | ds := &DirectiveSet{ |
| 142 | byLine: make(map[int][]Directive), |
| 143 | usedDirectives: make(map[int]bool), |
| 144 | } |
| 145 | |
| 146 | for _, d := range directives { |
| 147 | hasSkipFile := false |
| 148 | // Check for file-level skip-file directives |
| 149 | for _, rs := range d.Rules { |
| 150 | if rs.Severity == etscore.SeveritySkipFile { |
| 151 | ds.fileLevel = append(ds.fileLevel, rs) |
| 152 | hasSkipFile = true |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if d.IsNextLine { |
| 157 | // Next-line directives affect a specific line |
| 158 | affectedLine := d.AffectedLine() |
| 159 | ds.byLine[affectedLine] = append(ds.byLine[affectedLine], d) |
| 160 | } else if !hasSkipFile { |
| 161 | // Section directives (non-next-line, non-skip-file) apply from their position forward |
| 162 | // Filter out skip-file rules since they're already in fileLevel |
| 163 | sectionRules := make([]RuleSeverity, 0, len(d.Rules)) |
| 164 | for _, rs := range d.Rules { |
| 165 | if rs.Severity != etscore.SeveritySkipFile { |
| 166 | sectionRules = append(sectionRules, rs) |
| 167 | } |
| 168 | } |
| 169 | if len(sectionRules) > 0 { |
| 170 | sectionDirective := Directive{ |
| 171 | Line: d.Line, |
| 172 | IsNextLine: false, |
| 173 | Rules: sectionRules, |
| 174 | } |
| 175 | ds.sectionDirectives = append(ds.sectionDirectives, sectionDirective) |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | return ds |
| 181 | } |
| 182 | |
| 183 | // GetEffectiveSeverity returns the effective severity for a rule at a given line. |
| 184 | // It checks file-level suppressions first, then next-line directives, then section directives. |