blob: ff762856177b3a5ab114495b4137e43d9b9ae77d [file] [log] [blame]
Tim Windelschmidte6cc2272024-09-19 16:32:55 +02001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
4package haslicense
5
6import (
7 "fmt"
8 "strings"
9
10 "golang.org/x/tools/go/analysis"
11
12 alib "source.monogon.dev/build/analysis/lib"
13)
14
15const header = `// Copyright The Monogon Project Authors.
16// SPDX-License-Identifier: Apache-2.0
17
18`
19
20var Analyzer = &analysis.Analyzer{
21 Name: "haslicense",
22 Doc: "haslicense checks for an existing license header in monogon source code.",
23 Run: func(p *analysis.Pass) (any, error) {
24 for _, file := range p.Files {
25 if alib.IsGeneratedFile(file) {
26 continue
27 }
28
29 if len(file.Comments) > 0 {
30 var sb strings.Builder
31 for _, c := range file.Comments[0].List {
32 sb.WriteString(c.Text)
33 sb.WriteRune('\n')
34 }
35 sb.WriteRune('\n')
36
37 if strings.HasPrefix(sb.String(), header) {
38 continue
39 }
40 }
41
42 p.Report(analysis.Diagnostic{
43 Pos: file.FileStart,
44 End: file.FileStart,
45 Message: "File is missing license header. Please add it.",
46 SuggestedFixes: []analysis.SuggestedFix{
47 {
48 Message: fmt.Sprintf("should prepend file with `%s`", header),
49 TextEdits: []analysis.TextEdit{
50 {
51 Pos: file.FileStart,
52 End: file.FileStart,
53 NewText: []byte(header),
54 },
55 },
56 },
57 },
58 })
59 }
60
61 return nil, nil
62 },
63}