Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir heredocs closing triple quotes (""") behavior

Tags:

elixir

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?

like image 658
Luca Fülbier Avatar asked Feb 02 '17 23:02

Luca Fülbier


1 Answers

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
like image 162
Dogbert Avatar answered Oct 13 '22 23:10

Dogbert