Skip to content

Commit efd5eb3

Browse files
committed
tests: add cgo case
1 parent 36263ea commit efd5eb3

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

tagalign.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import (
44
"cmp"
55
"fmt"
66
"go/ast"
7+
"go/token"
78
"reflect"
89
"slices"
910
"strconv"
1011
"strings"
1112

1213
"github.com/fatih/structtag"
13-
1414
"golang.org/x/tools/go/analysis"
1515
)
1616

@@ -38,6 +38,13 @@ func NewAnalyzer(options ...Option) *analysis.Analyzer {
3838

3939
func Run(pass *analysis.Pass, options ...Option) {
4040
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+
4148
h := &Helper{
4249
style: DefaultStyle,
4350
align: true,
@@ -378,3 +385,12 @@ func removeField(fields []*ast.Field, index int) []*ast.Field {
378385

379386
return append(fields[:index], fields[index+1:]...)
380387
}
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+
}

tagalign_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func TestAnalyzer(t *testing.T) {
5757
}
5858
}
5959

60+
func TestAnalyzer_cgo(t *testing.T) {
61+
a := NewAnalyzer()
62+
63+
analysistest.Run(t, analysistest.TestData(), a, "cgo")
64+
}
65+
6066
func Test_alignFormat(t *testing.T) {
6167
format := alignFormat(20)
6268
assert.Equal(t, "%-20s", format)

testdata/src/cgo/cgo.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
}

0 commit comments

Comments
 (0)