Skip to content

revive: fix add-constant rule support. #2003

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 1 commit into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .golangci.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -496,14 +496,21 @@ linters-settings:
rowserrcheck:
packages:
- github.com/jmoiron/sqlx
-

revive:
# see https://github.com/mgechev/revive#available-rules for details.
ignore-generated-header: true
severity: warning
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'.
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"crypto/sha256"
"encoding/json"
"io"
"os"
"path/filepath"
Expand All @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
34 changes: 31 additions & 3 deletions pkg/golinters/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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),
}
}

Expand All @@ -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{
Expand Down