diff --git a/tagalign.go b/tagalign.go index 8896a14..1d79b35 100644 --- a/tagalign.go +++ b/tagalign.go @@ -4,13 +4,13 @@ import ( "cmp" "fmt" "go/ast" + "go/token" "reflect" "slices" "strconv" "strings" "github.com/fatih/structtag" - "golang.org/x/tools/go/analysis" ) @@ -38,6 +38,13 @@ func NewAnalyzer(options ...Option) *analysis.Analyzer { func Run(pass *analysis.Pass, options ...Option) { for _, f := range pass.Files { + filename := getFilename(pass.Fset, f) + if !strings.HasSuffix(filename, ".go") { + continue + } + + println(filename) + h := &Helper{ style: DefaultStyle, align: true, @@ -378,3 +385,12 @@ func removeField(fields []*ast.Field, index int) []*ast.Field { return append(fields[:index], fields[index+1:]...) } + +func getFilename(fset *token.FileSet, file *ast.File) string { + filename := fset.PositionFor(file.Pos(), true).Filename + if !strings.HasSuffix(filename, ".go") { + return fset.PositionFor(file.Pos(), false).Filename + } + + return filename +} diff --git a/tagalign_test.go b/tagalign_test.go index 59afebf..68189cf 100644 --- a/tagalign_test.go +++ b/tagalign_test.go @@ -57,6 +57,12 @@ func TestAnalyzer(t *testing.T) { } } +func TestAnalyzer_cgo(t *testing.T) { + a := NewAnalyzer() + + analysistest.Run(t, analysistest.TestData(), a, "cgo") +} + func Test_alignFormat(t *testing.T) { format := alignFormat(20) assert.Equal(t, "%-20s", format) diff --git a/testdata/src/cgo/cgo.go b/testdata/src/cgo/cgo.go new file mode 100644 index 0000000..a929e26 --- /dev/null +++ b/testdata/src/cgo/cgo.go @@ -0,0 +1,43 @@ +package cgo + +/* + #include + #include + + void myprint(char* s) { + printf("%d\n", s); + } +*/ +import "C" + +import ( + "unsafe" +) + +func _() { + cs := C.CString("Hello from stdio\n") + C.myprint(cs) + C.free(unsafe.Pointer(cs)) +} + +type FooBar struct { + Foo int `json:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo"` + Bar string `json:"___bar___,omitempty" validate:"required"` // want `json:"___bar___,omitempty" validate:"required"` + FooFoo int8 `json:"foo_foo" validate:"required" yaml:"fooFoo"` // want `tag is not aligned, should be: json:"foo_foo"` + BarBar int `json:"bar_bar" validate:"required"` // want `tag is not aligned, should be: json:"bar_bar"` + FooBar struct { + Foo int `json:"foo" yaml:"foo" validate:"required"` // want `tag is not aligned, should be: json:"foo" yaml:"foo" validate:"required"` + Bar222 string `json:"bar222" validate:"required" yaml:"bar"` // want `tag is not aligned, should be: json:"bar222" validate:"required" yaml:"bar"` + } `json:"foo_bar" validate:"required"` + FooFooFoo struct { + BarBarBar struct { + BarBarBarBar string `json:"bar_bar_bar_bar" validate:"required"` // want `json:"bar_bar_bar_bar" validate:"required"` + 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"` + } `json:"bar_bar_bar" validate:"required"` + } + BarFooBarFoo struct{} + // test comment + // test commnet 2 + BarFoo string `json:"bar_foo" validate:"required"` // want `tag is not aligned, should be: json:"bar_foo" validate:"required"` + BarFooBar string `json:"bar_foo_bar" validate:"required"` +}