Skip to content

Commit

Permalink
remove hardcoded stdlib packages
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoyang Tan <tanx@uber.com>
  • Loading branch information
xytan0056 committed Feb 16, 2023
1 parent 7f0d0bc commit c218bc6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 176 deletions.
2 changes: 1 addition & 1 deletion pkg/section/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Parse(data []string) (SectionList, error) {
if s == "default" {
list = append(list, Default{})
} else if s == "standard" {
list = append(list, Standard{})
list = append(list, NewStandard())
} else if s == "newline" {
list = append(list, NewLine{})
} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (list SectionList) String() []string {
}

func DefaultSections() SectionList {
return SectionList{Standard{}, Default{}}
return SectionList{NewStandard(), Default{}}
}

func DefaultSectionSeparators() SectionList {
Expand Down
25 changes: 18 additions & 7 deletions pkg/section/standard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,30 @@ package section
import (
"github.com/daixiang0/gci/pkg/parse"
"github.com/daixiang0/gci/pkg/specificity"
"golang.org/x/tools/go/packages"
)

const StandardType = "standard"

type Standard struct{}
type Standard struct {
standardPackages map[string]struct{}
}

func NewStandard() Standard {
pkgs, err := packages.Load(nil, "std")
if err != nil {
panic(err)
}

var standardPackages = make(map[string]struct{})
for _, p := range pkgs {
standardPackages[p.PkgPath] = struct{}{}
}
return Standard{standardPackages: standardPackages}
}

func (s Standard) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
if isStandard(spec.Path) {
if _, ok := s.standardPackages[spec.Path]; ok {
return specificity.StandardMatch{}
}
return specificity.MisMatch{}
Expand All @@ -23,8 +39,3 @@ func (s Standard) String() string {
func (s Standard) Type() string {
return StandardType
}

func isStandard(pkg string) bool {
_, ok := standardPackages[pkg]
return ok
}
160 changes: 0 additions & 160 deletions pkg/section/standard_list.go

This file was deleted.

14 changes: 7 additions & 7 deletions pkg/section/standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

func TestStandardPackageSpecificity(t *testing.T) {
testCases := []specificityTestData{
{"context", Standard{}, specificity.StandardMatch{}},
{"contexts", Standard{}, specificity.MisMatch{}},
{"crypto", Standard{}, specificity.StandardMatch{}},
{"crypto1", Standard{}, specificity.MisMatch{}},
{"crypto/ae", Standard{}, specificity.MisMatch{}},
{"crypto/aes", Standard{}, specificity.StandardMatch{}},
{"crypto/aes2", Standard{}, specificity.MisMatch{}},
{"context", NewStandard(), specificity.StandardMatch{}},
{"contexts", NewStandard(), specificity.MisMatch{}},
{"crypto", NewStandard(), specificity.StandardMatch{}},
{"crypto1", NewStandard(), specificity.MisMatch{}},
{"crypto/ae", NewStandard(), specificity.MisMatch{}},
{"crypto/aes", NewStandard(), specificity.StandardMatch{}},
{"crypto/aes2", NewStandard(), specificity.MisMatch{}},
}
testSpecificity(t, testCases)
}

0 comments on commit c218bc6

Please sign in to comment.