Skip to content

Commit

Permalink
remove hardcoded stdlib packages (#138)
Browse files Browse the repository at this point in the history
* remove hardcoded stdlib packages

Signed-off-by: Xiaoyang Tan <tanx@uber.com>

* ci

Signed-off-by: Xiaoyang Tan <tanx@uber.com>

---------

Signed-off-by: Xiaoyang Tan <tanx@uber.com>
  • Loading branch information
xytan0056 authored Feb 21, 2023
1 parent 7f0d0bc commit c19c1aa
Show file tree
Hide file tree
Showing 5 changed files with 29 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
26 changes: 19 additions & 7 deletions pkg/section/standard.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package section

import (
"golang.org/x/tools/go/packages"

"github.com/daixiang0/gci/pkg/parse"
"github.com/daixiang0/gci/pkg/specificity"
)

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)
}

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 +40,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.

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

func TestStandardPackageSpecificity(t *testing.T) {
standard := NewStandard()
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", 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{}},
}
testSpecificity(t, testCases)
}

0 comments on commit c19c1aa

Please sign in to comment.