Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IgnorePosLine and deprecate IgnoreLine #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (maps Maps) CommentsByPosLine(fset *token.FileSet, pos token.Pos) []*ast.Co
return nil
}

// Deprecated: This function does not work with multiple files.
// IgnoreLine checks either specified lineof AST node is ignored by the check.
// It follows staticcheck style as the below.
// //lint:ignore Check1[,Check2,...,CheckN] reason
Expand All @@ -121,6 +122,18 @@ func (maps Maps) IgnoreLine(fset *token.FileSet, line int, check string) bool {
return false
}

// IgnorePosLine checks either specified lineof AST node is ignored by the check.
// It follows staticcheck style as the below.
// //lint:ignore Check1[,Check2,...,CheckN] reason
func (maps Maps) IgnorePosLine(fset *token.FileSet, pos token.Pos, check string) bool {
for _, cg := range maps.CommentsByPosLine(fset, pos) {
if hasIgnoreCheck(cg, check) {
return true
}
}
return false
}

// hasIgnoreCheck returns true if the provided CommentGroup starts with a comment
// of the form "//lint:ignore Check1[,Check2,...,CheckN] reason" and one of the
// checks matches the provided check.
Expand Down
28 changes: 28 additions & 0 deletions comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,31 @@ func TestMaps_IgnoreLine(t *testing.T) {
})
}
}

func TestMaps_IgnorePosLine(t *testing.T) {
t.Parallel()

cases := map[string]struct {
path string
check string
want bool
}{
"single": {"testdata/Maps_IgnorePosLine/single.go", "test-check", true},
"multi": {"testdata/Maps_IgnorePosLine/multi.go", "test-check", true},
}

for n, tt := range cases {
tt := tt
t.Run(n, func(t *testing.T) {
t.Parallel()
fset := token.NewFileSet()
ms := maps(t, fset, tt.path)
p := pos(t, fset, tt.path)

got := ms.IgnorePosLine(fset, p, tt.check)
if tt.want != got {
t.Errorf("want %v, got %v", tt.want, got)
}
})
}
}
10 changes: 10 additions & 0 deletions testdata/Maps_IgnorePosLine/multi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- a.go --
package a

func A() {}
-- b.go --
package a

//lint:ignore test-check reason
func B_() {
}
7 changes: 7 additions & 0 deletions testdata/Maps_IgnorePosLine/single.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- a.go --
package a

//lint:ignore test-check reason
func A_() {}

func B() {}