Skip to content

Commit

Permalink
Improved decl
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Jan 6, 2025
1 parent f70204c commit e6d79b5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
5 changes: 5 additions & 0 deletions testdata/src/default_config/decl/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ func fn1() {
_ = a
_ = b
}

func fn2() {
var a = 1
var b = a // want "missing whitespace decreases readability"
}
6 changes: 6 additions & 0 deletions testdata/src/default_config/decl/decl.go.golden
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ func fn1() {
_ = a
_ = b
}

func fn2() {
var a = 1

var b = a // want "missing whitespace decreases readability"
}
37 changes: 25 additions & 12 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,20 @@ func (w *WSL) Run() {
}

func (w *WSL) CheckCuddling(stmt ast.Node, cursor *Cursor, maxAllowedStatements int) {
reset := cursor.Save()
defer reset()
w.checkCuddlingWithDecl(stmt, cursor, maxAllowedStatements, true)
}

func (w *WSL) CheckCuddlingNoDecl(stmt ast.Node, cursor *Cursor, maxAllowedStatements int) {
w.checkCuddlingWithDecl(stmt, cursor, maxAllowedStatements, false)
}

func (w *WSL) checkCuddlingWithDecl(
stmt ast.Node,
cursor *Cursor,
maxAllowedStatements int,
declIsValid bool,
) {
defer cursor.Save()()

currentIdents := allIdents(cursor.Stmt())
previousIdents := []*ast.Ident{}
Expand Down Expand Up @@ -137,6 +149,10 @@ func (w *WSL) CheckCuddling(stmt ast.Node, cursor *Cursor, maxAllowedStatements
_, prevIsIncDec := previousNode.(*ast.IncDecStmt)
_, currIsDefer := stmt.(*ast.DeferStmt)

if !declIsValid {
prevIsDecl = false
}

// We're cuddled but not with an assign, declare or defer statement which is
// never allowed.
if !prevIsAssign && !prevIsDecl && !currIsDefer && !prevIsIncDec {
Expand Down Expand Up @@ -402,12 +418,7 @@ func (w *WSL) CheckBranch(stmt *ast.BranchStmt, cursor *Cursor) {
return
}

w.addError(
stmt.Pos(),
stmt.Pos(),
stmt.Pos(),
MessageAddWhitespace,
)
w.addError(stmt.Pos(), stmt.Pos(), stmt.Pos(), MessageAddWhitespace)
}

func (w *WSL) CheckDecl(stmt *ast.DeclStmt, cursor *Cursor) {
Expand All @@ -419,7 +430,7 @@ func (w *WSL) CheckDecl(stmt *ast.DeclStmt, cursor *Cursor) {
return
}

w.CheckCuddling(stmt, cursor, 1)
w.CheckCuddlingNoDecl(stmt, cursor, 1)
}

func (w *WSL) CheckBlock(block *ast.BlockStmt) {
Expand All @@ -428,7 +439,6 @@ func (w *WSL) CheckBlock(block *ast.BlockStmt) {

cursor := NewCursor(-1, block.List)
for cursor.Next() {
// fmt.Printf("%d: %T\n", cursor.currentIdx, cursor.Stmt())
w.CheckStmt(cursor.Stmt(), cursor)
}
}
Expand Down Expand Up @@ -603,8 +613,7 @@ func (w *WSL) CheckExpr(expr ast.Expr, cursor *Cursor) {
// numberOfStatementsAbove will find out how many lines above the cursor's
// current statement there is without any newlines between.
func (w *WSL) numberOfStatementsAbove(cursor *Cursor) int {
reset := cursor.Save()
defer reset()
defer cursor.Save()()

statementsWithoutNewlines := 0
currentStmtStartLine := w.lineFor(cursor.Stmt().Pos())
Expand Down Expand Up @@ -785,6 +794,10 @@ func allIdents(node ast.Node) []*ast.Ident {
for _, name := range n.Names {
idents = append(idents, allIdents(name)...)
}

for _, value := range n.Values {
idents = append(idents, allIdents(value)...)
}
case *ast.AssignStmt:
// TODO: For TypeSwitchStatements, this can be a false positive by
// allowing shadowing and "tricking" usage;
Expand Down

0 comments on commit e6d79b5

Please sign in to comment.