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