xref: /aosp_15_r20/external/bazelbuild-rules_go/go/tools/builders/nolint.go (revision 9bb1b549b6a84214c53be0924760be030e66b93a)
1// Copyright 2023 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import "strings"
18
19// Parse nolint directives and return the applicable linters. If all linters
20// apply, returns (nil, true).
21func parseNolint(text string) (map[string]bool, bool) {
22	text = strings.TrimLeft(text, "/ ")
23	if !strings.HasPrefix(text, "nolint") {
24		return nil, false
25	}
26	parts := strings.Split(text, ":")
27	if len(parts) == 1 {
28		return nil, true
29	}
30	linters := strings.Split(parts[1], ",")
31	result := map[string]bool{}
32	for _, linter := range linters {
33		if strings.EqualFold(linter, "all") {
34			return nil, true
35		}
36		result[linter] = true
37	}
38	return result, true
39}
40