Skip to content

Commit 6d475f5

Browse files
sivcharildez
authored andcommitted
add nosnakecase lint (golangci#2828)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent fed51b5 commit 6d475f5

File tree

6 files changed

+177
-7
lines changed

6 files changed

+177
-7
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ require (
155155
github.com/quasilyte/gogrep v0.0.0-20220120141003-628d8b3623b5 // indirect
156156
github.com/quasilyte/regex/syntax v0.0.0-20200407221936-30656e2c4a95 // indirect
157157
github.com/quasilyte/stdinfo v0.0.0-20220114132959-f7386bf02567 // indirect
158+
github.com/sivchari/nosnakecase v1.5.0
158159
github.com/spf13/afero v1.8.2 // indirect
159160
github.com/spf13/cast v1.5.0 // indirect
160161
github.com/spf13/jwalterweatherman v1.1.0 // indirect

go.sum

+2-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/nosnakecase.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package golinters
2+
3+
import (
4+
"github.com/sivchari/nosnakecase"
5+
"golang.org/x/tools/go/analysis"
6+
7+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
8+
)
9+
10+
func NewNoSnakeCase() *goanalysis.Linter {
11+
a := nosnakecase.Analyzer
12+
13+
return goanalysis.NewLinter(
14+
a.Name,
15+
a.Doc,
16+
[]*analysis.Analyzer{a},
17+
nil,
18+
).WithLoadMode(goanalysis.LoadModeSyntax)
19+
}

pkg/lint/lintersdb/manager.go

+5
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
631631
WithPresets(linter.PresetStyle).
632632
WithURL("https://github.com/firefart/nonamedreturns"),
633633

634+
linter.NewConfig(golinters.NewNoSnakeCase()).
635+
WithSince("v1.47.0").
636+
WithPresets(linter.PresetStyle).
637+
WithURL("https://github.com/sivchari/nosnakecase"),
638+
634639
linter.NewConfig(golinters.NewNoSprintfHostPort()).
635640
WithSince("v1.46.0").
636641
WithPresets(linter.PresetStyle).

test/run_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func TestTestsAreLintedByDefault(t *testing.T) {
9595
}
9696

9797
func TestCgoOk(t *testing.T) {
98-
testshared.NewLintRunner(t).Run("--no-config", "--enable-all", getTestDataDir("cgo")).ExpectNoIssues()
98+
testshared.NewLintRunner(t).Run("--no-config", "--enable-all", "-D", "nosnakecase", getTestDataDir("cgo")).ExpectNoIssues()
9999
}
100100

101101
func TestCgoWithIssues(t *testing.T) {

test/testdata/nosnakecase.go

+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
//args: -Enosnakecase
2+
package testdata
3+
4+
import (
5+
_ "fmt"
6+
f_m_t "fmt" // ERROR "f_m_t contains underscore. You should use mixedCap or MixedCap."
7+
)
8+
9+
// global variable name with underscore.
10+
var v_v = 0 // ERROR "v_v contains underscore. You should use mixedCap or MixedCap."
11+
12+
// global constant name with underscore.
13+
const c_c = 0 // ERROR "c_c contains underscore. You should use mixedCap or MixedCap."
14+
15+
// struct name with underscore.
16+
type S_a struct { // ERROR "S_a contains underscore. You should use mixedCap or MixedCap."
17+
fi int
18+
}
19+
20+
// non-exported struct field name with underscore.
21+
type Sa struct {
22+
fi_a int // // ERROR "fi_a contains underscore. You should use mixedCap or MixedCap."
23+
}
24+
25+
// function as struct field, with parameter name with underscore.
26+
type Sb struct {
27+
fib func(p_a int) // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
28+
}
29+
30+
// exported struct field with underscore.
31+
type Sc struct {
32+
Fi_A int // ERROR "Fi_A contains underscore. You should use mixedCap or MixedCap."
33+
}
34+
35+
// function as struct field, with return name with underscore.
36+
type Sd struct {
37+
fib func(p int) (r_a int) // ERROR "r_a contains underscore. You should use mixedCap or MixedCap."
38+
}
39+
40+
// interface name with underscore.
41+
type I_a interface { // ERROR "I_a contains underscore. You should use mixedCap or MixedCap."
42+
fn(p int)
43+
}
44+
45+
// interface with parameter name with underscore.
46+
type Ia interface {
47+
fn(p_a int) // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
48+
}
49+
50+
// interface with parameter name with underscore.
51+
type Ib interface {
52+
Fn(p_a int) // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
53+
}
54+
55+
// function as struct field, with return name with underscore.
56+
type Ic interface {
57+
Fn_a() // ERROR "Fn_a contains underscore. You should use mixedCap or MixedCap."
58+
}
59+
60+
// interface with return name with underscore.
61+
type Id interface {
62+
Fn() (r_a int) // ERROR "r_a contains underscore. You should use mixedCap or MixedCap."
63+
}
64+
65+
// function name with underscore.
66+
func f_a() {} // ERROR "f_a contains underscore. You should use mixedCap or MixedCap."
67+
68+
// function's parameter name with underscore.
69+
func fb(p_a int) {} // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
70+
71+
// named return with underscore.
72+
func fc() (r_b int) { // ERROR "r_b contains underscore. You should use mixedCap or MixedCap."
73+
return 0
74+
}
75+
76+
// local variable (short declaration) with underscore.
77+
func fd(p int) int {
78+
v_b := p * 2 // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
79+
80+
return v_b // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
81+
}
82+
83+
// local constant with underscore.
84+
func fe(p int) int {
85+
const v_b = 2 // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
86+
87+
return v_b * p // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
88+
}
89+
90+
// local variable with underscore.
91+
func ff(p int) int {
92+
var v_b = 2 // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
93+
94+
return v_b * p // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
95+
}
96+
97+
// inner function, parameter name with underscore.
98+
func fg() {
99+
fgl := func(p_a int) {} // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
100+
fgl(1)
101+
}
102+
103+
type Foo struct{}
104+
105+
// method name with underscore.
106+
func (f Foo) f_a() {} // ERROR "f_a contains underscore. You should use mixedCap or MixedCap."
107+
108+
// method's parameter name with underscore.
109+
func (f Foo) fb(p_a int) {} // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
110+
111+
// named return with underscore.
112+
func (f Foo) fc() (r_b int) { return 0 } // ERROR "r_b contains underscore. You should use mixedCap or MixedCap."
113+
114+
// local variable (short declaration) with underscore.
115+
func (f Foo) fd(p int) int {
116+
v_b := p * 2 // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
117+
118+
return v_b // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
119+
}
120+
121+
// local constant with underscore.
122+
func (f Foo) fe(p int) int {
123+
const v_b = 2 // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
124+
125+
return v_b * p // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
126+
}
127+
128+
// local variable with underscore.
129+
func (f Foo) ff(p int) int {
130+
var v_b = 2 // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
131+
132+
return v_b * p // ERROR "v_b contains underscore. You should use mixedCap or MixedCap."
133+
}
134+
135+
func fna(a, p_a int) {} // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
136+
137+
func fna1(a string, p_a int) {} // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
138+
139+
func fnb(a, b, p_a int) {} // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
140+
141+
func fnb1(a, b string, p_a int) {} // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
142+
143+
func fnd(
144+
p_a int, // ERROR "p_a contains underscore. You should use mixedCap or MixedCap."
145+
p_b int, // ERROR "p_b contains underscore. You should use mixedCap or MixedCap."
146+
p_c int, // ERROR "p_c contains underscore. You should use mixedCap or MixedCap."
147+
) {
148+
f_m_t.Println("") // ERROR "f_m_t contains underscore. You should use mixedCap or MixedCap."
149+
}

0 commit comments

Comments
 (0)