Skip to content

Commit 88fac5c

Browse files
authored
syntax: don't join then/do with semicolon when heredocs are pending (#1320)
When a heredoc redirect appears in an if/while condition, semiOrNewl would place "; then" or "; do" on the same line as the redirect, producing hard-to-read output like: if cmd <<EOF; then body EOF foo fi Now force a newline when there are pending heredocs, so the heredoc body is flushed before the keyword: if cmd <<EOF body EOF then foo fi Fixes #1047.
1 parent b2c5d6c commit 88fac5c

2 files changed

Lines changed: 3 additions & 1 deletion

File tree

syntax/printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ func (p *Printer) spacedToken(s string, pos Pos) {
359359
}
360360

361361
func (p *Printer) semiOrNewl(s string, pos Pos) {
362-
if p.wantsNewline(Pos{}, false) {
362+
if p.wantsNewline(Pos{}, false) || len(p.pendingHdocs) > 0 {
363363
p.newline(pos)
364364
p.indent()
365365
} else {

syntax/printer_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ var printTests = []printCase{
113113
samePrint("<<EOF\nEOF"),
114114
samePrint("foo <<EOF\nEOF\n\nbar"),
115115
samePrint("foo <<'EOF'\nEOF\n\nbar"),
116+
samePrint("if cmd <<EOF\nbody\nEOF\nthen\n\tfoo\nfi"),
117+
samePrint("while cmd <<EOF\nbody\nEOF\ndo\n\tfoo\ndone"),
116118
{
117119
"{ foo; bar; }",
118120
"{\n\tfoo\n\tbar\n}",

0 commit comments

Comments
 (0)