From 712b809316401c2e13339514430035a6b4a1b674 Mon Sep 17 00:00:00 2001 From: riacataquian Date: Tue, 31 May 2022 19:30:24 +0800 Subject: [PATCH] syntax: fix whitespace on nested subshells 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 #814 --- cmd/shfmt/in.sh | 9 +++++++++ syntax/printer.go | 7 +++++++ syntax/printer_test.go | 8 ++++++++ 3 files changed, 24 insertions(+) create mode 100644 cmd/shfmt/in.sh diff --git a/cmd/shfmt/in.sh b/cmd/shfmt/in.sh new file mode 100644 index 000000000..7a32449f6 --- /dev/null +++ b/cmd/shfmt/in.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# OK - no blank +( (echo "Hello world!") ) + +# nok - blank ^ after first ( +( + (echo "Hello world!") +) diff --git a/syntax/printer.go b/syntax/printer.go index 7dc183a02..cbb310c83 100644 --- a/syntax/printer.go +++ b/syntax/printer.go @@ -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 } diff --git a/syntax/printer_test.go b/syntax/printer_test.go index 59483f689..4e12e6113 100644 --- a/syntax/printer_test.go +++ b/syntax/printer_test.go @@ -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) {