File tree 3 files changed +66
-1
lines changed 3 files changed +66
-1
lines changed Original file line number Diff line number Diff line change @@ -4,13 +4,13 @@ import (
4
4
"cmp"
5
5
"fmt"
6
6
"go/ast"
7
+ "go/token"
7
8
"reflect"
8
9
"slices"
9
10
"strconv"
10
11
"strings"
11
12
12
13
"github.com/fatih/structtag"
13
-
14
14
"golang.org/x/tools/go/analysis"
15
15
)
16
16
@@ -38,6 +38,13 @@ func NewAnalyzer(options ...Option) *analysis.Analyzer {
38
38
39
39
func Run (pass * analysis.Pass , options ... Option ) {
40
40
for _ , f := range pass .Files {
41
+ filename := getFilename (pass .Fset , f )
42
+ if ! strings .HasSuffix (filename , ".go" ) {
43
+ continue
44
+ }
45
+
46
+ println (filename )
47
+
41
48
h := & Helper {
42
49
style : DefaultStyle ,
43
50
align : true ,
@@ -378,3 +385,12 @@ func removeField(fields []*ast.Field, index int) []*ast.Field {
378
385
379
386
return append (fields [:index ], fields [index + 1 :]... )
380
387
}
388
+
389
+ func getFilename (fset * token.FileSet , file * ast.File ) string {
390
+ filename := fset .PositionFor (file .Pos (), true ).Filename
391
+ if ! strings .HasSuffix (filename , ".go" ) {
392
+ return fset .PositionFor (file .Pos (), false ).Filename
393
+ }
394
+
395
+ return filename
396
+ }
Original file line number Diff line number Diff line change @@ -57,6 +57,12 @@ func TestAnalyzer(t *testing.T) {
57
57
}
58
58
}
59
59
60
+ func TestAnalyzer_cgo (t * testing.T ) {
61
+ a := NewAnalyzer ()
62
+
63
+ analysistest .Run (t , analysistest .TestData (), a , "cgo" )
64
+ }
65
+
60
66
func Test_alignFormat (t * testing.T ) {
61
67
format := alignFormat (20 )
62
68
assert .Equal (t , "%-20s" , format )
Original file line number Diff line number Diff line change
1
+ package cgo
2
+
3
+ /*
4
+ #include <stdio.h>
5
+ #include <stdlib.h>
6
+
7
+ void myprint(char* s) {
8
+ printf("%d\n", s);
9
+ }
10
+ */
11
+ import "C"
12
+
13
+ import (
14
+ "unsafe"
15
+ )
16
+
17
+ func _ () {
18
+ cs := C .CString ("Hello from stdio\n " )
19
+ C .myprint (cs )
20
+ C .free (unsafe .Pointer (cs ))
21
+ }
22
+
23
+ type FooBar struct {
24
+ Foo int `json:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo"`
25
+ Bar string `json:"___bar___,omitempty" validate:"required"` // want `json:"___bar___,omitempty" validate:"required"`
26
+ FooFoo int8 `json:"foo_foo" validate:"required" yaml:"fooFoo"` // want `tag is not aligned, should be: json:"foo_foo"`
27
+ BarBar int `json:"bar_bar" validate:"required"` // want `tag is not aligned, should be: json:"bar_bar"`
28
+ FooBar struct {
29
+ Foo int `json:"foo" yaml:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo" yaml:"foo" validate:"required"`
30
+ Bar222 string `json:"bar222" validate:"required" yaml:"bar"` // want `tag is not aligned, should be: json:"bar222" validate:"required" yaml:"bar"`
31
+ } `json:"foo_bar" validate:"required"`
32
+ FooFooFoo struct {
33
+ BarBarBar struct {
34
+ BarBarBarBar string `json:"bar_bar_bar_bar" validate:"required"` // want `json:"bar_bar_bar_bar" validate:"required"`
35
+ BarBarBarFooBar string `json:"bar_bar_bar_foo_bar" yaml:"bar" validate:"required"` // want `tag is not aligned, should be: json:"bar_bar_bar_foo_bar" yaml:"bar" validate:"required"`
36
+ } `json:"bar_bar_bar" validate:"required"`
37
+ }
38
+ BarFooBarFoo struct {}
39
+ // test comment
40
+ // test commnet 2
41
+ BarFoo string `json:"bar_foo" validate:"required"` // want `tag is not aligned, should be: json:"bar_foo" validate:"required"`
42
+ BarFooBar string `json:"bar_foo_bar" validate:"required"`
43
+ }
You can’t perform that action at this time.
0 commit comments