File: linter.go

package info (click to toggle)
docker.io 27.5.1%2Bdfsg4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,384 kB
  • sloc: sh: 5,847; makefile: 1,146; ansic: 664; python: 162; asm: 133
file content (189 lines) | stat: -rw-r--r-- 4,458 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package linter

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/moby/buildkit/frontend/dockerfile/parser"
	"github.com/pkg/errors"
)

type Config struct {
	ExperimentalAll   bool
	ExperimentalRules []string
	ReturnAsError     bool
	SkipAll           bool
	SkipRules         []string
	Warn              LintWarnFunc
}

type Linter struct {
	CalledRules       []string
	ExperimentalAll   bool
	ExperimentalRules map[string]struct{}
	ReturnAsError     bool
	SkipAll           bool
	SkippedRules      map[string]struct{}
	Warn              LintWarnFunc
}

func New(config *Config) *Linter {
	toret := &Linter{
		SkippedRules:      map[string]struct{}{},
		ExperimentalRules: map[string]struct{}{},
		CalledRules:       []string{},
		Warn:              config.Warn,
	}
	toret.SkipAll = config.SkipAll
	toret.ExperimentalAll = config.ExperimentalAll
	toret.ReturnAsError = config.ReturnAsError
	for _, rule := range config.SkipRules {
		toret.SkippedRules[rule] = struct{}{}
	}
	for _, rule := range config.ExperimentalRules {
		toret.ExperimentalRules[rule] = struct{}{}
	}
	return toret
}

func (lc *Linter) Run(rule LinterRuleI, location []parser.Range, txt ...string) {
	if lc == nil || lc.Warn == nil || rule.IsDeprecated() {
		return
	}

	rulename := rule.RuleName()
	if rule.IsExperimental() {
		_, experimentalOk := lc.ExperimentalRules[rulename]
		if !(lc.ExperimentalAll || experimentalOk) {
			return
		}
	} else {
		_, skipOk := lc.SkippedRules[rulename]
		if lc.SkipAll || skipOk {
			return
		}
	}

	lc.CalledRules = append(lc.CalledRules, rulename)
	rule.Run(lc.Warn, location, txt...)
}

func (lc *Linter) Error() error {
	if lc == nil || !lc.ReturnAsError {
		return nil
	}
	if len(lc.CalledRules) == 0 {
		return nil
	}
	var rules []string
	uniqueRules := map[string]struct{}{}
	for _, r := range lc.CalledRules {
		uniqueRules[r] = struct{}{}
	}
	for r := range uniqueRules {
		rules = append(rules, r)
	}
	return errors.Errorf("lint violation found for rules: %s", strings.Join(rules, ", "))
}

type LinterRuleI interface {
	RuleName() string
	Run(warn LintWarnFunc, location []parser.Range, txt ...string)
	IsDeprecated() bool
	IsExperimental() bool
}

type LinterRule[F any] struct {
	Name         string
	Description  string
	Deprecated   bool
	Experimental bool
	URL          string
	Format       F
}

func (rule *LinterRule[F]) RuleName() string {
	return rule.Name
}

func (rule *LinterRule[F]) Run(warn LintWarnFunc, location []parser.Range, txt ...string) {
	if len(txt) == 0 {
		txt = []string{rule.Description}
	}
	short := strings.Join(txt, " ")
	warn(rule.Name, rule.Description, rule.URL, short, location)
}

func (rule *LinterRule[F]) IsDeprecated() bool {
	return rule.Deprecated
}

func (rule *LinterRule[F]) IsExperimental() bool {
	return rule.Experimental
}

func LintFormatShort(rulename, msg string, line int) string {
	msg = fmt.Sprintf("%s: %s", rulename, msg)
	if line > 0 {
		msg = fmt.Sprintf("%s (line %d)", msg, line)
	}
	return msg
}

type LintWarnFunc func(rulename, description, url, fmtmsg string, location []parser.Range)

func ParseLintOptions(checkStr string) (*Config, error) {
	checkStr = strings.TrimSpace(checkStr)
	if checkStr == "" {
		return &Config{}, nil
	}

	parts := strings.SplitN(checkStr, ";", 3)
	var skipSet, experimentalSet []string
	var errorOnWarn, skipAll, experimentalAll bool
	for _, p := range parts {
		k, v, ok := strings.Cut(p, "=")
		if !ok {
			return nil, errors.Errorf("invalid check option %q", p)
		}
		k = strings.TrimSpace(k)
		switch k {
		case "skip":
			v = strings.TrimSpace(v)
			if v == "all" {
				skipAll = true
			} else {
				skipSet = strings.Split(v, ",")
				for i, rule := range skipSet {
					skipSet[i] = strings.TrimSpace(rule)
				}
			}
		case "experimental":
			v = strings.TrimSpace(v)
			if v == "all" {
				experimentalAll = true
			} else {
				experimentalSet = strings.Split(v, ",")
				for i, rule := range experimentalSet {
					experimentalSet[i] = strings.TrimSpace(rule)
				}
			}
		case "error":
			v, err := strconv.ParseBool(strings.TrimSpace(v))
			if err != nil {
				return nil, errors.Wrapf(err, "failed to parse check option %q", p)
			}
			errorOnWarn = v
		default:
			return nil, errors.Errorf("invalid check option %q", k)
		}
	}
	return &Config{
		ExperimentalAll:   experimentalAll,
		ExperimentalRules: experimentalSet,
		SkipRules:         skipSet,
		SkipAll:           skipAll,
		ReturnAsError:     errorOnWarn,
	}, nil
}