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

wip: syntax: flush heredocs before right parens #924

Closed
wants to merge 1 commit into from
Closed
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 syntax/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ func (p *Printer) rightParen(pos Pos) {
if !p.minify {
p.newlines(pos)
}
if len(p.pendingHdocs) > 0 {
p.flushHeredocs()
p.WriteByte('\n')
}
p.WriteByte(')')
p.wantSpace = spaceRequired
}
Expand Down
12 changes: 12 additions & 0 deletions syntax/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,18 @@ func TestPrintMinify(t *testing.T) {
},
samePrint("foo >bar 2>baz <etc"),
samePrint("<<-EOF\n$(a|b)\nEOF"),
{
"a=$(\n\tcat <<'EOF'\n hello\nEOF\n)",
"a=$(cat \\\n<<'EOF'\n hello\nEOF\n)",
},
{
"(\n\tcat <<EOF\n hello\nEOF\n)",
"(cat \\\n<<EOF\n hello\nEOF\n)",
},
{
"diff -y <(cat <<EOF\n1\n2\n3\nEOF\n) <(cat <<EOF\n1\n4\n3\nEOF\n)",
"diff -y <(cat \\\n<<EOF\n1\n2\n3\nEOF\n) <(\ncat <<EOF\n1\n4\n3\nEOF\n)",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something I realized while writing these examples is minified heredocs might not necessarily be minified! For example the following is 21 bytes:

a=$(cat <<EOF
EOF
)

And produces the following 22 bytes:

a=$(cat \
<<EOF
EOF
)

It's because of the cat \, but digging through the code I couldn't immediately see what causing that, or how to address it without breaking too much else. In the meantime, I figure valid shell is a lot better than invalid shell though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The diff example in the test is very funky, because the minified code has the first cat continuing its <<EOF onto the next line, but the next cat in the second proc subst is left alone as cat <<EOF.

},
}
parser := NewParser(KeepComments(true))
printer := NewPrinter(Minify(true))
Expand Down