@@ -131,7 +131,7 @@ type AnalyzerGroup struct {
131
131
logger * log.Logger
132
132
analyzers []analyzer
133
133
postAnalyzers []PostAnalyzer
134
- filePatterns map [Type ][] * regexp. Regexp
134
+ filePatterns map [Type ]FilePatterns
135
135
detectionPriority types.DetectionPriority
136
136
}
137
137
@@ -149,8 +149,20 @@ type AnalysisInput struct {
149
149
}
150
150
151
151
type PostAnalysisInput struct {
152
- FS fs.FS
153
- Options AnalysisOptions
152
+ FS fs.FS
153
+ FilePatterns FilePatterns
154
+ Options AnalysisOptions
155
+ }
156
+
157
+ type FilePatterns []* regexp.Regexp
158
+
159
+ func (f FilePatterns ) Match (filePath string ) bool {
160
+ for _ , pattern := range f {
161
+ if pattern .MatchString (filePath ) {
162
+ return true
163
+ }
164
+ }
165
+ return false
154
166
}
155
167
156
168
type AnalysisOptions struct {
@@ -333,7 +345,7 @@ func NewAnalyzerGroup(opts AnalyzerOptions) (AnalyzerGroup, error) {
333
345
334
346
group := AnalyzerGroup {
335
347
logger : log .WithPrefix ("analyzer" ),
336
- filePatterns : make (map [Type ][] * regexp. Regexp ),
348
+ filePatterns : make (map [Type ]FilePatterns ),
337
349
detectionPriority : opts .DetectionPriority ,
338
350
}
339
351
for _ , p := range opts .FilePatterns {
@@ -349,10 +361,6 @@ func NewAnalyzerGroup(opts AnalyzerOptions) (AnalyzerGroup, error) {
349
361
return group , xerrors .Errorf ("invalid file regexp (%s): %w" , p , err )
350
362
}
351
363
352
- if _ , ok := group .filePatterns [Type (fileType )]; ! ok {
353
- group .filePatterns [Type (fileType )] = []* regexp.Regexp {}
354
- }
355
-
356
364
group .filePatterns [Type (fileType )] = append (group .filePatterns [Type (fileType )], r )
357
365
}
358
366
@@ -422,7 +430,7 @@ func (ag AnalyzerGroup) AnalyzeFile(ctx context.Context, wg *sync.WaitGroup, lim
422
430
continue
423
431
}
424
432
425
- if ! ag .filePatternMatch ( a .Type (), cleanPath ) && ! a .Required (cleanPath , info ) {
433
+ if ! ag .filePatterns [ a .Type ()]. Match ( cleanPath ) && ! a .Required (cleanPath , info ) {
426
434
continue
427
435
}
428
436
rc , err := opener ()
@@ -468,7 +476,7 @@ func (ag AnalyzerGroup) RequiredPostAnalyzers(filePath string, info os.FileInfo)
468
476
}
469
477
var postAnalyzerTypes []Type
470
478
for _ , a := range ag .postAnalyzers {
471
- if ag .filePatternMatch ( a .Type (), filePath ) || a .Required (filePath , info ) {
479
+ if ag .filePatterns [ a .Type ()]. Match ( filePath ) || a .Required (filePath , info ) {
472
480
postAnalyzerTypes = append (postAnalyzerTypes , a .Type ())
473
481
}
474
482
}
@@ -479,7 +487,8 @@ func (ag AnalyzerGroup) RequiredPostAnalyzers(filePath string, info os.FileInfo)
479
487
// and passes it to the respective post-analyzer.
480
488
// The obtained results are merged into the "result".
481
489
// This function may be called concurrently and must be thread-safe.
482
- func (ag AnalyzerGroup ) PostAnalyze (ctx context.Context , compositeFS * CompositeFS , result * AnalysisResult , opts AnalysisOptions ) error {
490
+ func (ag AnalyzerGroup ) PostAnalyze (ctx context.Context , compositeFS * CompositeFS , result * AnalysisResult ,
491
+ opts AnalysisOptions ) error {
483
492
for _ , a := range ag .postAnalyzers {
484
493
fsys , ok := compositeFS .Get (a .Type ())
485
494
if ! ok {
@@ -510,8 +519,9 @@ func (ag AnalyzerGroup) PostAnalyze(ctx context.Context, compositeFS *CompositeF
510
519
}
511
520
512
521
res , err := a .PostAnalyze (ctx , PostAnalysisInput {
513
- FS : filteredFS ,
514
- Options : opts ,
522
+ FS : filteredFS ,
523
+ FilePatterns : ag .filePatterns [a .Type ()],
524
+ Options : opts ,
515
525
})
516
526
if err != nil {
517
527
return xerrors .Errorf ("post analysis error: %w" , err )
@@ -526,15 +536,6 @@ func (ag AnalyzerGroup) PostAnalyzerFS() (*CompositeFS, error) {
526
536
return NewCompositeFS ()
527
537
}
528
538
529
- func (ag AnalyzerGroup ) filePatternMatch (analyzerType Type , filePath string ) bool {
530
- for _ , pattern := range ag .filePatterns [analyzerType ] {
531
- if pattern .MatchString (filePath ) {
532
- return true
533
- }
534
- }
535
- return false
536
- }
537
-
538
539
// StaticPaths collects static paths from all enabled analyzers
539
540
// It returns the collected paths and a boolean indicating if all enabled analyzers implement StaticPathAnalyzer
540
541
func (ag AnalyzerGroup ) StaticPaths (disabled []Type ) ([]string , bool ) {
@@ -546,6 +547,12 @@ func (ag AnalyzerGroup) StaticPaths(disabled []Type) ([]string, bool) {
546
547
continue
547
548
}
548
549
550
+ // We can't be sure that the file pattern uses a static path.
551
+ // So we don't need to use `StaticPath` logic if any enabled analyzer has a file pattern.
552
+ if _ , ok := ag .filePatterns [a .Type ()]; ok {
553
+ return nil , false
554
+ }
555
+
549
556
// If any analyzer doesn't implement StaticPathAnalyzer, return false
550
557
staticPathAnalyzer , ok := a .(StaticPathAnalyzer )
551
558
if ! ok {
0 commit comments