revive: fix add-constant rule support. (#2003)
This commit is contained in:
parent
b73972f6af
commit
cf8fd685d0
@ -495,7 +495,7 @@ linters-settings:
|
||||
rowserrcheck:
|
||||
packages:
|
||||
- github.com/jmoiron/sqlx
|
||||
-
|
||||
|
||||
revive:
|
||||
# see https://github.com/mgechev/revive#available-rules for details.
|
||||
ignore-generated-header: true
|
||||
@ -503,6 +503,13 @@ linters-settings:
|
||||
rules:
|
||||
- name: indent-error-flow
|
||||
severity: warning
|
||||
- name: add-constant
|
||||
severity: warning
|
||||
arguments:
|
||||
- maxLitCount: "3"
|
||||
allowStrs: '""'
|
||||
allowInts: "0,1,2"
|
||||
allowFloats: "0.0,0.,1.0,1.,2.0,2."
|
||||
|
||||
staticcheck:
|
||||
# Select the Go version to target. The default is '1.13'.
|
||||
|
@ -4,7 +4,6 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@ -16,6 +15,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"gopkg.in/yaml.v3"
|
||||
|
||||
"github.com/golangci/golangci-lint/internal/cache"
|
||||
"github.com/golangci/golangci-lint/internal/pkgcache"
|
||||
@ -194,7 +194,7 @@ func computeConfigSalt(cfg *config.Config) ([]byte, error) {
|
||||
// We don't hash all config fields to reduce meaningless cache
|
||||
// invalidations. At least, it has a huge impact on tests speed.
|
||||
|
||||
lintersSettingsBytes, err := json.Marshal(cfg.LintersSettings)
|
||||
lintersSettingsBytes, err := yaml.Marshal(cfg.LintersSettings)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to json marshal config linter settings")
|
||||
}
|
||||
|
@ -13,16 +13,20 @@ import (
|
||||
reviveConfig "github.com/mgechev/revive/config"
|
||||
"github.com/mgechev/revive/lint"
|
||||
"github.com/mgechev/revive/rule"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/tools/go/analysis"
|
||||
|
||||
"github.com/golangci/golangci-lint/pkg/config"
|
||||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
|
||||
"github.com/golangci/golangci-lint/pkg/lint/linter"
|
||||
"github.com/golangci/golangci-lint/pkg/logutils"
|
||||
"github.com/golangci/golangci-lint/pkg/result"
|
||||
)
|
||||
|
||||
const reviveName = "revive"
|
||||
|
||||
var reviveDebugf = logutils.Debug("revive")
|
||||
|
||||
// jsonObject defines a JSON object of an failure
|
||||
type jsonObject struct {
|
||||
Severity lint.Severity
|
||||
@ -145,18 +149,20 @@ func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
|
||||
|
||||
err := toml.NewEncoder(buf).Encode(rawRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "failed to encode configuration")
|
||||
}
|
||||
|
||||
conf = &lint.Config{}
|
||||
_, err = toml.DecodeReader(buf, conf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "failed to decode configuration")
|
||||
}
|
||||
}
|
||||
|
||||
normalizeConfig(conf)
|
||||
|
||||
reviveDebugf("revive configuration: %#v", conf)
|
||||
|
||||
return conf, nil
|
||||
}
|
||||
|
||||
@ -184,7 +190,7 @@ func createConfigMap(cfg *config.ReviveSettings) map[string]interface{} {
|
||||
for _, s := range cfg.Rules {
|
||||
rawRules[s.Name] = map[string]interface{}{
|
||||
"severity": s.Severity,
|
||||
"arguments": s.Arguments,
|
||||
"arguments": safeTomlSlice(s.Arguments),
|
||||
}
|
||||
}
|
||||
|
||||
@ -195,6 +201,28 @@ func createConfigMap(cfg *config.ReviveSettings) map[string]interface{} {
|
||||
return rawRoot
|
||||
}
|
||||
|
||||
func safeTomlSlice(r []interface{}) []interface{} {
|
||||
if len(r) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, ok := r[0].(map[interface{}]interface{}); !ok {
|
||||
return r
|
||||
}
|
||||
|
||||
var typed []interface{}
|
||||
for _, elt := range r {
|
||||
item := map[string]interface{}{}
|
||||
for k, v := range elt.(map[interface{}]interface{}) {
|
||||
item[k.(string)] = v
|
||||
}
|
||||
|
||||
typed = append(typed, item)
|
||||
}
|
||||
|
||||
return typed
|
||||
}
|
||||
|
||||
// This element is not exported by revive, so we need copy the code.
|
||||
// Extracted from https://github.com/mgechev/revive/blob/389ba853b0b3587f0c3b71b5f0c61ea4e23928ec/config/config.go#L15
|
||||
var defaultRules = []lint.Rule{
|
||||
|
Loading…
x
Reference in New Issue
Block a user