Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove newlines immediately after assignment operators #157

Merged
merged 1 commit into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}