Skip to content

Commit

Permalink
syntax: fix whitespace on nested subshells
Browse files Browse the repository at this point in the history
we want white space on nested subshells if its in a single line since
its ambiguous, eg we want `( (` over `((`

this shouldn't be the case for multiple lines- fix the logic that adds
white space to only do so if the two subshells are in the same line

fixes mvdan#814
  • Loading branch information
riacataquian committed May 31, 2022
1 parent 4c7f3cb commit 712b809
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/shfmt/in.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

# OK - no blank
( (echo "Hello world!") )

# nok - blank ^ after first (
(
(echo "Hello world!")
)
7 changes: 7 additions & 0 deletions syntax/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,13 @@ func (p *Printer) command(cmd Command, redirs []*Redirect) (startRedirs int) {
p.WriteByte('(')
if len(x.Stmts) > 0 && startsWithLparen(x.Stmts[0]) {
p.wantSpace = spaceRequired
if nested, ok := x.Stmts[0].Cmd.(*Subshell); ok {
// we only want to keep the space between two nested subshells' open brackets
// if its in a single line to avoid ambiguity
if x.Lparen.Line() != nested.Pos().Line() {
p.wantSpace = spaceNotRequired
}
}
} else {
p.wantSpace = spaceNotRequired
}
Expand Down
8 changes: 8 additions & 0 deletions syntax/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,14 @@ var printTests = []printCase{
"`declare`",
"$(declare)",
},
{
"(\n(foo >redir)\n)",
"(\n\t(foo >redir)\n)",
},
{
"( (foo >redir) )",
"( (foo >redir))",
},
}

func TestPrintWeirdFormat(t *testing.T) {
Expand Down

0 comments on commit 712b809

Please sign in to comment.