wsl: Support to cuddle assignments for a whole block

Right now you have to use the variable, type or field in the first statement in a block if you want to use it like this:

variable := "assigned"
[token] {
    // Must use 'variable' here
}

I think this is reasonable for most of the time but I kind of like to be able to use this pattern, especially if it’s spawning a go routine like this:

done := make(chan struct{})
go func() {
    fmt.Println("Let's spawn this go routine")
    <-somethingBlocking

    close(done)
}()

// Wait for done
<-done

This might be useful in more scenarios like ranges, if-statements etcetera and therefore could possible be configured. Although as of now the only place I really felt that this was needed was a very few go routines.

Since this linter isn’t really embrarcing any kind of official best practices like pythons black I think it’s ok to have the linter heavily configurable like e.g. perltidy.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 1
  • Comments: 16 (10 by maintainers)

Most upvoted comments

Another one of those super old issues finally getting resolved, this time thanks to @jacobdrury! Thank you so much!! 🎉 😍

If you want this now, you can install wsl from master: go install github.com/bombsimon/wsl/v4/cmd/wsl@master.

However I’ll release a new version and once dependabot picks it up add the new option to golangci-lint. Keeping this issue open until that’s done.

It’s very annoying that I have to add new line, just because I’ve added a condition.

var out []string
for i := range in {
    out = append(out, in[i])
}
var out []string // NOT OK, new line is required

for i := range in {
  if in[i] == "whatever" {
    out = append(out, in[i])
  }
}

Ah, I see! I think it would make sense to disable it completely, so meaning either you have to use it in the block or first in statement like now, or the lint is off and you can use it wherever.

Yes, cuddling of variable assignments with a followup block should be possible if they are then used somewhere (anywhere) inside the block. To me this is the main obstacle with wsl and it makes code less readable by requiring a newline in there.

The idea is to be able to use the cuddled variable wherever you want in the block. Instead of just this:

counter := 0
if somethingTrue {
    counter++
}

I also think it should be possible to configure it like this:

counter := 0
if somethingTrue {
    checker := getAChecker()
    if !checker {
        return
    }

    counter++ // Cuddled variable used in block, but not as first statement
}