Skip to content

Commit

Permalink
remove newlines immediately after an assignment operator
Browse files Browse the repository at this point in the history
Fixes #76.
  • Loading branch information
kaperys authored Oct 13, 2021
1 parent 4d8e76d commit f3595ed
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,25 @@ The added formatting rules are in the `format` package.

### Added rules

No empty lines following an assignment operator

<details><summary><i>example</i></summary>

```
func foo() {
foo :=
"bar"
}
```

```
func foo() {
foo := "bar"
}
```

</details>

No empty lines at the beginning or end of a function

<details><summary><i>example</i></summary>
Expand Down
4 changes: 4 additions & 0 deletions format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,10 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
c.Replace(node)
}
}

case *ast.AssignStmt:
// Only remove lines between the assignment token and the first right-hand side expression
f.removeLines(f.Line(node.TokPos), f.Line(node.Rhs[0].Pos()))
}
}

Expand Down
60 changes: 60 additions & 0 deletions testdata/scripts/assignment-newlines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
gofumpt -w foo.go
cmp foo.go foo.go.golden

gofumpt -d foo.go.golden
! stdout .

-- foo.go --
package p

func f() {
foo :=


"bar"

foo :=
"bar"

_, _ =
0,
1

_, _ = 0,
1

_ =
`
foo
`

_ = /* inline */
"foo"

_ = // inline
"foo"
}

-- foo.go.golden --
package p

func f() {
foo := "bar"

foo := "bar"

_, _ = 0,
1

_, _ = 0,
1

_ = `
foo
`

_ = /* inline */ "foo"

_ = // inline
"foo"
}

0 comments on commit f3595ed

Please sign in to comment.