Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove hardcoded stdlib packages #138

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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{}},
daixiang0 marked this conversation as resolved.
Show resolved Hide resolved
{"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)
}