Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
I simplify logic and adapt undetected syntax (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
sivchari authored May 31, 2022
1 parent 0b3726d commit 83240da
Show file tree
Hide file tree
Showing 3 changed files with 309 additions and 92 deletions.
186 changes: 172 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,191 @@ go install github.com/sivchari/nosnakecase/cmd/nosnakecase@latest
```go
package sandbox

func a_() {
// global variable name with underscore.
var v_v = 0 // want "v_v is used under score. You should use mixedCap or MixedCap."

// global constant name with underscore.
const c_c = 0 // want "c_c is used under score. You should use mixedCap or MixedCap."

// struct name with underscore.
type S_a struct { // want "S_a is used under score. You should use mixedCap or MixedCap."
fi int
}

// non-exported struct field name with underscore.
type Sa struct {
fi_a int // // want "fi_a is used under score. You should use mixedCap or MixedCap."
}

// function as struct field, with parameter name with underscore.
type Sb struct {
fib func(p_a int) // want "p_a is used under score. You should use mixedCap or MixedCap."
}

// exported struct field with underscore.
type Sc struct {
Fi_A int // want "Fi_A is used under score. You should use mixedCap or MixedCap."
}

// function as struct field, with return name with underscore.
type Sd struct {
fib func(p int) (r_a int) // want "r_a is used under score. You should use mixedCap or MixedCap."
}

// interface name with underscore.
type I_a interface { // want "I_a is used under score. You should use mixedCap or MixedCap."
fn(p int)
}

// interface with parameter name with underscore.
type Ia interface {
fn(p_a int) // want "p_a is used under score. You should use mixedCap or MixedCap."
}

// interface with parameter name with underscore.
type Ib interface {
Fn(p_a int) // want "p_a is used under score. You should use mixedCap or MixedCap."
}

// function as struct field, with return name with underscore.
type Ic interface {
Fn_a() // want "Fn_a is used under score. You should use mixedCap or MixedCap."
}

// interface with return name with underscore.
type Id interface {
Fn() (r_a int) // want "r_a is used under score. You should use mixedCap or MixedCap."
}

// function name with underscore.
func f_a() {} // want "f_a is used under score. You should use mixedCap or MixedCap."

// function's parameter name with underscore.
func fb(p_a int) {} // want "p_a is used under score. You should use mixedCap or MixedCap."

// named return with underscore.
func fc() (r_b int) { // want "r_b is used under score. You should use mixedCap or MixedCap."
return 0
}

func b(a_a int) {
// local variable (short declaration) with underscore.
func fd(p int) int {
v_b := p * 2 // want "v_b is used under score. You should use mixedCap or MixedCap."

return v_b // want "v_b is used under score. You should use mixedCap or MixedCap."
}

// local constant with underscore.
func fe(p int) int {
const v_b = 2 // want "v_b is used under score. You should use mixedCap or MixedCap."

return v_b * p // want "v_b is used under score. You should use mixedCap or MixedCap."
}

// local variable with underscore.
func ff(p int) int {
var v_b = 2 // want "v_b is used under score. You should use mixedCap or MixedCap."

return v_b * p // want "v_b is used under score. You should use mixedCap or MixedCap."
}

// inner function, parameter name with underscore.
func fg() {
fgl := func(p_a int) {} // want "p_a is used under score. You should use mixedCap or MixedCap."
fgl(1)
}

type Foo struct{}

// method name with underscore.
func (f Foo) f_a() {} // want "f_a is used under score. You should use mixedCap or MixedCap."

// method's parameter name with underscore.
func (f Foo) fb(p_a int) {} // want "p_a is used under score. You should use mixedCap or MixedCap."

// named return with underscore.
func (f Foo) fc() (r_b int) { return 0 } // want "r_b is used under score. You should use mixedCap or MixedCap."

// local variable (short declaration) with underscore.
func (f Foo) fd(p int) int {
v_b := p * 2 // want "v_b is used under score. You should use mixedCap or MixedCap."

return v_b // want "v_b is used under score. You should use mixedCap or MixedCap."
}

// local constant with underscore.
func (f Foo) fe(p int) int {
const v_b = 2 // want "v_b is used under score. You should use mixedCap or MixedCap."

return v_b * p // want "v_b is used under score. You should use mixedCap or MixedCap."
}

func c() (c_c int) {
c_c = 1
return c_c
// local variable with underscore.
func (f Foo) ff(p int) int {
var v_b = 2 // want "v_b is used under score. You should use mixedCap or MixedCap."

return v_b * p // want "v_b is used under score. You should use mixedCap or MixedCap."
}

func d() {
var d_d int
_ = d_d // It's never detected, because `_` is meaningful in Go and `d_d` is already detected.
func fna(a, p_a int) {} // want "p_a is used under score. You should use mixedCap or MixedCap."

func fna1(a string, p_a int) {} // want "p_a is used under score. You should use mixedCap or MixedCap."

func fnb(a, b, p_a int) {} // want "p_a is used under score. You should use mixedCap or MixedCap."

func fnb1(a, b string, p_a int) {} // want "p_a is used under score. You should use mixedCap or MixedCap."

func fnd(
p_a int, // want "p_a is used under score. You should use mixedCap or MixedCap."
p_b int, // want "p_b is used under score. You should use mixedCap or MixedCap."
p_c int, // want "p_c is used under score. You should use mixedCap or MixedCap."
) {
}
```

```console
go vet -vettool=(which nosnakecase) ./...

# command-line-arguments
./main.go:3:6: a_ is used under score. You should use mixedCap or MixedCap.
./main.go:6:8: a_a is used under score. You should use mixedCap or MixedCap.
./main.go:10:2: c_c is used under score. You should use mixedCap or MixedCap.
./main.go:11:9: c_c is used under score. You should use mixedCap or MixedCap.
./main.go:9:11: c_c is used under score. You should use mixedCap or MixedCap.
./main.go:15:6: d_d is used under score. You should use mixedCap or MixedCap.
# a
./a.go:4:5: v_v is used under score. You should use mixedCap or MixedCap.
./a.go:7:7: c_c is used under score. You should use mixedCap or MixedCap.
./a.go:10:6: S_a is used under score. You should use mixedCap or MixedCap.
./a.go:16:2: fi_a is used under score. You should use mixedCap or MixedCap.
./a.go:21:11: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:26:2: Fi_A is used under score. You should use mixedCap or MixedCap.
./a.go:31:19: r_a is used under score. You should use mixedCap or MixedCap.
./a.go:35:6: I_a is used under score. You should use mixedCap or MixedCap.
./a.go:41:5: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:46:5: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:51:2: Fn_a is used under score. You should use mixedCap or MixedCap.
./a.go:56:8: r_a is used under score. You should use mixedCap or MixedCap.
./a.go:60:6: f_a is used under score. You should use mixedCap or MixedCap.
./a.go:63:9: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:66:12: r_b is used under score. You should use mixedCap or MixedCap.
./a.go:72:2: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:74:9: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:79:8: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:81:9: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:86:6: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:88:9: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:93:14: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:100:14: f_a is used under score. You should use mixedCap or MixedCap.
./a.go:103:17: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:106:20: r_b is used under score. You should use mixedCap or MixedCap.
./a.go:110:2: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:112:9: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:117:8: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:119:9: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:124:6: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:126:9: v_b is used under score. You should use mixedCap or MixedCap.
./a.go:129:13: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:131:21: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:133:16: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:135:24: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:138:2: p_a is used under score. You should use mixedCap or MixedCap.
./a.go:139:2: p_b is used under score. You should use mixedCap or MixedCap.
./a.go:140:2: p_c is used under score. You should use mixedCap or MixedCap.
```

## CI
Expand Down
72 changes: 3 additions & 69 deletions nosnakecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"go/ast"
"go/token"
"strings"
"sync"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
Expand All @@ -27,78 +26,13 @@ func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

nodeFilter := []ast.Node{
(*ast.FuncDecl)(nil),
(*ast.Ident)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.FuncDecl:
var wg sync.WaitGroup
wg.Add(4)

go func() {
defer wg.Done()
name := n.Name
report(pass, name.Pos(), name.Name)
}()

go func() {
defer wg.Done()
params := n.Type.Params
for _, param := range params.List {
for _, ident := range param.Names {
report(pass, ident.Pos(), ident.Name)
}
}
}()

go func() {
defer wg.Done()
results := n.Type.Results
if results == nil {
return
}
for _, list := range results.List {
for _, ident := range list.Names {
report(pass, ident.Pos(), ident.Name)
}
}
}()

go func() {
defer wg.Done()
if n.Body == nil {
return
}
for _, list := range n.Body.List {
switch l := list.(type) {
case *ast.AssignStmt:
for _, lh := range l.Lhs {
ident, ok := lh.(*ast.Ident)
if !ok {
continue
}
report(pass, ident.Pos(), ident.Name)
}
case *ast.DeclStmt:
gendecl, ok := l.Decl.(*ast.GenDecl)
if !ok {
continue
}
for _, spec := range gendecl.Specs {
valspec, ok := spec.(*ast.ValueSpec)
if !ok {
continue
}
for _, ident := range valspec.Names {
report(pass, ident.Pos(), ident.Name)
}
}
}
}
}()

wg.Wait()
case *ast.Ident:
report(pass, n.Pos(), n.Name)
}
})

Expand Down
Loading

0 comments on commit 83240da

Please sign in to comment.