Skip to content

Commit

Permalink
Add noinline option for checking all comments except inlined.
Browse files Browse the repository at this point in the history
  • Loading branch information
tetafro committed Feb 15, 2025
1 parent c4546a8 commit fa6ac64
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ defaults are used:
# Which comments to check:
# declarations - for top level declaration comments (default);
# toplevel - for top level comments;
# noinline - for all except inline comments;
# all - for all comments.
scope: declarations

Expand Down
31 changes: 31 additions & 0 deletions getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (pf *parsedFile) getComments(scope Scope, exclude []*regexp.Regexp) []comme
case AllScope:
// All comments
comments = pf.getAllComments(exclude)
case NoInlineScope:
// All except inline comments
comments = pf.getNoInline(exclude)
case TopLevelScope:
// All top level comments and comments from the inside
// of top level blocks
Expand Down Expand Up @@ -173,6 +176,34 @@ func (pf *parsedFile) getDeclarationComments(exclude []*regexp.Regexp) []comment
return comments
}

// getNoInline gets all except inline comments.
func (pf *parsedFile) getNoInline(exclude []*regexp.Regexp) []comment {
var comments []comment //nolint:prealloc
for _, c := range pf.file.Comments {
if c == nil || len(c.List) == 0 {
continue
}
firstLine := pf.fset.Position(c.Pos()).Line
lastLine := pf.fset.Position(c.End()).Line

c := comment{
lines: pf.lines[firstLine-1 : lastLine],
start: pf.fset.Position(c.List[0].Slash),
text: getText(c, exclude),
}

// Skip inline
if len(c.lines) == 1 {
before := c.lines[0][:c.start.Column-1]
if len(strings.TrimSpace(before)) > 0 {
continue
}
}
comments = append(comments, c)
}
return comments
}

// getAllComments gets every single comment from the file.
func (pf *parsedFile) getAllComments(exclude []*regexp.Regexp) []comment {
var comments []comment //nolint:prealloc
Expand Down
7 changes: 6 additions & 1 deletion getters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ func TestGetComments(t *testing.T) {
scope: TopLevelScope,
contains: []string{"[DECL]", "[TOP]"},
},
{
name: "scope: noinline",
scope: NoInlineScope,
contains: []string{"[DECL]", "[TOP]", "[ALL]"},
},
{
name: "scope: all",
scope: AllScope,
contains: []string{"[DECL]", "[TOP]", "[ALL]"},
contains: []string{"[DECL]", "[TOP]", "[ALL]", "[INLINE]"},
},
}

Expand Down
63 changes: 63 additions & 0 deletions godot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,23 @@ func TestRun(t *testing.T) {
"[PERIOD_TOP]", "[CAPITAL_TOP]",
},
},
{
name: "scope: noinline",
scope: NoInlineScope,
contains: []string{
"[PERIOD_DECL]", "[CAPITAL_DECL]",
"[PERIOD_TOP]", "[CAPITAL_TOP]",
"[PERIOD_ALL]", "[CAPITAL_ALL]",
},
},
{
name: "scope: all",
scope: AllScope,
contains: []string{
"[PERIOD_DECL]", "[CAPITAL_DECL]",
"[PERIOD_TOP]", "[CAPITAL_TOP]",
"[PERIOD_ALL]", "[CAPITAL_ALL]",
"[PERIOD_INLINE]", "[CAPITAL_INLINE]",
},
},
}
Expand Down Expand Up @@ -254,13 +264,36 @@ func TestFix(t *testing.T) {
assertEqualContent(t, expected, string(fixed))
})

t.Run("scope: noinline", func(t *testing.T) {
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")

fixed, err := Fix(testFile, file, fset, Settings{
Scope: NoInlineScope,
Exclude: testExclude,
Period: true,
Capital: true,
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

assertEqualContent(t, expected, string(fixed))
})

t.Run("scope: all", func(t *testing.T) {
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
expected = strings.ReplaceAll(expected, "[PERIOD_INLINE]", "[PERIOD_INLINE].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")
expected = strings.ReplaceAll(expected, "non-capital-inline", "Non-capital-inline")

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

t.Run("scope: noinline", func(t *testing.T) {
defer func() {
os.WriteFile(testFile, content, mode)
}()
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")

err := Replace(testFile, file, fset, Settings{
Scope: NoInlineScope,
Exclude: testExclude,
Period: true,
Capital: true,
})
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
fixed, err := os.ReadFile(testFile)
if err != nil {
t.Fatalf("Failed to read fixed file %s: %v", testFile, err)
}

assertEqualContent(t, expected, string(fixed))
})

t.Run("scope: all", func(t *testing.T) {
defer func() {
os.WriteFile(testFile, content, mode)
}()
expected := strings.ReplaceAll(string(content), "[PERIOD_DECL]", "[PERIOD_DECL].")
expected = strings.ReplaceAll(expected, "[PERIOD_TOP]", "[PERIOD_TOP].")
expected = strings.ReplaceAll(expected, "[PERIOD_ALL]", "[PERIOD_ALL].")
expected = strings.ReplaceAll(expected, "[PERIOD_INLINE]", "[PERIOD_INLINE].")
expected = strings.ReplaceAll(expected, "non-capital-decl", "Non-capital-decl")
expected = strings.ReplaceAll(expected, "non-capital-top", "Non-capital-top")
expected = strings.ReplaceAll(expected, "non-capital-all", "Non-capital-all")
expected = strings.ReplaceAll(expected, "non-capital-inline", "Non-capital-inline")

err := Replace(testFile, file, fset, Settings{
Scope: AllScope,
Expand Down
2 changes: 2 additions & 0 deletions settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
DeclScope Scope = "declarations"
// TopLevelScope is for all top level comments.
TopLevelScope Scope = "toplevel"
// NoInlineScope is for all except inline comments.
NoInlineScope Scope = "noinline"
// AllScope is for all comments.
AllScope Scope = "all"
)
8 changes: 4 additions & 4 deletions testdata/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
const (
one = 1

two = 2
two = 2 // Inline comment [PERIOD_INLINE]

)

Expand Down Expand Up @@ -105,7 +105,7 @@ func Sum(a, b int) int {
a++
b++

return a + b // Inline comment [PERIOD_ALL]
return a + b // Inline comment [PERIOD_INLINE]
}

// Declaration multiline comment
Expand Down Expand Up @@ -147,7 +147,7 @@ func inside() {
type thing struct {
field string
}
t := thing{} // Inline comment [PERIOD_ALL]
t := thing{} // Inline comment [PERIOD_INLINE]
println(t)
// @Comment without a period excluded by regexp pattern [PASS]
}
Expand All @@ -158,7 +158,7 @@ func nonCapital() int {
x := 10

// non-capital-all [CAPITAL_ALL].
return x // non-capital-all [CAPITAL_ALL].
return x // non-capital-inline [CAPITAL_INLINE].
}

// ExamplePrintln is a function that you would normally see in tests.
Expand Down
6 changes: 3 additions & 3 deletions testdata/get/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "fmt"
const (
one = 1

two = 2
two = 2 // Inline comment [TOP]

)

Expand Down Expand Up @@ -45,7 +45,7 @@ type Thing struct {
}

// Example is an example function. Top level function declaration comment [DECL].
func Example() { // top level inline comment [ALL]
func Example() { // top level inline comment [INLINE]
// Regular comment [ALL]

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

prn() // Inline comment [ALL]
prn() // Inline comment [INLINE]
}

0 comments on commit fa6ac64

Please sign in to comment.