-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
gci: fix standard packages list for go1.24 #5402
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2020, Xiang Dai | ||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package config | ||
|
||
import ( | ||
"sort" | ||
"strings" | ||
|
||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/daixiang0/gci/pkg/config" | ||
"github.com/daixiang0/gci/pkg/section" | ||
) | ||
|
||
var defaultOrder = map[string]int{ | ||
section.StandardType: 0, | ||
section.DefaultType: 1, | ||
section.CustomType: 2, | ||
section.BlankType: 3, | ||
section.DotType: 4, | ||
section.AliasType: 5, | ||
section.LocalModuleType: 6, | ||
} | ||
|
||
type Config struct { | ||
config.BoolConfig | ||
Sections section.SectionList | ||
SectionSeparators section.SectionList | ||
} | ||
ldez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type YamlConfig struct { | ||
Cfg config.BoolConfig `yaml:",inline"` | ||
SectionStrings []string `yaml:"sections"` | ||
SectionSeparatorStrings []string `yaml:"sectionseparators"` | ||
|
||
// Since history issue, Golangci-lint needs Analyzer to run and GCI add an Analyzer layer to integrate. | ||
// The ModPath param is only from analyzer.go, no need to set it in all other places. | ||
ModPath string `yaml:"-"` | ||
} | ||
|
||
func (g YamlConfig) Parse() (*Config, error) { | ||
var err error | ||
|
||
sections, err := section.Parse(g.SectionStrings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if sections == nil { | ||
sections = section.DefaultSections() | ||
} | ||
if err := configureSections(sections, g.ModPath); err != nil { | ||
return nil, err | ||
} | ||
|
||
// if default order sorted sections | ||
if !g.Cfg.CustomOrder { | ||
sort.Slice(sections, func(i, j int) bool { | ||
sectionI, sectionJ := sections[i].Type(), sections[j].Type() | ||
|
||
if g.Cfg.NoLexOrder || strings.Compare(sectionI, sectionJ) != 0 { | ||
return defaultOrder[sectionI] < defaultOrder[sectionJ] | ||
} | ||
|
||
return strings.Compare(sections[i].String(), sections[j].String()) < 0 | ||
}) | ||
} | ||
|
||
sectionSeparators, err := section.Parse(g.SectionSeparatorStrings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if sectionSeparators == nil { | ||
sectionSeparators = section.DefaultSectionSeparators() | ||
} | ||
|
||
return &Config{g.Cfg, sections, sectionSeparators}, nil | ||
} | ||
|
||
func ParseConfig(in string) (*Config, error) { | ||
config := YamlConfig{} | ||
|
||
err := yaml.Unmarshal([]byte(in), &config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
gciCfg, err := config.Parse() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return gciCfg, nil | ||
} | ||
|
||
// configureSections now only do golang module path finding. | ||
// Since history issue, Golangci-lint needs Analyzer to run and GCI add an Analyzer layer to integrate. | ||
// The path param is from analyzer.go, in all other places should pass empty string. | ||
func configureSections(sections section.SectionList, path string) error { | ||
for _, sec := range sections { | ||
switch s := sec.(type) { | ||
case *section.LocalModule: | ||
if err := s.Configure(path); err != nil { | ||
return err | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Code borrowed from gci and modified to use new std packages. | ||
|
||
https://github.com/daixiang0/gci/pull/227 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package section | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/daixiang0/gci/pkg/section" | ||
) | ||
|
||
func Parse(data []string) (section.SectionList, error) { | ||
if len(data) == 0 { | ||
return nil, nil | ||
} | ||
|
||
var list section.SectionList | ||
var errString string | ||
for _, d := range data { | ||
s := strings.ToLower(d) | ||
if len(s) == 0 { | ||
return nil, nil | ||
} | ||
|
||
if s == "default" { | ||
list = append(list, section.Default{}) | ||
} else if s == "standard" { | ||
list = append(list, Standard{}) | ||
} else if s == "newline" { | ||
list = append(list, section.NewLine{}) | ||
} else if strings.HasPrefix(s, "prefix(") && len(d) > 8 { | ||
list = append(list, section.Custom{Prefix: d[7 : len(d)-1]}) | ||
} else if strings.HasPrefix(s, "commentline(") && len(d) > 13 { | ||
list = append(list, section.Custom{Prefix: d[12 : len(d)-1]}) | ||
} else if s == "dot" { | ||
list = append(list, section.Dot{}) | ||
} else if s == "blank" { | ||
list = append(list, section.Blank{}) | ||
} else if s == "alias" { | ||
list = append(list, section.Alias{}) | ||
} else if s == "localmodule" { | ||
// pointer because we need to mutate the section at configuration time | ||
list = append(list, §ion.LocalModule{}) | ||
} else { | ||
errString += fmt.Sprintf(" %s", s) | ||
} | ||
} | ||
if errString != "" { | ||
return nil, errors.New(fmt.Sprintf("invalid params:%s", errString)) | ||
} | ||
return list, nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package section | ||
|
||
import ( | ||
"github.com/daixiang0/gci/pkg/parse" | ||
"github.com/daixiang0/gci/pkg/specificity" | ||
) | ||
|
||
const StandardType = "standard" | ||
|
||
type Standard struct{} | ||
|
||
func (s Standard) MatchSpecificity(spec *parse.GciImports) specificity.MatchSpecificity { | ||
if isStandard(spec.Path) { | ||
return specificity.StandardMatch{} | ||
} | ||
return specificity.MisMatch{} | ||
} | ||
|
||
func (s Standard) String() string { | ||
return StandardType | ||
} | ||
|
||
func (s Standard) Type() string { | ||
return StandardType | ||
} | ||
|
||
func isStandard(pkg string) bool { | ||
_, ok := standardPackages[pkg] | ||
return ok | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.