Skip to content

Commit

Permalink
first review
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 31, 2022
1 parent deb352c commit 3059c0f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 35 deletions.
31 changes: 22 additions & 9 deletions .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -295,18 +295,31 @@ linters-settings:
statements: -1

gci:
# Checks that no inline Comments are present
no-inlineComments: false
# Checks that no prefix Comments(comment lines above an import) are present
no-prefixComments: false
# DEPRECATED: use `sections` and `prefix(github.com/org/project)` instead.
local-prefixes: github.com/org/project

# Checks that no inline Comments are present.
# Default: false
no-inlineComments: true

# Checks that no prefix Comments(comment lines above an import) are present.
# Default: false
no-prefixComments: true

# Section configuration to compare against.
# Run gci print -h for a detailed explanation
# Section names are case-insensitive and may contain parameters in ().
# Default: ["standard", "default"]
sections:
- Standard
- Default
# Separators that should be present between sections
- standard # Captures all standard packages if they do not match another section.
- default # Contains all imports that could not be matched to another section type.
- comment(your text here) # Prints the specified indented comment.
- newLine # Prints an empty line
- prefix(github.com/org/project) # Groups all imports with the specified Prefix.

# Separators that should be present between sections.
# Default: ["newLine"]
sectionSeparators:
- Newline
- newLine

gocognit:
# Minimal code complexity to report
Expand Down
6 changes: 0 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ linters-settings:
funlen:
lines: 100
statements: 50
gci:
sections:
- Standard
- Default
- Prefix(github.com/golangci/golangci-lint)
goconst:
min-len: 2
min-occurrences: 3
Expand Down Expand Up @@ -76,7 +71,6 @@ linters:
- errcheck
- exportloopref
- funlen
- gci
- gochecknoinits
- goconst
- gocritic
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package config
import "github.com/pkg/errors"

var defaultLintersSettings = LintersSettings{
Gci: GciSettings{
Sections: []string{"default", "standard"},
SectionSeparator: []string{"newline"},
},
Decorder: DecorderSettings{
DecOrder: []string{"type", "const", "var", "func"},
DisableDecNumCheck: true,
Expand Down Expand Up @@ -253,6 +257,7 @@ type FunlenSettings struct {
}

type GciSettings struct {
LocalPrefixes string `mapstructure:"local-prefixes"` // Deprecated
NoInlineComments bool `mapstructure:"no-inlineComments"`
NoPrefixComments bool `mapstructure:"no-prefixComments"`
Sections []string `mapstructure:"sections"`
Expand Down
30 changes: 19 additions & 11 deletions pkg/golinters/gci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package golinters

import (
"fmt"
"strings"

gciAnalyzer "github.com/daixiang0/gci/pkg/analyzer"
Expand All @@ -13,23 +14,30 @@ import (
const gciName = "gci"

func NewGci(settings *config.GciSettings) *goanalysis.Linter {
analyzer := gciAnalyzer.Analyzer
var cfg map[string]map[string]interface{}
var linterCfg map[string]map[string]interface{}

if settings != nil {
cfg = map[string]map[string]interface{}{
analyzer.Name: {
gciAnalyzer.NoInlineCommentsFlag: settings.NoInlineComments,
gciAnalyzer.NoPrefixCommentsFlag: settings.NoPrefixComments,
gciAnalyzer.SectionsFlag: strings.Join(settings.Sections, gciAnalyzer.SectionDelimiter),
gciAnalyzer.SectionSeparatorsFlag: strings.Join(settings.SectionSeparator, gciAnalyzer.SectionDelimiter),
},
cfg := map[string]interface{}{
gciAnalyzer.NoInlineCommentsFlag: settings.NoInlineComments,
gciAnalyzer.NoPrefixCommentsFlag: settings.NoPrefixComments,
gciAnalyzer.SectionsFlag: strings.Join(settings.Sections, gciAnalyzer.SectionDelimiter),
gciAnalyzer.SectionSeparatorsFlag: strings.Join(settings.SectionSeparator, gciAnalyzer.SectionDelimiter),
}

if settings.LocalPrefixes != "" {
prefix := []string{"Standard", "Default", fmt.Sprintf("Prefix(%s)", settings.LocalPrefixes)}
cfg[gciAnalyzer.SectionsFlag] = strings.Join(prefix, gciAnalyzer.SectionDelimiter)
}

linterCfg = map[string]map[string]interface{}{
gciAnalyzer.Analyzer.Name: cfg,
}
}

return goanalysis.NewLinter(
gciName,
"Gci controls golang package import order and makes it always deterministic.",
[]*analysis.Analyzer{analyzer},
cfg,
[]*analysis.Analyzer{gciAnalyzer.Analyzer},
linterCfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
9 changes: 0 additions & 9 deletions pkg/golinters/gofmt_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,6 @@ func getErrorTextForLinter(lintCtx *linter.Context, linterName string) string {
if lintCtx.Settings().Goimports.LocalPrefixes != "" {
text += " with -local " + lintCtx.Settings().Goimports.LocalPrefixes
}
case gciName:
optionsText := []string{}
if lintCtx.Settings().Gci.NoInlineComments {
optionsText = append(optionsText, "NoInlineComments")
}
if lintCtx.Settings().Gci.NoPrefixComments {
optionsText = append(optionsText, "NoPrefixComments")
}
text = fmt.Sprintf("File does not conform to the import format configured for `Gci`(%s)", strings.Join(optionsText, ","))
}
return text
}
Expand Down

0 comments on commit 3059c0f

Please sign in to comment.