Skip to content

Commit

Permalink
revive: the default configuration is only applied when no dedicated c…
Browse files Browse the repository at this point in the history
…onfiguration.
  • Loading branch information
ldez committed Mar 10, 2021
1 parent 2ebc9d7 commit 680d64e
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 5 deletions.
36 changes: 35 additions & 1 deletion pkg/golinters/revive.go
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"go/token"
"io/ioutil"
"reflect"

"github.com/BurntSushi/toml"
"github.com/mgechev/dots"
Expand Down Expand Up @@ -135,7 +136,7 @@ func NewRevive(cfg *config.ReviveSettings) *goanalysis.Linter {
// This allow to get default values and right types.
// https://github.com/golangci/golangci-lint/issues/1745
// https://github.com/mgechev/revive/blob/389ba853b0b3587f0c3b71b5f0c61ea4e23928ec/config/config.go#L155
func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
func getReviveConfigOLD(cfg *config.ReviveSettings) (*lint.Config, error) {
rawRoot := createConfigMap(cfg)

buf := bytes.NewBuffer(nil)
Expand All @@ -162,6 +163,39 @@ func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
return conf, nil
}

// This function mimics the GetConfig function of revive.
// This allow to get default values and right types.
// https://github.com/golangci/golangci-lint/issues/1745
// https://github.com/mgechev/revive/blob/389ba853b0b3587f0c3b71b5f0c61ea4e23928ec/config/config.go#L155
func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
conf := defaultConfig()

if !reflect.DeepEqual(cfg, &config.ReviveSettings{}) {
rawRoot := createConfigMap(cfg)
buf := bytes.NewBuffer(nil)

err := toml.NewEncoder(buf).Encode(rawRoot)
if err != nil {
return nil, err
}

conf = &lint.Config{}
_, err = toml.DecodeReader(buf, conf)
if err != nil {
return nil, err
}
}

normalizeConfig(conf)

// By default golangci-lint ignores missing doc comments, follow same convention by removing this default rule
// Relevant issue: https://github.com/golangci/golangci-lint/issues/456
delete(conf.Rules, "package-comments")
delete(conf.Rules, "exported")

return conf, nil
}

func createConfigMap(cfg *config.ReviveSettings) map[string]interface{} {
rawRoot := map[string]interface{}{
"ignoreGeneratedHeader": cfg.IgnoreGeneratedHeader,
Expand Down
2 changes: 0 additions & 2 deletions test/testdata/configs/revive.yml
Expand Up @@ -3,8 +3,6 @@ linters-settings:
ignore-generated-header: true
severity: warning
rules:
- name: indent-error-flow
severity: warning
- name: cognitive-complexity
arguments: [ 7 ]
- name: line-length-limit
Expand Down
21 changes: 19 additions & 2 deletions test/testdata/revive.go
Expand Up @@ -2,12 +2,29 @@
//config_path: testdata/configs/revive.yml
package testdata

import "time"
import (
"net/http"
"time"
)

func testRevive(t *time.Duration) error {
if t == nil {
return nil
} else { // ERROR "indent-error-flow: if block ends with a return statement, .*"
} else {
return nil
}
}

func testReviveComplexity(s string) { // ERROR "cyclomatic: function testReviveComplexity has cyclomatic complexity 22"
if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}

if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}

if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}
}
29 changes: 29 additions & 0 deletions test/testdata/revive_default.go
@@ -0,0 +1,29 @@
//args: -Erevive
package testdata

import (
"net/http"
"time"
)

func testReviveDefault(t *time.Duration) error {
if t == nil {
return nil
} else { // ERROR "indent-error-flow: if block ends with a return statement, .*"
return nil
}
}

func testReviveComplexityDefault(s string) {
if s == http.MethodGet || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}

if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}

if s == "1" || s == "2" || s == "3" || s == "4" || s == "5" || s == "6" || s == "7" {
return
}
}

0 comments on commit 680d64e

Please sign in to comment.