Skip to content

Commit 03f90a6

Browse files
committed
fix: toStaticCheckSettings
Signed-off-by: sivchari <[email protected]>
1 parent 60f4cff commit 03f90a6

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

pkg/commands/internal/migrate/migrate_linters_settings.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,22 @@ func toSpancheckSettings(old versionone.SpancheckSettings) versiontwo.SpancheckS
789789
}
790790

791791
func toStaticCheckSettings(old versionone.LintersSettings) versiontwo.StaticCheckSettings {
792-
checks := slices.Concat(old.Staticcheck.Checks, old.Stylecheck.Checks, old.Gosimple.Checks)
792+
checks := Unique(slices.Concat(old.Staticcheck.Checks, old.Stylecheck.Checks, old.Gosimple.Checks))
793+
// If an element is prefixed with `-` it means that we want to disable that check.
794+
// So we have to resort and put everything after the non-minus elements.
795+
slices.SortFunc(checks, func(e1, e2 string) int {
796+
if strings.HasPrefix(e1, "-") && !strings.HasPrefix(e2, "-") {
797+
return 1
798+
}
799+
800+
if !strings.HasPrefix(e1, "-") && strings.HasPrefix(e2, "-") {
801+
return -1
802+
}
803+
return strings.Compare(e1, e2)
804+
})
793805

794806
return versiontwo.StaticCheckSettings{
795-
Checks: Unique(checks),
807+
Checks: checks,
796808
Initialisms: old.Stylecheck.Initialisms,
797809
DotImportWhitelist: old.Stylecheck.DotImportWhitelist,
798810
HTTPStatusCodeWhitelist: old.Stylecheck.HTTPStatusCodeWhitelist,
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package migrate
2+
3+
import (
4+
"testing"
5+
6+
"github.com/golangci/golangci-lint/v2/pkg/commands/internal/migrate/versionone"
7+
"github.com/golangci/golangci-lint/v2/pkg/commands/internal/migrate/versiontwo"
8+
"github.com/google/go-cmp/cmp"
9+
)
10+
11+
func Test_toStaticCheckSettings(t *testing.T) {
12+
old := versionone.LintersSettings{
13+
Staticcheck: versionone.StaticCheckSettings{
14+
Checks: []string{"all", "-SA1000"},
15+
},
16+
}
17+
new := toStaticCheckSettings(old)
18+
19+
expected := versiontwo.StaticCheckSettings{
20+
Checks: []string{"all", "-SA1000"},
21+
}
22+
23+
if diff := cmp.Diff(new, expected); diff != "" {
24+
t.Errorf("toStaticCheckSettings() mismatch (-got +want):\n%s", diff)
25+
}
26+
}

0 commit comments

Comments
 (0)