1
0
mirror of https://github.com/mgechev/revive.git synced 2025-11-23 22:04:49 +02:00

chore: format code with gofumpt (#1583)

This commit is contained in:
Oleksandr Redko
2025-11-17 13:35:59 +02:00
committed by GitHub
parent db0fc8688f
commit dbcecde212
13 changed files with 132 additions and 59 deletions

View File

@@ -137,6 +137,7 @@ formatters:
enable: enable:
- gci - gci
- gofmt - gofmt
- gofumpt
- goimports - goimports
settings: settings:
gci: gci:

View File

@@ -287,7 +287,6 @@ func TestGetConfig(t *testing.T) {
} }
cfg, err := config.GetConfig(cfgPath) cfg, err := config.GetConfig(cfgPath)
if err != nil { if err != nil {
t.Fatalf("Unexpected error %v", err) t.Fatalf("Unexpected error %v", err)
} }

View File

@@ -57,8 +57,10 @@ func (f *Friendly) printFriendlyFailure(sb *strings.Builder, failure lint.Failur
sb.WriteString("\n\n") sb.WriteString("\n\n")
} }
var errorEmoji = color.RedString("✘") var (
var warningEmoji = color.YellowString("") errorEmoji = color.RedString("")
warningEmoji = color.YellowString("⚠")
)
func (*Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) { func (*Friendly) printHeaderRow(sb *strings.Builder, failure lint.Failure, severity lint.Severity) {
emoji := warningEmoji emoji := warningEmoji

View File

@@ -12,7 +12,6 @@ func TestGetLogger(t *testing.T) {
t.Setenv("DEBUG", "") t.Setenv("DEBUG", "")
logger, err := logging.GetLogger() logger, err := logging.GetLogger()
if err != nil { if err != nil {
t.Fatalf("expected no error, got %v", err) t.Fatalf("expected no error, got %v", err)
} }
@@ -27,7 +26,6 @@ func TestGetLogger(t *testing.T) {
t.Cleanup(func() { os.Remove("revive.log") }) t.Cleanup(func() { os.Remove("revive.log") })
logger, err := logging.GetLogger() logger, err := logging.GetLogger()
if err != nil { if err != nil {
t.Fatalf("expected no error, got %v", err) t.Fatalf("expected no error, got %v", err)
} }

View File

@@ -36,8 +36,7 @@ func TestReviveCreateInstance(t *testing.T) {
} }
} }
type mockRule struct { type mockRule struct{}
}
func (*mockRule) Name() string { func (*mockRule) Name() string {
return "mock-rule" return "mock-rule"

View File

@@ -17,7 +17,6 @@ func TestAddConstantRule_Configure(t *testing.T) {
wantStrLitLimit int wantStrLitLimit int
}{ }{
{ {
name: "no arguments", name: "no arguments",
arguments: lint.Arguments{}, arguments: lint.Arguments{},
wantErr: nil, wantErr: nil,

View File

@@ -18,121 +18,177 @@ func TestStringFormatConfigure(t *testing.T) {
{ {
name: "Not a Slice", name: "Not a Slice",
arguments: lint.Arguments{ arguments: lint.Arguments{
"this is not a slice"}, "this is not a slice",
wantErr: errors.New("invalid configuration for string-format: argument is not a slice [argument 0, option 0]")}, },
wantErr: errors.New("invalid configuration for string-format: argument is not a slice [argument 0, option 0]"),
},
{ {
name: "Missing Regex", name: "Missing Regex",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"method[0]"}}, "method[0]",
wantErr: errors.New("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]")}, },
},
wantErr: errors.New("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]"),
},
{ {
name: "Bad Argument Type", name: "Bad Argument Type",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
1}}, 1,
wantErr: errors.New("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]")}, },
},
wantErr: errors.New("invalid configuration for string-format: less than two slices found in argument, scope and regex are required [argument 0, option 0]"),
},
{ {
name: "Empty Scope", name: "Empty Scope",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"", "",
"//"}}, "//",
wantErr: errors.New("invalid configuration for string-format: empty scope provided [argument 0, option 0]")}, },
},
wantErr: errors.New("invalid configuration for string-format: empty scope provided [argument 0, option 0]"),
},
{ {
name: "Small or Empty Regex", name: "Small or Empty Regex",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"method[1].a", "method[1].a",
"-"}}, "-",
wantErr: errors.New("invalid configuration for string-format: regex is too small (regexes should begin and end with '/') [argument 0, option 1]")}, },
},
wantErr: errors.New("invalid configuration for string-format: regex is too small (regexes should begin and end with '/') [argument 0, option 1]"),
},
{ {
name: "Bad Scope", name: "Bad Scope",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"1.a", "1.a",
"//"}}, "//",
wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 0]")}, },
},
wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 0]"),
},
{ {
name: "Bad Regex", name: "Bad Regex",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"method[1].a", "method[1].a",
"/(/"}}, "/(/",
wantErr: errors.New("failed to parse configuration for string-format: unable to compile /(/ as regexp [argument 0, option 1]")}, },
},
wantErr: errors.New("failed to parse configuration for string-format: unable to compile /(/ as regexp [argument 0, option 1]"),
},
{ {
name: "Sample Config", name: "Sample Config",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"core.WriteError[1].Message", "/^([^A-Z]$)/", "must not start with a capital letter"}, "core.WriteError[1].Message", "/^([^A-Z]$)/", "must not start with a capital letter",
},
[]any{ []any{
"fmt.Errorf[0]", "/^|[^\\.!?]$/", "must not end in punctuation"}, "fmt.Errorf[0]", "/^|[^\\.!?]$/", "must not end in punctuation",
},
[]any{ []any{
"panic", "/^[^\\n]*$/", "must not contain line breaks"}}}, "panic", "/^[^\\n]*$/", "must not contain line breaks",
},
},
},
{ {
name: "Underscores in Scope", name: "Underscores in Scope",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"some_pkg._some_function_name[5].some_member", "some_pkg._some_function_name[5].some_member",
"//"}}}, "//",
},
},
},
{ {
name: "Underscores in Multiple Scopes", name: "Underscores in Multiple Scopes",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"fmt.Errorf[0],core.WriteError[1].Message", "fmt.Errorf[0],core.WriteError[1].Message",
"//"}}}, "//",
},
},
},
{ {
name: "', ' Delimiter", name: "', ' Delimiter",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"abc, mt.Errorf", "abc, mt.Errorf",
"//"}}}, "//",
},
},
},
{ {
name: "' ,' Delimiter", name: "' ,' Delimiter",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"abc ,mt.Errorf", "abc ,mt.Errorf",
"//"}}}, "//",
},
},
},
{ {
name: "', ' Delimiter", name: "', ' Delimiter",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"abc, mt.Errorf", "abc, mt.Errorf",
"//"}}}, "//",
},
},
},
{ {
name: "', ' Delimiter", name: "', ' Delimiter",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"abc, mt.Errorf", "abc, mt.Errorf",
"//"}}}, "//",
},
},
},
{ {
name: "Empty Middle Scope", name: "Empty Middle Scope",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"abc, ,mt.Errorf", "abc, ,mt.Errorf",
"//"}}, "//",
wantErr: errors.New("failed to parse configuration for string-format: empty scope in rule scopes: [argument 0, option 0, scope index 1]")}, },
},
wantErr: errors.New("failed to parse configuration for string-format: empty scope in rule scopes: [argument 0, option 0, scope index 1]"),
},
{ {
name: "Empty First Scope", name: "Empty First Scope",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
",mt.Errorf", ",mt.Errorf",
"//"}}, "//",
wantErr: errors.New("failed to parse configuration for string-format: empty scope in rule scopes: [argument 0, option 0, scope index 0]")}, },
},
wantErr: errors.New("failed to parse configuration for string-format: empty scope in rule scopes: [argument 0, option 0, scope index 0]"),
},
{ {
name: "Bad First Scope", name: "Bad First Scope",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"1.a,fmt.Errorf[0]", "1.a,fmt.Errorf[0]",
"//"}}, "//",
wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 0]")}, },
},
wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 0]"),
},
{ {
name: "Bad Second Scope", name: "Bad Second Scope",
arguments: lint.Arguments{ arguments: lint.Arguments{
[]any{ []any{
"fmt.Errorf[0],1.a", "fmt.Errorf[0],1.a",
"//"}}, "//",
wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 1]")}} },
},
wantErr: errors.New("failed to parse configuration for string-format: unable to parse rule scope [argument 0, option 0, scope index 1]"),
},
}
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {

View File

@@ -631,7 +631,7 @@ func checkTOMLTag(checkCtx *checkContext, tag *structtag.Tag, _ *ast.Field) (mes
} }
func checkURLTag(checkCtx *checkContext, tag *structtag.Tag, _ *ast.Field) (message string, succeeded bool) { func checkURLTag(checkCtx *checkContext, tag *structtag.Tag, _ *ast.Field) (message string, succeeded bool) {
var delimiter = "" var delimiter string
for _, opt := range tag.Options { for _, opt := range tag.Options {
switch opt { switch opt {
case "int", "omitempty", "numbered", "brackets", case "int", "omitempty", "numbered", "brackets",

View File

@@ -42,13 +42,15 @@ type lintUnsecureURLSchemeRule struct {
onFailure func(lint.Failure) onFailure func(lint.Failure)
} }
const schemeSeparator = "://" const (
const schemeHTTP = "http" schemeSeparator = "://"
const schemeWS = "ws" schemeHTTP = "http"
const urlPrefixHTTP = schemeHTTP + schemeSeparator schemeWS = "ws"
const urlPrefixWS = schemeWS + schemeSeparator urlPrefixHTTP = schemeHTTP + schemeSeparator
const lenURLPrefixHTTP = len(urlPrefixHTTP) urlPrefixWS = schemeWS + schemeSeparator
const lenURLPrefixWS = len(urlPrefixWS) lenURLPrefixHTTP = len(urlPrefixHTTP)
lenURLPrefixWS = len(urlPrefixWS)
)
func (w lintUnsecureURLSchemeRule) Visit(node ast.Node) ast.Visitor { func (w lintUnsecureURLSchemeRule) Visit(node ast.Node) ast.Visitor {
n, ok := node.(*ast.BasicLit) n, ok := node.(*ast.BasicLit)

View File

@@ -9,6 +9,7 @@ import (
func TestCommentSpacings(t *testing.T) { func TestCommentSpacings(t *testing.T) {
testRule(t, "comment_spacings", &rule.CommentSpacingsRule{}, &lint.RuleConfig{ testRule(t, "comment_spacings", &rule.CommentSpacingsRule{}, &lint.RuleConfig{
Arguments: []any{"myOwnDirective:", "+optional"}}, Arguments: []any{"myOwnDirective:", "+optional"},
},
) )
} }

View File

@@ -13,6 +13,7 @@ func TestMaxControlNestingDefault(t *testing.T) {
func TestMaxControlNesting(t *testing.T) { func TestMaxControlNesting(t *testing.T) {
testRule(t, "max_control_nesting", &rule.MaxControlNestingRule{}, &lint.RuleConfig{ testRule(t, "max_control_nesting", &rule.MaxControlNestingRule{}, &lint.RuleConfig{
Arguments: []any{int64(2)}}, Arguments: []any{int64(2)},
},
) )
} }

View File

@@ -13,19 +13,25 @@ func TestStringFormat(t *testing.T) {
[]any{ []any{
"stringFormatMethod1", // The first argument is checked by default "stringFormatMethod1", // The first argument is checked by default
"/^[A-Z]/", "/^[A-Z]/",
"must start with a capital letter"}, "must start with a capital letter",
},
[]any{ []any{
"stringFormatMethod2[2].d", "stringFormatMethod2[2].d",
"/[^\\.]$/"}, // Must not end with a period "/[^\\.]$/",
}, // Must not end with a period
[]any{ []any{
"s.Method3[2]", "s.Method3[2]",
"!/^[Tt][Hh]/", "!/^[Tt][Hh]/",
"must not start with 'th'"}, "must not start with 'th'",
},
[]any{ []any{
"s.Method4", // same as before, but called from a struct "s.Method4", // same as before, but called from a struct
"!/^[Ot][Tt]/", "!/^[Ot][Tt]/",
"must not start with 'ot'"}}}) "must not start with 'ot'",
},
},
})
} }
func TestStringFormatDuplicatedStrings(t *testing.T) { func TestStringFormatDuplicatedStrings(t *testing.T) {

View File

@@ -16,19 +16,22 @@ func TestVarNaming(t *testing.T) {
[]any{}, []any{},
[]any{}, []any{},
[]any{map[string]any{"skip-initialism-name-checks": true}}, []any{map[string]any{"skip-initialism-name-checks": true}},
}}) },
})
testRule(t, "var_naming_skip_initialism_name_checks_false", &rule.VarNamingRule{}, &lint.RuleConfig{ testRule(t, "var_naming_skip_initialism_name_checks_false", &rule.VarNamingRule{}, &lint.RuleConfig{
Arguments: []any{ Arguments: []any{
[]any{}, []any{},
[]any{}, []any{},
[]any{map[string]any{"skip-initialism-name-checks": false}}, []any{map[string]any{"skip-initialism-name-checks": false}},
}}) },
})
testRule(t, "var_naming_allowlist_blocklist_skip_initialism_name_checks", &rule.VarNamingRule{}, &lint.RuleConfig{ testRule(t, "var_naming_allowlist_blocklist_skip_initialism_name_checks", &rule.VarNamingRule{}, &lint.RuleConfig{
Arguments: []any{ Arguments: []any{
[]any{"ID"}, []any{"ID"},
[]any{"VM"}, []any{"VM"},
[]any{map[string]any{"skip-initialism-name-checks": true}}, []any{map[string]any{"skip-initialism-name-checks": true}},
}}) },
})
testRule(t, "var_naming_test", &rule.VarNamingRule{}, &lint.RuleConfig{}) testRule(t, "var_naming_test", &rule.VarNamingRule{}, &lint.RuleConfig{})
@@ -49,19 +52,25 @@ func TestVarNaming(t *testing.T) {
}) })
testRule(t, "var_naming_meaningless_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{}) testRule(t, "var_naming_meaningless_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{})
testRule(t, "var_naming_meaningless_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{ testRule(t, "var_naming_meaningless_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{
Arguments: []any{[]any{}, []any{}, Arguments: []any{
[]any{},
[]any{},
[]any{map[string]any{"skip-package-name-checks": false}}, []any{map[string]any{"skip-package-name-checks": false}},
}, },
}) })
testRule(t, "var_naming_bad_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{ testRule(t, "var_naming_bad_package_name", &rule.VarNamingRule{}, &lint.RuleConfig{
Arguments: []any{[]any{}, []any{}, Arguments: []any{
[]any{},
[]any{},
[]any{map[string]any{"skip-package-name-checks": false, "extra-bad-package-names": []any{"helpers"}}}, []any{map[string]any{"skip-package-name-checks": false, "extra-bad-package-names": []any{"helpers"}}},
}, },
}) })
testRule(t, "var_naming_top_level_pkg", &rule.VarNamingRule{}, &lint.RuleConfig{}) testRule(t, "var_naming_top_level_pkg", &rule.VarNamingRule{}, &lint.RuleConfig{})
testRule(t, "var_naming_std_lib_conflict", &rule.VarNamingRule{}, &lint.RuleConfig{}) testRule(t, "var_naming_std_lib_conflict", &rule.VarNamingRule{}, &lint.RuleConfig{})
testRule(t, "var_naming_std_lib_conflict_skip", &rule.VarNamingRule{}, &lint.RuleConfig{ testRule(t, "var_naming_std_lib_conflict_skip", &rule.VarNamingRule{}, &lint.RuleConfig{
Arguments: []any{[]any{}, []any{}, Arguments: []any{
[]any{},
[]any{},
[]any{map[string]any{"skip-package-name-collision-with-go-std": true}}, []any{map[string]any{"skip-package-name-collision-with-go-std": true}},
}, },
}) })