Skip to content

Commit a8f7ebd

Browse files
add star-block like headers support
Signed-off-by: Denis Tingaikin <[email protected]>
1 parent adc22d3 commit a8f7ebd

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

analyzer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ func (a *Analyzer) Analyze(target *Target) (i Issue) {
9696
if len(file.Comments) > 0 && file.Comments[0].Pos() < file.Package {
9797
if strings.HasPrefix(file.Comments[0].List[0].Text, "/*") {
9898
header = (&ast.CommentGroup{List: []*ast.Comment{file.Comments[0].List[0]}}).Text()
99+
header = trimEachLine(strings.TrimSpace(header), strings.TrimSpace)
100+
header = trimEachLine(header, func(s string) string {
101+
return strings.TrimFunc(s, func(r rune) bool {
102+
return r == '*'
103+
})
104+
})
105+
header = trimEachLine(strings.TrimSpace(header), strings.TrimSpace)
99106
} else {
100107
header = file.Comments[0].Text()
101108
offset.Position += 3
@@ -112,6 +119,7 @@ func (a *Analyzer) Analyze(target *Target) (i Issue) {
112119
i = NewIssueWithFix(i.Message(), i.Location(), fix)
113120
}()
114121
header = strings.TrimSpace(header)
122+
115123
if header == "" {
116124
return NewIssue("Missed header for check")
117125
}
@@ -305,3 +313,11 @@ func countLines(text string) int {
305313
}
306314
return lines
307315
}
316+
317+
func trimEachLine(input string, trimFunc func(string) string) string {
318+
lines := strings.Split(input, "\n")
319+
for i, line := range lines {
320+
lines[i] = trimFunc(line)
321+
}
322+
return strings.Join(lines, "\n")
323+
}

analyzer_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ func TestAnalyzer_Analyze(t *testing.T) {
9393
config: "readme/readme.yml",
9494
assert: assert.Nil,
9595
},
96+
{
97+
desc: "star-block like header",
98+
filename: "starcomment/starcomment.go",
99+
config: "starcomment/starcomment.yml",
100+
assert: assert.Nil,
101+
},
96102
}
97103

98104
for _, test := range testCases {

testdata/starcomment/starcomment.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* A 2020
3+
* B
4+
*/
5+
6+
package constvalue

testdata/starcomment/starcomment.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
template: |-
2+
A {{ .YEAR }}
3+
B
4+
5+
vars:
6+
'YEAR': '2020'
7+

0 commit comments

Comments
 (0)