I noticed that the following two calls produce a different result string:
# Closing quotes not indented
"""
a
"""
# Closing quotes indented
"""
a
"""
The first call will return " a\n"
, while the second one returns a\n
.
It seems as if the indentation level of the closing quotes indicate a point up until the leading whitespace is truncated for every line in the heredoc. If you have 8 leading whitespaces and an ending quote indentation of 4 you would end up with 4 leading whitespaces in the resulting string. Characters and everything after the first actual character is not truncated.
I did not find any documentation on that behavior in the Elixir docs. Is it a bug?
I did not find any documentation about this but this definitely intentional as a commit with subject
Allow heredocs to be aligned according to the position the heredoc end.
was committed by José Valim on 20 Feb 2012 and it includes a new function in elixir_tokenizer
with the comment:
%% Remove spaces from heredoc based on the position of the final quotes.
and a test case similar to the one you wrote in the question:
test :double_quoted_aligned_heredoc do
assert_equal "foo\nbar\nbar\n", """ <> "bar\n"
foo
bar
"""
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With