Skip to content

Commit fa6ac64

Browse files
committed
Add noinline option for checking all comments except inlined.
1 parent c4546a8 commit fa6ac64

File tree

7 files changed

+110
-8
lines changed

7 files changed

+110
-8
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ defaults are used:
3636
# Which comments to check:
3737
# declarations - for top level declaration comments (default);
3838
# toplevel - for top level comments;
39+
# noinline - for all except inline comments;
3940
# all - for all comments.
4041
scope: declarations
4142

getters.go

+31
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ func (pf *parsedFile) getComments(scope Scope, exclude []*regexp.Regexp) []comme
6666
case AllScope:
6767
// All comments
6868
comments = pf.getAllComments(exclude)
69+
case NoInlineScope:
70+
// All except inline comments
71+
comments = pf.getNoInline(exclude)
6972
case TopLevelScope:
7073
// All top level comments and comments from the inside
7174
// of top level blocks
@@ -173,6 +176,34 @@ func (pf *parsedFile) getDeclarationComments(exclude []*regexp.Regexp) []comment
173176
return comments
174177
}
175178

179+
// getNoInline gets all except inline comments.
180+
func (pf *parsedFile) getNoInline(exclude []*regexp.Regexp) []comment {
181+
var comments []comment //nolint:prealloc
182+
for _, c := range pf.file.Comments {
183+
if c == nil || len(c.List) == 0 {
184+
continue
185+
}
186+
firstLine := pf.fset.Position(c.Pos()).Line
187+
lastLine := pf.fset.Position(c.End()).Line
188+
189+
c := comment{
190+
lines: pf.lines[firstLine-1 : lastLine],
191+
start: pf.fset.Position(c.List[0].Slash),
192+
text: getText(c, exclude),
193+
}
194+
195+
// Skip inline
196+
if len(c.lines) == 1 {
197+
before := c.lines[0][:c.start.Column-1]
198+
if len(strings.TrimSpace(before)) > 0 {
199+
continue
200+
}
201+
}
202+
comments = append(comments, c)
203+
}
204+
return comments
205+
}
206+
176207
// getAllComments gets every single comment from the file.
177208
func (pf *parsedFile) getAllComments(exclude []*regexp.Regexp) []comment {
178209
var comments []comment //nolint:prealloc

getters_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,15 @@ func TestGetComments(t *testing.T) {
3838
scope: TopLevelScope,
3939
contains: []string{"[DECL]", "[TOP]"},
4040
},
41+
{
42+
name: "scope: noinline",
43+
scope: NoInlineScope,
44+
contains: []string{"[DECL]", "[TOP]", "[ALL]"},
45+
},
4146
{
4247
name: "scope: all",
4348
scope: AllScope,
44-
contains: []string{"[DECL]", "[TOP]", "[ALL]"},
49+
contains: []string{"[DECL]", "[TOP]", "[ALL]", "[INLINE]"},
4550
},
4651
}
4752

godot_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,23 @@ func TestRun(t *testing.T) {
9595
"[PERIOD_TOP]", "[CAPITAL_TOP]",
9696
},
9797
},
98+
{
99+
name: "scope: noinline",
100+
scope: NoInlineScope,
101+
contains: []string{
102+
"[PERIOD_DECL]", "[CAPITAL_DECL]",
103+
"[PERIOD_TOP]", "[CAPITAL_TOP]",
104+
"[PERIOD_ALL]", "[CAPITAL_ALL]",
105+
},
106+
},
98107
{
99108
name: "scope: all",
100109
scope: AllScope,
101110
contains: []string{
102111
"[PERIOD_DECL]", "[CAPITAL_DECL]",
103112
"[PERIOD_TOP]", "[CAPITAL_TOP]",
104113
"[PERIOD_ALL]", "[CAPITAL_ALL]",
114+
"[PERIOD_INLINE]", "[CAPITAL_INLINE]",
105115
},
106116
},
107117
}
@@ -254,13 +264,36 @@ func TestFix(t *testing.T) {
254264
assertEqualContent(t, expected, string(fixed))
255265
})
256266

