Skip to content

Commit aff4c05

Browse files
authored
Merge pull request #107 from 3vilhamster/fix-lint
fix: Create an alias around deprecated ast.Package
2 parents 024620b + 58c34c8 commit aff4c05

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

generator/generator.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ type methodsList map[string]Method
139139
type processInput struct {
140140
fileSet *token.FileSet
141141
currentPackage *packages.Package
142-
astPackage *ast.Package
142+
astPackage *pkg.Package
143143
targetName string
144144
genericParams genericParams
145145
}
@@ -435,7 +435,7 @@ func findSourcePackage(ident *ast.Ident, imports []*ast.ImportSpec) string {
435435
return ""
436436
}
437437

438-
func iterateFiles(p *ast.Package, name string) (selectedType *ast.TypeSpec, imports []*ast.ImportSpec, types []*ast.TypeSpec) {
438+
func iterateFiles(p *pkg.Package, name string) (selectedType *ast.TypeSpec, imports []*ast.ImportSpec, types []*ast.TypeSpec) {
439439
for _, f := range p.Files {
440440
if f != nil {
441441
for _, ts := range typeSpecs(f) {
@@ -452,7 +452,7 @@ func iterateFiles(p *ast.Package, name string) (selectedType *ast.TypeSpec, impo
452452
}
453453

454454
func typeSpecs(f *ast.File) []*ast.TypeSpec {
455-
result := []*ast.TypeSpec{}
455+
var result []*ast.TypeSpec
456456

457457
for _, decl := range f.Decls {
458458
if gd, ok := decl.(*ast.GenDecl); ok && gd.Tok == token.TYPE {

generator/generator_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@ import (
99
"text/template"
1010
"time"
1111

12-
minimock "github.com/gojuno/minimock/v3"
12+
"github.com/gojuno/minimock/v3"
1313
"github.com/pkg/errors"
1414
"github.com/stretchr/testify/assert"
1515
"github.com/stretchr/testify/require"
1616
"golang.org/x/tools/go/packages"
17+
18+
"github.com/hexdigest/gowrap/pkg"
1719
)
1820

1921
func Test_unquote(t *testing.T) {
@@ -478,7 +480,7 @@ func Test_findTarget(t *testing.T) {
478480
name: "not found",
479481
args: args{
480482
input: processInput{
481-
astPackage: &ast.Package{},
483+
astPackage: &pkg.Package{},
482484
},
483485
},
484486
wantErr: true,
@@ -490,7 +492,7 @@ func Test_findTarget(t *testing.T) {
490492
name: "found",
491493
args: args{
492494
input: processInput{
493-
astPackage: &ast.Package{Files: map[string]*ast.File{
495+
astPackage: &pkg.Package{Files: map[string]*ast.File{
494496
"file.go": {
495497
Decls: []ast.Decl{&ast.GenDecl{Tok: token.TYPE, Specs: []ast.Spec{&ast.TypeSpec{
496498
Name: &ast.Ident{Name: "Interface"},
@@ -506,13 +508,14 @@ func Test_findTarget(t *testing.T) {
506508
name: "found interface alias",
507509
args: args{
508510
input: processInput{
509-
astPackage: &ast.Package{
511+
astPackage: &pkg.Package{
510512
Files: map[string]*ast.File{
511513
"file.go": {
512514
Decls: []ast.Decl{&ast.GenDecl{Tok: token.TYPE, Specs: []ast.Spec{&ast.TypeSpec{
513515
Name: &ast.Ident{Name: "InterfaceAlias"},
514516
Type: &ast.Ident{
515517
Name: "Interface",
518+
//nolint:staticcheck // SA1019, this is an internal object that is still used by ast library.
516519
Obj: &ast.Object{
517520
Decl: &ast.TypeSpec{
518521
Type: &ast.InterfaceType{},
@@ -531,7 +534,7 @@ func Test_findTarget(t *testing.T) {
531534
name: "found interface alias on exported type",
532535
args: args{
533536
input: processInput{
534-
astPackage: &ast.Package{
537+
astPackage: &pkg.Package{
535538
Files: map[string]*ast.File{
536539
"file.go": {
537540
Decls: []ast.Decl{&ast.GenDecl{Tok: token.TYPE, Specs: []ast.Spec{&ast.TypeSpec{
@@ -583,7 +586,7 @@ func Test_findTarget(t *testing.T) {
583586
name: "found interface alias on exported type with named package",
584587
args: args{
585588
input: processInput{
586-
astPackage: &ast.Package{
589+
astPackage: &pkg.Package{
587590
Files: map[string]*ast.File{
588591
"file.go": {
589592
Decls: []ast.Decl{&ast.GenDecl{Tok: token.TYPE, Specs: []ast.Spec{&ast.TypeSpec{

pkg/package.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ import (
1212

1313
var errPackageNotFound = errors.New("package not found")
1414

15+
type Package struct {
16+
Name string
17+
Files map[string]*ast.File
18+
}
19+
1520
// Load loads package by its import path
1621
func Load(path string) (*packages.Package, error) {
1722
cfg := &packages.Config{Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedDeps}
@@ -32,7 +37,7 @@ func Load(path string) (*packages.Package, error) {
3237
}
3338

3439
// AST returns package's abstract syntax tree
35-
func AST(fs *token.FileSet, p *packages.Package) (*ast.Package, error) {
40+
func AST(fs *token.FileSet, p *packages.Package) (*Package, error) {
3641
dir := Dir(p)
3742

3843
pkgs, err := parser.ParseDir(fs, dir, nil, parser.DeclarationErrors|parser.ParseComments)
@@ -41,10 +46,13 @@ func AST(fs *token.FileSet, p *packages.Package) (*ast.Package, error) {
4146
}
4247

4348
if ap, ok := pkgs[p.Name]; ok {
44-
return ap, nil
49+
return &Package{
50+
Name: p.Name,
51+
Files: ap.Files,
52+
}, nil
4553
}
4654

47-
return &ast.Package{Name: p.Name}, nil
55+
return &Package{Name: p.Name}, nil
4856
}
4957

5058
// Dir returns absolute path of the package in a filesystem

0 commit comments

Comments
 (0)