@@ -46,7 +46,7 @@ func (i *ignoredRange) doesMatch(issue *result.Issue) bool {
46
46
47
47
// handle possible unused nolint directives
48
48
// nolintlint generates potential issues for every nolint directive and they are filtered out here
49
- if issue .FromLinter == golinters .NolintlintName && issue .ExpectNoLint {
49
+ if issue .FromLinter == golinters .NolintlintName && issue .ExpectNoLint {
50
50
if issue .ExpectedNoLintLinter != "" {
51
51
return i .matchedIssueFromLinter [issue .ExpectedNoLintLinter ]
52
52
}
@@ -218,11 +218,93 @@ func (e *rangeExpander) Visit(node ast.Node) ast.Visitor {
218
218
return e
219
219
}
220
220
221
+ func (p * Nolint ) extractRangeBeginFromComment (
222
+ text string ,
223
+ g ast.Node ,
224
+ fset * token.FileSet ,
225
+ ignoredRangeMap map [string ]* ignoredRange ,
226
+ ) map [string ]* ignoredRange {
227
+ text = strings .TrimLeft (text , "/ " )
228
+ if strings .HasPrefix (text , "nolint-begin:" ) {
229
+ linterItems := strings .Split (strings .TrimPrefix (text , "nolint-begin:" ), "," )
230
+ for _ , l := range linterItems {
231
+ l = strings .TrimSpace (l )
232
+ if l == "" {
233
+ continue
234
+ }
235
+ _ , ok := ignoredRangeMap [l ]
236
+ if ok {
237
+ // If there are two consecutive nolint-begins, the first nolint-begin is valid.
238
+ continue
239
+ }
240
+ ignoredRangeMap [l ] = & ignoredRange {
241
+ Range : result.Range {
242
+ From : fset .Position (g .Pos ()).Line ,
243
+ To : fset .Position (g .End ()).Line ,
244
+ },
245
+ linters : []string {l },
246
+ matchedIssueFromLinter : make (map [string ]bool ),
247
+ }
248
+ }
249
+ return ignoredRangeMap
250
+ }
251
+
252
+ if strings .HasPrefix (text , "nolint-begin" ) {
253
+ key := ""
254
+ ignoredRangeMap [key ] = & ignoredRange {
255
+ Range : result.Range {
256
+ From : fset .Position (g .Pos ()).Line ,
257
+ To : fset .Position (g .End ()).Line ,
258
+ },
259
+ linters : nil ,
260
+ matchedIssueFromLinter : make (map [string ]bool ),
261
+ }
262
+ }
263
+ return ignoredRangeMap
264
+ }
265
+
266
+ func (p * Nolint ) extractRangeEndFromComment (
267
+ text string ,
268
+ g ast.Node ,
269
+ fset * token.FileSet ,
270
+ ignoredRangeMap map [string ]* ignoredRange ,
271
+ ) (map [string ]* ignoredRange , * ignoredRange ) {
272
+ text = strings .TrimLeft (text , "/ " )
273
+ if strings .HasPrefix (text , "nolint-end" ) {
274
+ var linters []string
275
+ if strings .HasPrefix (text , "nolint-end:" ) {
276
+ linters = strings .Split (strings .TrimPrefix (text , "nolint-end:" ), "," )
277
+ } else {
278
+ linters = []string {"" }
279
+ }
280
+
281
+ for _ , linter := range linters {
282
+ linter = strings .TrimSpace (linter )
283
+ ir := ignoredRangeMap [linter ]
284
+ if ir != nil {
285
+ ir .Range .To = fset .Position (g .End ()).Line
286
+ ignoredRangeMap [linter ] = nil
287
+ return ignoredRangeMap , ir
288
+ }
289
+ }
290
+ }
291
+ return ignoredRangeMap , nil
292
+ }
293
+
221
294
func (p * Nolint ) extractFileCommentsInlineRanges (fset * token.FileSet , comments ... * ast.CommentGroup ) []ignoredRange {
222
295
var ret []ignoredRange
296
+ ignoredRangeMap := map [string ]* ignoredRange {}
223
297
for _ , g := range comments {
224
298
for _ , c := range g .List {
225
- ir := p .extractInlineRangeFromComment (c .Text , g , fset )
299
+ ignoredRangeMap = p .extractRangeBeginFromComment (c .Text , g , fset , ignoredRangeMap )
300
+
301
+ var ir * ignoredRange
302
+ ignoredRangeMap , ir = p .extractRangeEndFromComment (c .Text , g , fset , ignoredRangeMap )
303
+ if ir != nil {
304
+ ret = append (ret , * ir )
305
+ }
306
+
307
+ ir = p .extractInlineRangeFromComment (c .Text , g , fset )
226
308
if ir != nil {
227
309
ret = append (ret , * ir )
228
310
}
@@ -251,13 +333,14 @@ func (p *Nolint) extractInlineRangeFromComment(text string, g ast.Node, fset *to
251
333
}
252
334
}
253
335
254
- if ! strings .HasPrefix (text , "nolint:" ) {
336
+ text = strings .Split (text , "//" )[0 ] // allow another comment after this comment
337
+ text = strings .TrimSpace (text )
338
+ if text == "nolint" {
255
339
return buildRange (nil ) // ignore all linters
256
340
}
257
341
258
342
// ignore specific linters
259
343
var linters []string
260
- text = strings .Split (text , "//" )[0 ] // allow another comment after this comment
261
344
linterItems := strings .Split (strings .TrimPrefix (text , "nolint:" ), "," )
262
345
for _ , linter := range linterItems {
263
346
linterName := strings .ToLower (strings .TrimSpace (linter ))
0 commit comments