language-bash icon indicating copy to clipboard operation
language-bash copied to clipboard

Pretty printing of here documents without trailing newline

Open mmhat opened this issue 6 years ago • 6 comments

Consider the following here document:

Heredoc Here "EOF" False (stringToWord "here document")

If pretty printed it looks like that:

<<EOF
here documentEOF

This is clearly not the intended result. Maybe we should add a trailing newline if it is not present in the Word being rendered.

mmhat avatar Nov 17 '19 19:11 mmhat

I could implement that if we agree on a solution.

mmhat avatar Nov 17 '19 19:11 mmhat

Hmm right now parse(prettyPrint(ast)) == ast for all possible trees (or at least that's the goal) but adding a newline would break that. Is it ever possible for a heredoc to not end in a newline in bash? Maybe we should comment that on the Heredoc constructor instead. What do you think?

knrafto avatar Nov 18 '19 00:11 knrafto

I'm not yet sure about that. I stumbled upon that issue while writing a package for shell script generation in the spirit of shell-monad. I do think it would be nice to force the EOF marker on a separate line because otherwise bash will extend the end of file which could lead to some really unintended results. Indeed it is IMHO not possible in bash to produce a heredoc without trailing newline. I'm not sure why a newline list terminator would break the assertion you mentioned above; can you elaborate a bit more on that?

mmhat avatar Nov 18 '19 00:11 mmhat

I implemented a solution here. Didn't test it though.

mmhat avatar Nov 18 '19 01:11 mmhat

You're right, it won't break that assertion. The solution looks good to me, feel free to turn it into a pull request.

knrafto avatar Nov 18 '19 01:11 knrafto

Ok, I'll do that. I'll wait until #24 gets merged.

mmhat avatar Nov 18 '19 05:11 mmhat