Elixir heredocs 关闭三重引号 (""") 行为

Luc*_*ier 5 elixir

我注意到以下两个调用产生了不同的结果字符串:

# Closing quotes not indented
"""
 a
"""

# Closing quotes indented
"""
 a
 """
Run Code Online (Sandbox Code Playgroud)

第一个调用将返回" a\n",而第二个调用将返回a\n

似乎结束引号的缩进级别指示一个点,直到为heredoc中的每一行截断前导空格为止。如果您有 8 个前导空格和 4 的结束引号缩进,则结果字符串中将有 4 个前导空格。字符和第一个实际字符之后的所有内容都不会被截断。

我在 Elixir 文档中没有找到关于该行为的任何文档。这是一个错误吗?

Dog*_*ert 6

我没有找到任何关于此的文档,但这绝对是故意作为主题提交

允许根据heredoc结束的位置对齐heredoc。

20月2012犯下何塞·Valim,它包括一个新的功能elixir_tokenizer与评论:

%% Remove spaces from heredoc based on the position of the final quotes.
Run Code Online (Sandbox Code Playgroud)

和一个类似于你在问题中写的测试用例:

test :double_quoted_aligned_heredoc do
  assert_equal "foo\nbar\nbar\n", """ <> "bar\n"
  foo
  bar
  """
end
Run Code Online (Sandbox Code Playgroud)