Skip to content

Commit ad105f2

Browse files
authored
Merge pull request #9 from gostaticanalysis/fix-CommentsByLine
Add CommentsByPosLine
2 parents d7dc70a + 9c70244 commit ad105f2

File tree

7 files changed

+180
-4
lines changed

7 files changed

+180
-4
lines changed

comment.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ func (maps Maps) IgnorePos(pos token.Pos, check string) bool {
7474
return false
7575
}
7676

77+
// Deprecated: This function does not work with multiple files.
78+
// CommentsByPosLine can be used instead of CommentsByLine.
79+
//
7780
// CommentsByLine returns correspond a CommentGroup slice to specified line.
7881
func (maps Maps) CommentsByLine(fset *token.FileSet, line int) []*ast.CommentGroup {
7982
for i := range maps {
@@ -87,6 +90,25 @@ func (maps Maps) CommentsByLine(fset *token.FileSet, line int) []*ast.CommentGro
8790
return nil
8891
}
8992

93+
// CommentsByPosLine returns correspond a CommentGroup slice to specified line.
94+
func (maps Maps) CommentsByPosLine(fset *token.FileSet, pos token.Pos) []*ast.CommentGroup {
95+
f1 := fset.File(pos)
96+
for i := range maps {
97+
for n, cgs := range maps[i] {
98+
f2 := fset.File(n.Pos())
99+
if f1 != f2 {
100+
// different file
101+
continue
102+
}
103+
104+
if f1.Line(pos) == f2.Line(n.Pos()) {
105+
return cgs
106+
}
107+
}
108+
}
109+
return nil
110+
}
111+
90112
// IgnoreLine checks either specified lineof AST node is ignored by the check.
91113
// It follows staticcheck style as the below.
92114
// //lint:ignore Check1[,Check2,...,CheckN] reason

comment_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package comment_test
2+
3+
import (
4+
"go/token"
5+
"strings"
6+
"testing"
7+
8+
"github.com/google/go-cmp/cmp"
9+
)
10+
11+
func TestMaps_CommentsByPosLine(t *testing.T) {
12+
t.Parallel()
13+
14+
cases := map[string]struct {
15+
path string
16+
want []string
17+
}{
18+
"single": {"testdata/Maps_CommentsByPosLine/single.go", []string{"a"}},
19+
"multi": {"testdata/Maps_CommentsByPosLine/multi.go", []string{"b"}},
20+
}
21+
22+
for n, tt := range cases {
23+
tt := tt
24+
t.Run(n, func(t *testing.T) {
25+
t.Parallel()
26+
fset := token.NewFileSet()
27+
ms := maps(t, fset, tt.path)
28+
p := pos(t, fset, tt.path)
29+
cgs := ms.CommentsByPosLine(fset, p)
30+
got := make([]string, len(cgs))
31+
for i := range cgs {
32+
got[i] = strings.TrimSpace(cgs[i].Text())
33+
}
34+
35+
if diff := cmp.Diff(tt.want, got); diff != "" {
36+
t.Errorf(diff)
37+
}
38+
})
39+
}
40+
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/gostaticanalysis/comment
22

33
go 1.12
44

5-
require golang.org/x/tools v0.0.0-20190307163923-6a08e3108db3
5+
require (
6+
github.com/google/go-cmp v0.5.1
7+
golang.org/x/tools v0.0.0-20200820010801-b793a1359eac
8+
)

go.sum

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1-
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
2-
golang.org/x/tools v0.0.0-20190307163923-6a08e3108db3 h1:2oZsfYnKfYzL4I57uYiRFsUf0bqlLkiuw8nnj3+voUA=
3-
golang.org/x/tools v0.0.0-20190307163923-6a08e3108db3/go.mod h1:25r3+/G6/xytQM8iWZKq3Hn0kr0rgFKPUNVEL/dr3z4=
1+
github.com/google/go-cmp v0.5.1 h1:JFrFEBb2xKufg6XkJsJr+WbKb4FQlURi5RUcBveYu9k=
2+
github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
3+
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
4+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5+
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
6+
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
7+
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
8+
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
9+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
10+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
11+
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
12+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
13+
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
14+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
15+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
16+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
17+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
18+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
19+
golang.org/x/tools v0.0.0-20200820010801-b793a1359eac h1:DugppSxw0LSF8lcjaODPJZoDzq0ElTGskTst3ZaBkHI=
20+
golang.org/x/tools v0.0.0-20200820010801-b793a1359eac/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
21+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
22+
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
23+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
24+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

helper_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package comment_test
2+
3+
import (
4+
"bytes"
5+
"go/ast"
6+
"go/parser"
7+
"go/token"
8+
"testing"
9+
10+
"github.com/gostaticanalysis/comment"
11+
"golang.org/x/tools/txtar"
12+
)
13+
14+
func parse(t *testing.T, fset *token.FileSet, path string) []*ast.File {
15+
t.Helper()
16+
ar, err := txtar.ParseFile(path)
17+
if err != nil {
18+
t.Fatal("unexpected error:", err)
19+
}
20+
21+
files := make([]*ast.File, len(ar.Files))
22+
for i := range ar.Files {
23+
n, d := ar.Files[i].Name, ar.Files[i].Data
24+
f, err := parser.ParseFile(fset, n, d, parser.ParseComments)
25+
if err != nil {
26+
t.Fatal("unexpected error:", err)
27+
}
28+
files[i] = f
29+
}
30+
31+
return files
32+
}
33+
34+
func maps(t *testing.T, fset *token.FileSet, path string) comment.Maps {
35+
t.Helper()
36+
files := parse(t, fset, path)
37+
return comment.New(fset, files)
38+
}
39+
40+
// pos find position of `_` in source codes as a token.Pos.
41+
func pos(t *testing.T, fset *token.FileSet, path string) token.Pos {
42+
t.Helper()
43+
44+
ar, err := txtar.ParseFile(path)
45+
if err != nil {
46+
t.Fatal("unexpected error:", err)
47+
}
48+
49+
for i := range ar.Files {
50+
n, d := ar.Files[i].Name, ar.Files[i].Data
51+
index := bytes.Index(d, []byte("_"))
52+
if index == -1 {
53+
continue
54+
}
55+
56+
var pos token.Pos
57+
fset.Iterate(func(f *token.File) bool {
58+
if n == f.Name() {
59+
pos = f.Pos(index)
60+
return false
61+
}
62+
return true
63+
})
64+
65+
if pos != token.NoPos {
66+
return pos
67+
}
68+
}
69+
70+
return token.NoPos
71+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- a.go --
2+
package a
3+
4+
// a
5+
func A() {}
6+
-- b.go --
7+
package a
8+
9+
// b
10+
func B_() {
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- a.go --
2+
package a
3+
4+
// a
5+
func A_() {}
6+
7+
// b
8+
func B() {}

0 commit comments

Comments
 (0)