Skip to content

Commit

Permalink
Extend support for allow cuddle declaration. (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed May 20, 2020
1 parent 2023abc commit 1f8b44c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions wsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,10 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
continue
}

if _, ok := previousStatement.(*ast.DeclStmt); ok && p.config.AllowCuddleDeclaration {
continue
}

// If the assignment is from a type or variable called on the line
// above we can allow it by setting AllowAssignAndCallCuddle to
// true.
Expand All @@ -505,6 +509,10 @@ func (p *Processor) parseBlockStatements(statements []ast.Stmt) {
case *ast.ExprStmt:
switch previousStatement.(type) {
case *ast.DeclStmt, *ast.ReturnStmt:
if p.config.AllowAssignAndCallCuddle && p.config.AllowCuddleDeclaration {
continue
}

p.addError(t.Pos(), reasonExpressionCuddledWithDeclOrRet)
case *ast.IfStmt, *ast.RangeStmt, *ast.SwitchStmt:
p.addError(t.Pos(), reasonExpressionCuddledWithBlock)
Expand Down
49 changes: 49 additions & 0 deletions wsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,55 @@ func TestWithConfig(t *testing.T) {
}`),
expectedErrorStrings: []string{reasonOnlyCuddleIfWithAssign},
},
{
description: "allow cuddling of declaration (#83)",
code: []byte(`package main
func main() {
var first = 1
var second = 2
var config Configuration
if err := conf.Load(config); err != nil {
panic(err)
}
var config Configuration
conf.Load(&config)
var config Configuration
err = json.Unmarshal(body, &config)
var notUsed bool
err = json.Unmarshal(body, &something)
var x = 1
if x > 0 {
// ...
}
// This one fails because we're not using y in the if statement.
var y = 1
if first > 0 {
// ...
}
// This one fails because we cuddled too many
var z = 1
var zz = 2
if zz > z {
// ...
}
}`),
customConfig: &Configuration{
AllowAssignAndCallCuddle: true,
AllowCuddleDeclaration: true,
},
expectedErrorStrings: []string{
reasonOnlyCuddleWithUsedAssign,
reasonOnlyOneCuddle,
},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 1f8b44c

Please sign in to comment.