267+
t.Run("scope: noinline", func(t *testing.T) {
268+
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
269+
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
270+
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
271+
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
272+
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
273+
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")
274+
275+
fixed, err := Fix(testFile, file, fset, Settings{
276+
Scope: NoInlineScope,
277+
Exclude: testExclude,
278+
Period: true,
279+
Capital: true,
280+
})
281+
if err != nil {
282+
t.Fatalf("Unexpected error: %v", err)
283+
}
284+
285+
assertEqualContent(t, expected, string(fixed))
286+
})
287+
257288
t.Run("scope: all", func(t *testing.T) {
258289
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
259290
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
260291
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
292+
expected = strings.ReplaceAll(expected, "[PERIOD_INLINE]", "[PERIOD_INLINE].")
261293
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
262294
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
263295
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")
296+
expected = strings.ReplaceAll(expected, "non-capital-inline", "Non-capital-inline")
264297

265298
fixed, err := Fix(testFile, file, fset, Settings{
266299
Scope: AllScope,
@@ -417,16 +450,46 @@ func TestReplace(t *testing.T) {
417450
assertEqualContent(t, expected, string(fixed))
418451
})
419452

453+
t.Run("scope: noinline", func(t *testing.T) {
454+
defer func() {
455+
os.WriteFile(testFile, content, mode)
456+
}()
457+
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
458+
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
459+
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
460+
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
461+
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
462+
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")
463+
464+
err := Replace(testFile, file, fset, Settings{
465+
Scope: NoInlineScope,
466+
Exclude: testExclude,
467+
Period: true,
468+
Capital: true,
469+
})
470+
if err != nil {
471+
t.Fatalf("Unexpected error: %v", err)
472+
}
473+
fixed, err := os.ReadFile(testFile)
474+
if err != nil {
475+
t.Fatalf("Failed to read fixed file %s: %v", testFile, err)
476+
}
477+
478+
assertEqualContent(t, expected, string(fixed))
479+
})
480+
420481
t.Run("scope: all", func(t *testing.T) {
421482
defer func() {
422483
os.WriteFile(testFile, content, mode)
423484
}()
424485
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
425486
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
426487
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
488+
expected = strings.ReplaceAll(expected, "[PERIOD_INLINE]", "[PERIOD_INLINE].")
427489
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
428490
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
429491
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")
492+
expected = strings.ReplaceAll(expected, "non-capital-inline", "Non-capital-inline")
430493

431494
err := Replace(testFile, file, fset, Settings{
432495
Scope: AllScope,

settings.go

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const (
2424
DeclScope Scope = "declarations"
2525
// TopLevelScope is for all top level comments.
2626
TopLevelScope Scope = "toplevel"
27+
// NoInlineScope is for all except inline comments.
28+
NoInlineScope Scope = "noinline"
2729
// AllScope is for all comments.
2830
AllScope Scope = "all"
2931
)

testdata/check/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
const (
2121
one = 1
2222

23-
two = 2
23+
two = 2 // Inline comment [PERIOD_INLINE]
2424

2525
)
2626

@@ -105,7 +105,7 @@ func Sum(a, b int) int {
105105
a++
106106
b++
107107

108-
return a + b // Inline comment [PERIOD_ALL]
108+
return a + b // Inline comment [PERIOD_INLINE]
109109
}
110110

111111
// Declaration multiline comment
@@ -147,7 +147,7 @@ func inside() {
147147
type thing struct {
148148
field string
149149
}
150-
t := thing{} // Inline comment [PERIOD_ALL]
150+
t := thing{} // Inline comment [PERIOD_INLINE]
151151
println(t)
152152
// @Comment without a period excluded by regexp pattern [PASS]
153153
}
@@ -158,7 +158,7 @@ func nonCapital() int {
158158
x := 10
159159

160160
// non-capital-all [CAPITAL_ALL].
161-
return x // non-capital-all [CAPITAL_ALL].
161+
return x // non-capital-inline [CAPITAL_INLINE].
162162
}
163163

164164
// ExamplePrintln is a function that you would normally see in tests.

testdata/get/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import "fmt"
88
const (
99
one = 1
1010

11-
two = 2
11+
two = 2 // Inline comment [TOP]
1212

1313
)
1414

@@ -45,7 +45,7 @@ type Thing struct {
4545
}
4646

4747
// Example is an example function. Top level function declaration comment [DECL].
48-
func Example() { // top level inline comment [ALL]
48+
func Example() { // top level inline comment [INLINE]
4949
// Regular comment [ALL]
5050

5151
// Declaration comment [ALL]
@@ -54,5 +54,5 @@ func Example() { // top level inline comment [ALL]
5454
fmt.Println("hello, world")
5555
}
5656

57-
prn() // Inline comment [ALL]
57+
prn() // Inline comment [INLINE]
5858
}

0 commit comments

Comments
 (0)