11package goheader
22
33import (
4- "go/token"
54 "strings"
65
76 goheader "github.com/denis-tingaikin/go-header"
@@ -14,118 +13,18 @@ import (
1413const linterName = "goheader"
1514
1615func New (settings * config.GoHeaderSettings , replacer * strings.Replacer ) * goanalysis.Linter {
17- conf := & goheader.Configuration {}
16+ conf := & goheader.Config {}
1817 if settings != nil {
19- conf = & goheader.Configuration {
18+ conf = & goheader.Config {
2019 Values : settings .Values ,
2120 Template : settings .Template ,
2221 TemplatePath : replacer .Replace (settings .TemplatePath ),
2322 }
2423 }
25-
26- analyzer := & analysis.Analyzer {
27- Name : linterName ,
28- Doc : goanalysis .TheOnlyanalyzerDoc ,
29- Run : func (pass * analysis.Pass ) (any , error ) {
30- err := runGoHeader (pass , conf )
31- if err != nil {
32- return nil , err
33- }
34-
35- return nil , nil
36- },
37- }
38-
3924 return goanalysis .NewLinter (
4025 linterName ,
4126 "Checks if file header matches to pattern" ,
42- []* analysis.Analyzer {analyzer },
27+ []* analysis.Analyzer {goheader . NewAnalyzer ( conf ) },
4328 nil ,
4429 ).WithLoadMode (goanalysis .LoadModeSyntax )
4530}
46-
47- func runGoHeader (pass * analysis.Pass , conf * goheader.Configuration ) error {
48- if conf .TemplatePath == "" && conf .Template == "" {
49- // User did not pass template, so then do not run go-header linter
50- return nil
51- }
52-
53- template , err := conf .GetTemplate ()
54- if err != nil {
55- return err
56- }
57-
58- values , err := conf .GetValues ()
59- if err != nil {
60- return err
61- }
62-
63- a := goheader .New (goheader .WithTemplate (template ), goheader .WithValues (values ))
64-
65- for _ , file := range pass .Files {
66- position , isGoFile := goanalysis .GetGoFilePosition (pass , file )
67- if ! isGoFile {
68- continue
69- }
70-
71- issue := a .Analyze (& goheader.Target {File : file , Path : position .Filename })
72- if issue == nil {
73- continue
74- }
75-
76- f := pass .Fset .File (file .Pos ())
77-
78- commentLine := 1
79- var offset int
80-
81- // Inspired by https://github.com/denis-tingaikin/go-header/blob/4c75a6a2332f025705325d6c71fff4616aedf48f/analyzer.go#L85-L92
82- if len (file .Comments ) > 0 && file .Comments [0 ].Pos () < file .Package {
83- if ! strings .HasPrefix (file .Comments [0 ].List [0 ].Text , "/*" ) {
84- // When the comment is "//" there is a one character offset.
85- offset = 1
86- }
87- commentLine = goanalysis .GetFilePositionFor (pass .Fset , file .Comments [0 ].Pos ()).Line
88- }
89-
90- // Skip issues related to build directives.
91- // https://github.com/denis-tingaikin/go-header/issues/18
92- if issue .Location ().Position - offset < 0 {
93- continue
94- }
95-
96- diag := analysis.Diagnostic {
97- Pos : f .LineStart (issue .Location ().Line + 1 ) + token .Pos (issue .Location ().Position - offset ), // The position of the first divergence.
98- Message : issue .Message (),
99- }
100-
101- if fix := issue .Fix (); fix != nil {
102- current := len (fix .Actual )
103- for _ , s := range fix .Actual {
104- current += len (s )
105- }
106-
107- start := f .LineStart (commentLine )
108-
109- end := start + token .Pos (current )
110-
111- header := strings .Join (fix .Expected , "\n " ) + "\n "
112-
113- // Adds an extra line between the package and the header.
114- if end == file .Package {
115- header += "\n "
116- }
117-
118- diag .SuggestedFixes = []analysis.SuggestedFix {{
119- TextEdits : []analysis.TextEdit {{
120- Pos : start ,
121- End : end ,
122- NewText : []byte (header ),
123- }},
124- }}
125- }
126-
127- pass .Report (diag )
128- }
129-
130- return nil
131- }
0 commit comments