@@ -4,23 +4,21 @@ import (
4
4
"fmt"
5
5
"go/ast"
6
6
"go/token"
7
- "sort"
8
7
"strings"
9
8
10
9
"github.com/golangci/golangci-lint/pkg/lint/astcache"
10
+ "github.com/golangci/golangci-lint/pkg/logutils"
11
11
"github.com/golangci/golangci-lint/pkg/result"
12
12
)
13
13
14
+ var nolintDebugf = logutils .Debug ("nolint" )
15
+
14
16
type ignoredRange struct {
15
17
linters []string
16
18
result.Range
17
19
col int
18
20
}
19
21
20
- func (i * ignoredRange ) isAdjacent (col , start int ) bool {
21
- return col == i .col && i .To == start - 1
22
- }
23
-
24
22
func (i * ignoredRange ) doesMatch (issue * result.Issue ) bool {
25
23
if issue .Line () < i .From || issue .Line () > i .To {
26
24
return false
@@ -81,25 +79,31 @@ func (p *Nolint) getOrCreateFileData(i *result.Issue) (*fileData, error) {
81
79
return nil , fmt .Errorf ("can't parse file %s: %s" , i .FilePath (), file .Err )
82
80
}
83
81
84
- fd .ignoredRanges = buildIgnoredRangesForFile (file .F , file .Fset )
82
+ fd .ignoredRanges = buildIgnoredRangesForFile (file .F , file .Fset , i .FilePath ())
83
+ nolintDebugf ("file %s: built nolint ranges are %+v" , i .FilePath (), fd .ignoredRanges )
85
84
return fd , nil
86
85
}
87
86
88
- func buildIgnoredRangesForFile (f * ast.File , fset * token.FileSet ) []ignoredRange {
87
+ func buildIgnoredRangesForFile (f * ast.File , fset * token.FileSet , filePath string ) []ignoredRange {
89
88
inlineRanges := extractFileCommentsInlineRanges (fset , f .Comments ... )
89
+ nolintDebugf ("file %s: inline nolint ranges are %+v" , filePath , inlineRanges )
90
90
91
91
if len (inlineRanges ) == 0 {
92
92
return nil
93
93
}
94
94
95
95
e := rangeExpander {
96
- fset : fset ,
97
- ranges : ignoredRanges ( inlineRanges ) ,
96
+ fset : fset ,
97
+ inlineRanges : inlineRanges ,
98
98
}
99
99
100
100
ast .Walk (& e , f )
101
101
102
- return e .ranges
102
+ // TODO: merge all ranges: there are repeated ranges
103
+ allRanges := append ([]ignoredRange {}, inlineRanges ... )
104
+ allRanges = append (allRanges , e .expandedRanges ... )
105
+
106
+ return allRanges
103
107
}
104
108
105
109
func (p * Nolint ) shouldPassIssue (i * result.Issue ) (bool , error ) {
@@ -117,38 +121,39 @@ func (p *Nolint) shouldPassIssue(i *result.Issue) (bool, error) {
117
121
return true , nil
118
122
}
119
123
120
- type ignoredRanges []ignoredRange
121
-
122
- func (ir ignoredRanges ) Len () int { return len (ir ) }
123
- func (ir ignoredRanges ) Swap (i , j int ) { ir [i ], ir [j ] = ir [j ], ir [i ] }
124
- func (ir ignoredRanges ) Less (i , j int ) bool { return ir [i ].To < ir [j ].To }
125
-
126
124
type rangeExpander struct {
127
- fset * token.FileSet
128
- ranges ignoredRanges
125
+ fset * token.FileSet
126
+ inlineRanges []ignoredRange
127
+ expandedRanges []ignoredRange
129
128
}
130
129
131
130
func (e * rangeExpander ) Visit (node ast.Node ) ast.Visitor {
132
131
if node == nil {
133
132
return e
134
133
}
135
134
136
- startPos := e .fset .Position (node .Pos ())
137
- start := startPos .Line
138
- end := e .fset .Position (node .End ()).Line
139
- found := sort .Search (len (e .ranges ), func (i int ) bool {
140
- return e .ranges [i ].To + 1 >= start
141
- })
142
-
143
- if found < len (e .ranges ) && e .ranges [found ].isAdjacent (startPos .Column , start ) {
144
- r := & e .ranges [found ]
145
- if r .From > start {
146
- r .From = start
147
- }
148
- if r .To < end {
149
- r .To = end
135
+ nodeStartPos := e .fset .Position (node .Pos ())
136
+ nodeStartLine := nodeStartPos .Line
137
+ nodeEndLine := e .fset .Position (node .End ()).Line
138
+
139
+ var foundRange * ignoredRange
140
+ for _ , r := range e .inlineRanges {
141
+ if r .To == nodeStartLine - 1 && nodeStartPos .Column == r .col {
142
+ foundRange = & r
143
+ break
150
144
}
151
145
}
146
+ if foundRange == nil {
147
+ return e
148
+ }
149
+
150
+ expandedRange := * foundRange
151
+ if expandedRange .To < nodeEndLine {
152
+ expandedRange .To = nodeEndLine
153
+ }
154
+ nolintDebugf ("found range is %v for node %#v [%d;%d], expanded range is %v" ,
155
+ * foundRange , node , nodeStartLine , nodeEndLine , expandedRange )
156
+ e .expandedRanges = append (e .expandedRanges , expandedRange )
152
157
153
158
return e
154
159
}
0 commit comments