Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	pkg/astutil/testdata/testdata_with_deprecated_build_tag.go
  • Loading branch information
incu6us committed Jul 15, 2022
2 parents b1fd0ae + 49f9945 commit d4b0fb0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
9 changes: 5 additions & 4 deletions pkg/astutil/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

const (
buildTagPrefix = "//go:build "
buildTagPrefix = "//go:build"
deprecatedBuildTagPrefix = "//+build"
)

// PackageImports is map of imports with their package names
Expand Down Expand Up @@ -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))
}
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/astutil/astutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ func main(){

func TestLoadPackageDeps(t *testing.T) {
type args struct {
dir string
dir string
filename string
}

tests := []struct {
Expand All @@ -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",
Expand All @@ -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,
)
Expand Down
3 changes: 1 addition & 2 deletions pkg/astutil/testdata/testdata_with_deprecated_build_tag.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build test
// +build test
//+build test

package testdata

Expand Down

0 comments on commit d4b0fb0

Please sign in to comment.