Skip to content

Commit d303ecd

Browse files
authored
Merge pull request #8 from git-pkgs/fix/lint-issues
Fix all golangci-lint issues with extended linters
2 parents bd6f35b + 29ce1bc commit d303ecd

6 files changed

Lines changed: 115 additions & 96 deletions

File tree

.golangci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,9 @@ version: "2"
22
linters:
33
disable:
44
- unused
5+
settings:
6+
ireturn:
7+
allow:
8+
- error
9+
- stdlib
10+
- github.com/git-pkgs/spdx.Expression

category.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ func initCategoryMap() {
5252
return
5353
}
5454

55-
categoryMap = make(map[string]Category, len(licenseData)*2)
55+
// Each entry may have a primary SPDX key, alternative keys, and a license_key.
56+
estimatedKeys := 3
57+
categoryMap = make(map[string]Category, len(licenseData)*estimatedKeys)
5658
for _, entry := range licenseData {
5759
cat := Category(entry.Category)
5860
if cat == "" {

normalize.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ type transform func(string) string
160160

161161
var transforms = []transform{
162162
// Uppercase
163-
func(s string) string { return strings.ToUpper(s) },
163+
strings.ToUpper,
164164
// Trim whitespace
165-
func(s string) string { return strings.TrimSpace(s) },
165+
strings.TrimSpace,
166166
// Remove dots (M.I.T. -> MIT)
167167
func(s string) string { return strings.ReplaceAll(s, ".", "") },
168168
// Remove all whitespace (Apache- 2.0 -> Apache-2.0)
@@ -246,7 +246,7 @@ var transforms = []transform{
246246
if result != s && !strings.HasPrefix(result, "CC-") {
247247
result = "CC-" + result
248248
if !reCCVersion.MatchString(result) {
249-
result = result + "-4.0"
249+
result += "-4.0"
250250
}
251251
}
252252
return result

parse.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ const (
160160
tokenOpenParen
161161
tokenCloseParen
162162
tokenEOF
163+
164+
opAND = "AND"
165+
opOR = "OR"
166+
opWITH = "WITH"
163167
)
164168

165169
type token struct {
@@ -222,12 +226,12 @@ func (l *lexer) next() (token, error) {
222226
upper := strings.ToUpper(word)
223227

224228
switch upper {
225-
case "AND":
226-
return token{typ: tokenAnd, value: "AND"}, nil
227-
case "OR":
228-
return token{typ: tokenOr, value: "OR"}, nil
229-
case "WITH":
230-
return token{typ: tokenWith, value: "WITH"}, nil
229+
case opAND:
230+
return token{typ: tokenAnd, value: opAND}, nil
231+
case opOR:
232+
return token{typ: tokenOr, value: opOR}, nil
233+
case opWITH:
234+
return token{typ: tokenWith, value: opWITH}, nil
231235
}
232236

233237
// Check for DocumentRef or LicenseRef

parse_lax.go

Lines changed: 92 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func tokenizeForNormalization(expr string) []tokenForNorm {
4444
word := current.String()
4545
upper := strings.ToUpper(word)
4646
switch upper {
47-
case "AND", "OR", "WITH":
47+
case opAND, opOR, opWITH:
4848
tokens = append(tokens, tokenForNorm{value: upper, isOp: true})
4949
default:
5050
tokens = append(tokens, tokenForNorm{value: word})
@@ -76,109 +76,116 @@ func tokenizeForNormalization(expr string) []tokenForNorm {
7676
return tokens
7777
}
7878

79-
// normalizeTokens processes tokens and normalizes informal license names.
80-
func normalizeTokens(tokens []tokenForNorm) (string, error) {
81-
var result strings.Builder
82-
var licenseWords []string
83-
expectException := false // true if we just saw WITH
84-
85-
flushLicense := func() error {
86-
if len(licenseWords) == 0 {
87-
return nil
88-
}
79+
// tokenNormalizer holds state for normalizing a stream of tokens.
80+
type tokenNormalizer struct {
81+
result strings.Builder
82+
licenseWords []string
83+
expectException bool
84+
}
8985

90-
normalized, err := normalizeLicenseWords(licenseWords)
91-
if err != nil {
92-
return err
93-
}
86+
func (n *tokenNormalizer) flushPending() error {
87+
if n.expectException {
88+
return n.flushException()
89+
}
90+
return n.flushLicense()
91+
}
9492

95-
if result.Len() > 0 && !strings.HasSuffix(result.String(), "(") {
96-
result.WriteString(" ")
97-
}
98-
result.WriteString(normalized)
99-
licenseWords = nil
93+
func (n *tokenNormalizer) flushLicense() error {
94+
if len(n.licenseWords) == 0 {
10095
return nil
10196
}
10297

103-
flushException := func() error {
104-
if len(licenseWords) == 0 {
105-
return nil
106-
}
98+
normalized, err := normalizeLicenseWords(n.licenseWords)
99+
if err != nil {
100+
return err
101+
}
107102

108-
// Exception should be a single valid exception ID
109-
exc := strings.Join(licenseWords, "-")
110-
if lookupException(exc) == "" {
111-
// Try the original form
112-
exc = strings.Join(licenseWords, " ")
113-
if lookupException(exc) == "" {
114-
return &LicenseError{License: exc, Err: ErrInvalidException}
115-
}
116-
}
103+
if n.result.Len() > 0 && !strings.HasSuffix(n.result.String(), "(") {
104+
n.result.WriteString(" ")
105+
}
106+
n.result.WriteString(normalized)
107+
n.licenseWords = nil
108+
return nil
109+
}
117110

118-
result.WriteString(" ")
119-
result.WriteString(lookupException(exc))
120-
licenseWords = nil
111+
func (n *tokenNormalizer) flushException() error {
112+
if len(n.licenseWords) == 0 {
121113
return nil
122114
}
123115

124-
for _, tok := range tokens {
125-
if tok.isOp {
126-
if expectException {
127-
if err := flushException(); err != nil {
128-
return "", err
129-
}
130-
expectException = false
131-
} else {
132-
if err := flushLicense(); err != nil {
133-
return "", err
134-
}
135-
}
136-
result.WriteString(" ")
137-
result.WriteString(tok.value)
138-
if tok.value == "WITH" {
139-
expectException = true
140-
}
141-
} else if tok.isParen {
142-
if expectException {
143-
if err := flushException(); err != nil {
144-
return "", err
145-
}
146-
expectException = false
147-
} else {
148-
if err := flushLicense(); err != nil {
149-
return "", err
150-
}
151-
}
152-
if tok.value == "(" {
153-
if result.Len() > 0 && !strings.HasSuffix(result.String(), "(") && !strings.HasSuffix(result.String(), " ") {
154-
result.WriteString(" ")
155-
}
156-
result.WriteString("(")
157-
} else {
158-
result.WriteString(")")
159-
}
160-
} else if tok.isPlus {
161-
// Plus attaches to previous license word
162-
if len(licenseWords) > 0 {
163-
licenseWords[len(licenseWords)-1] += "+"
164-
}
165-
} else {
166-
// License word (or exception word if expectException)
167-
licenseWords = append(licenseWords, tok.value)
116+
// Exception should be a single valid exception ID
117+
exc := strings.Join(n.licenseWords, "-")
118+
if lookupException(exc) == "" {
119+
// Try the original form
120+
exc = strings.Join(n.licenseWords, " ")
121+
if lookupException(exc) == "" {
122+
return &LicenseError{License: exc, Err: ErrInvalidException}
168123
}
169124
}
170125

171-
if expectException {
172-
if err := flushException(); err != nil {
173-
return "", err
126+
n.result.WriteString(" ")
127+
n.result.WriteString(lookupException(exc))
128+
n.licenseWords = nil
129+
return nil
130+
}
131+
132+
func (n *tokenNormalizer) handleOp(tok tokenForNorm) error {
133+
if err := n.flushPending(); err != nil {
134+
return err
135+
}
136+
n.expectException = false
137+
n.result.WriteString(" ")
138+
n.result.WriteString(tok.value)
139+
if tok.value == opWITH {
140+
n.expectException = true
141+
}
142+
return nil
143+
}
144+
145+
func (n *tokenNormalizer) handleParen(tok tokenForNorm) error {
146+
if err := n.flushPending(); err != nil {
147+
return err
148+
}
149+
n.expectException = false
150+
if tok.value == "(" {
151+
if n.result.Len() > 0 && !strings.HasSuffix(n.result.String(), "(") && !strings.HasSuffix(n.result.String(), " ") {
152+
n.result.WriteString(" ")
174153
}
154+
n.result.WriteString("(")
175155
} else {
176-
if err := flushLicense(); err != nil {
156+
n.result.WriteString(")")
157+
}
158+
return nil
159+
}
160+
161+
// normalizeTokens processes tokens and normalizes informal license names.
162+
func normalizeTokens(tokens []tokenForNorm) (string, error) {
163+
n := &tokenNormalizer{}
164+
165+
for _, tok := range tokens {
166+
var err error
167+
switch {
168+
case tok.isOp:
169+
err = n.handleOp(tok)
170+
case tok.isParen:
171+
err = n.handleParen(tok)
172+
case tok.isPlus:
173+
if len(n.licenseWords) > 0 {
174+
n.licenseWords[len(n.licenseWords)-1] += "+"
175+
}
176+
default:
177+
n.licenseWords = append(n.licenseWords, tok.value)
178+
}
179+
if err != nil {
177180
return "", err
178181
}
179182
}
180183

181-
return strings.TrimSpace(result.String()), nil
184+
if err := n.flushPending(); err != nil {
185+
return "", err
186+
}
187+
188+
return strings.TrimSpace(n.result.String()), nil
182189
}
183190

184191
// normalizeLicenseWords takes a slice of words that should form a license name

spdx_correct_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "testing"
55
// TestSPDXCorrectCompatibility tests our implementation against spdx-correct.js test cases.
66
// spdx-correct.js is maintained by a license lawyer and is the de facto standard.
77
// Source: https://github.com/jslicense/spdx-correct.js/blob/main/test.js
8-
func TestSPDXCorrectCompatibility(t *testing.T) {
8+
func TestSPDXCorrectCompatibility(t *testing.T) { //nolint:maintidx // large table-driven test by design
99
// These are the test cases from spdx-correct.js with upgrade: true (default)
1010
cases := map[string]string{
1111
// BSD variants

0 commit comments

Comments
 (0)