Skip to content

Commit c19c1aa

Browse files
authored
remove hardcoded stdlib packages (#138)
* remove hardcoded stdlib packages Signed-off-by: Xiaoyang Tan <[email protected]> * ci Signed-off-by: Xiaoyang Tan <[email protected]> --------- Signed-off-by: Xiaoyang Tan <[email protected]>
1 parent 7f0d0bc commit c19c1aa

File tree

5 files changed

+29
-176
lines changed

5 files changed

+29
-176
lines changed

pkg/section/parser.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func Parse(data []string) (SectionList, error) {
2222
if s == "default" {
2323
list = append(list, Default{})
2424
} else if s == "standard" {
25-
list = append(list, Standard{})
25+
list = append(list, NewStandard())
2626
} else if s == "newline" {
2727
list = append(list, NewLine{})
2828
} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 {

pkg/section/section.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (list SectionList) String() []string {
2828
}
2929

3030
func DefaultSections() SectionList {
31-
return SectionList{Standard{}, Default{}}
31+
return SectionList{NewStandard(), Default{}}
3232
}
3333

3434
func DefaultSectionSeparators() SectionList {

pkg/section/standard.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
11
package section
22

33
import (
4+
"golang.org/x/tools/go/packages"
5+
46
"github.com/daixiang0/gci/pkg/parse"
57
"github.com/daixiang0/gci/pkg/specificity"
68
)
79

810
const StandardType = "standard"
911

10-
type Standard struct{}
12+
type Standard struct {
13+
standardPackages map[string]struct{}
14+
}
15+
16+
func NewStandard() Standard {
17+
pkgs, err := packages.Load(nil, "std")
18+
if err != nil {
19+
panic(err)
20+
}
21+
22+
standardPackages := make(map[string]struct{})
23+
for _, p := range pkgs {
24+
standardPackages[p.PkgPath] = struct{}{}
25+
}
26+
return Standard{standardPackages: standardPackages}
27+
}
1128

1229
func (s Standard) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity {
13-
if isStandard(spec.Path) {
30+
if _, ok := s.standardPackages[spec.Path]; ok {
1431
return specificity.StandardMatch{}
1532
}
1633
return specificity.MisMatch{}
@@ -23,8 +40,3 @@ func (s Standard) String() string {
2340
func (s Standard) Type() string {
2441
return StandardType
2542
}
26-
27-
func isStandard(pkg string) bool {
28-
_, ok := standardPackages[pkg]
29-
return ok
30-
}

pkg/section/standard_list.go

-160
This file was deleted.

pkg/section/standard_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import (
77
)
88

99
func TestStandardPackageSpecificity(t *testing.T) {
10+
standard := NewStandard()
1011
testCases := []specificityTestData{
11-
{"context", Standard{}, specificity.StandardMatch{}},
12-
{"contexts", Standard{}, specificity.MisMatch{}},
13-
{"crypto", Standard{}, specificity.StandardMatch{}},
14-
{"crypto1", Standard{}, specificity.MisMatch{}},
15-
{"crypto/ae", Standard{}, specificity.MisMatch{}},
16-
{"crypto/aes", Standard{}, specificity.StandardMatch{}},
17-
{"crypto/aes2", Standard{}, specificity.MisMatch{}},
12+
{"context", standard, specificity.StandardMatch{}},
13+
{"contexts", standard, specificity.MisMatch{}},
14+
{"crypto", standard, specificity.StandardMatch{}},
15+
{"crypto1", standard, specificity.MisMatch{}},
16+
{"crypto/ae", standard, specificity.MisMatch{}},
17+
{"crypto/aes", standard, specificity.StandardMatch{}},
18+
{"crypto/aes2", standard, specificity.MisMatch{}},
1819
}
1920
testSpecificity(t, testCases)
2021
}

0 commit comments

Comments
 (0)