From f6220e1f305ef31371f6f9cd8b23d1fc87e50b52 Mon Sep 17 00:00:00 2001 From: neglect-yp Date: Fri, 7 Jan 2022 16:49:39 +0900 Subject: [PATCH] Add IgnorePosLine and deprecate IgnoreLine --- comment.go | 13 +++++++++++++ comment_test.go | 28 +++++++++++++++++++++++++++ testdata/Maps_IgnorePosLine/multi.go | 10 ++++++++++ testdata/Maps_IgnorePosLine/single.go | 7 +++++++ 4 files changed, 58 insertions(+) create mode 100644 testdata/Maps_IgnorePosLine/multi.go create mode 100644 testdata/Maps_IgnorePosLine/single.go diff --git a/comment.go b/comment.go index 79cb093..e515e72 100644 --- a/comment.go +++ b/comment.go @@ -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 @@ -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. diff --git a/comment_test.go b/comment_test.go index 0a64b07..f8867b0 100644 --- a/comment_test.go +++ b/comment_test.go @@ -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) + } + }) + } +} diff --git a/testdata/Maps_IgnorePosLine/multi.go b/testdata/Maps_IgnorePosLine/multi.go new file mode 100644 index 0000000..50d162e --- /dev/null +++ b/testdata/Maps_IgnorePosLine/multi.go @@ -0,0 +1,10 @@ +-- a.go -- +package a + +func A() {} +-- b.go -- +package a + +//lint:ignore test-check reason +func B_() { +} diff --git a/testdata/Maps_IgnorePosLine/single.go b/testdata/Maps_IgnorePosLine/single.go new file mode 100644 index 0000000..448e6ff --- /dev/null +++ b/testdata/Maps_IgnorePosLine/single.go @@ -0,0 +1,7 @@ +-- a.go -- +package a + +//lint:ignore test-check reason +func A_() {} + +func B() {}