Skip to content

Commit

Permalink
Revert "remove hardcoded stdlib packages (#138)"
Browse files Browse the repository at this point in the history
This reverts commit c19c1aa.
  • Loading branch information
daixiang0 committed Mar 13, 2023
1 parent b0ea6e8 commit 0328396
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 29 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, NewStandard())
list = append(list, Standard{})
} 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{NewStandard(), Default{}}
return SectionList{Standard{}, Default{}}
}

func DefaultSectionSeparators() SectionList {
Expand Down
26 changes: 7 additions & 19 deletions pkg/section/standard.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
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 {
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}
}
type Standard struct{}

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

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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 7 additions & 8 deletions pkg/section/standard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ 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 0328396

Please sign in to comment.