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

Collapse empty call expressions #1308

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions build/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,10 @@ func (p *printer) useCompactMode(start *Position, list *[]Expr, end *End, mode s
if mode == modeSeq {
return true
}
// Use compact mode for empty call expressions if ForceMultiLine is not set
if mode == modeCall && len(*list) == 0 && !forceMultiLine {
return true
}

// In the Default and .bzl printing modes try to keep the original printing style.
// Non-top-level statements and lists of arguments of a function definition
Expand Down
15 changes: 15 additions & 0 deletions build/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ var rewrites = []struct {
{"formatdocstrings", formatDocstrings, scopeBoth},
{"reorderarguments", reorderArguments, scopeBoth},
{"editoctal", editOctals, scopeBoth},
{"collapseEmpty", collapseEmpty, scopeBoth},
}

// leaveAlone reports whether any of the nodes on the stack are marked
Expand Down Expand Up @@ -1429,3 +1430,17 @@ func removeParens(f *File, _ *Rewriter) {

Edit(f, simplify)
}

// collapseEmpty unsets ForceMultiLine for empty call expressions.
func collapseEmpty(f *File, _ *Rewriter) {
Walk(f, func(expr Expr, stack []Expr) {
c, ok := expr.(*CallExpr)
if !ok {
return
}

if len(c.List) == 0 { // No arguments
c.ForceMultiLine = false
}
})
}
18 changes: 18 additions & 0 deletions build/testdata/076.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Test collapse empty call expressions

func()

func()

# before comment
func()

func() # after comment

func(
# line comment
)

func(
name = "not_empty",
)
22 changes: 22 additions & 0 deletions build/testdata/076.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Test collapse empty call expressions

func(
)

func(
)

# before comment
func(
)

func(
) # after comment

func(
# line comment
)

func(
name = "not_empty",
)