From 49f9945468e6e356f19d625745ae9bf8c4c21759 Mon Sep 17 00:00:00 2001 From: Vyacheslav Pryimak Date: Fri, 15 Jul 2022 19:43:01 +0300 Subject: [PATCH] update to support go1.18 (#78) --- pkg/astutil/astutil.go | 9 +++++---- pkg/astutil/astutil_test.go | 20 ++++++++++++++++--- .../testdata_with_deprecated_build_tag.go | 13 ++++++++++++ 3 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 pkg/astutil/testdata/testdata_with_deprecated_build_tag.go diff --git a/pkg/astutil/astutil.go b/pkg/astutil/astutil.go index cc3bd51..0157bc4 100644 --- a/pkg/astutil/astutil.go +++ b/pkg/astutil/astutil.go @@ -10,7 +10,8 @@ import ( ) const ( - buildTagPrefix = "//go:build " + buildTagPrefix = "//go:build" + deprecatedBuildTagPrefix = "//+build" ) // PackageImports is map of imports with their package names @@ -95,14 +96,14 @@ func LoadPackageDependencies(dir, buildTag string) (PackageImports, error) { return result, nil } -// ParseBuildTag parse `// +build ...` on a first line of *ast.File +// ParseBuildTag parse `//+build ...` or `//go:build ` on a first line of *ast.File func ParseBuildTag(f *ast.File) string { for _, g := range f.Comments { for _, c := range g.List { - if !strings.HasPrefix(c.Text, buildTagPrefix) { + if !(strings.HasPrefix(c.Text, buildTagPrefix) || strings.HasPrefix(c.Text, deprecatedBuildTagPrefix)) { continue } - return strings.TrimSpace(strings.TrimPrefix(c.Text, buildTagPrefix)) + return strings.TrimSpace(strings.TrimPrefix(strings.TrimPrefix(c.Text, buildTagPrefix), deprecatedBuildTagPrefix)) } } diff --git a/pkg/astutil/astutil_test.go b/pkg/astutil/astutil_test.go index a52fcc7..b8ebb10 100644 --- a/pkg/astutil/astutil_test.go +++ b/pkg/astutil/astutil_test.go @@ -167,7 +167,8 @@ func main(){ func TestLoadPackageDeps(t *testing.T) { type args struct { - dir string + dir string + filename string } tests := []struct { @@ -179,7 +180,20 @@ func TestLoadPackageDeps(t *testing.T) { { name: "success", args: args{ - dir: "./testdata/", + dir: "./testdata/", + filename: "testdata.go", + }, + want: map[string]string{ + "fmt": "fmt", + "github.com/pkg/errors": "errors", + }, + wantErr: false, + }, + { + name: "success with deprecated build tag", + args: args{ + dir: "./testdata/", + filename: "testdata_with_deprecated_build_tag.go", }, want: map[string]string{ "fmt": "fmt", @@ -193,7 +207,7 @@ func TestLoadPackageDeps(t *testing.T) { t.Run(tt.name, func(t *testing.T) { f, err := parser.ParseFile( token.NewFileSet(), - fmt.Sprintf("%s/%s", tt.args.dir, "testdata.go"), + fmt.Sprintf("%s/%s", tt.args.dir, tt.args.filename), nil, parser.ParseComments, ) diff --git a/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go b/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go new file mode 100644 index 0000000..e02157d --- /dev/null +++ b/pkg/astutil/testdata/testdata_with_deprecated_build_tag.go @@ -0,0 +1,13 @@ +//+build test + +package testdata + +import ( + "fmt" + + "github.com/pkg/errors" +) + +func main() { + fmt.Printf("%s", errors.New("some error here")) +}