Skip to content

Commit e8e6e80

Browse files
committed
Fix false positive for TrailingWhitespace
Refs #1203
1 parent d5f7f36 commit e8e6e80

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

lib/credo/code/strings.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ defmodule Credo.Code.Strings do
589589
delimiter,
590590
empty_line_replacement
591591
) do
592-
parse_heredoc(t, acc, heredoc_acc, replacement, delimiter, empty_line_replacement)
592+
parse_heredoc(t, acc, heredoc_acc <> "\\\"", replacement, delimiter, empty_line_replacement)
593593
end
594594

595595
defp parse_heredoc(

test/credo/check/readability/trailing_white_space_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,36 @@ defmodule Credo.Check.Readability.TrailingWhiteSpaceTest do
351351
|> refute_issues()
352352
end
353353

354+
test "it should NOT report escaped heredocs in regular heredocs" do
355+
~S'''
356+
defmodule Example do
357+
@moduledoc ~S"""
358+
config_yaml = \"\"\"
359+
apiVersion: v1
360+
\"\"\"
361+
"""
362+
end
363+
'''
364+
|> to_source_file
365+
|> run_check(@described_check)
366+
|> refute_issues()
367+
end
368+
369+
test "it should NOT report escaped heredocs in regular heredocs /2" do
370+
~S'''
371+
defmodule Example do
372+
@moduledoc ~S"""
373+
\"\"\"
374+
...
375+
\"\"\"
376+
"""
377+
end
378+
'''
379+
|> to_source_file
380+
|> run_check(@described_check)
381+
|> refute_issues()
382+
end
383+
354384
#
355385
# cases raising issues
356386
#

test/credo/code/heredocs_test.exs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,4 +744,30 @@ defmodule Credo.Code.HeredocsTest do
744744

745745
assert result == result2, "Heredocs.replace_with_spaces/2 should be idempotent"
746746
end
747+
748+
test "should treat heredoc sigils correctly (issue #1202)" do
749+
source =
750+
~S'''
751+
defmodule Example do
752+
@moduledoc ~S"""
753+
\"\"\"
754+
asdf
755+
\"\"\"
756+
"""
757+
end
758+
'''
759+
760+
expected =
761+
~S'''
762+
defmodule Example do
763+
@moduledoc ~S"""
764+
......
765+
....
766+
......
767+
"""
768+
end
769+
'''
770+
771+
assert expected == source |> Heredocs.replace_with_spaces(".", ".", ".")
772+
end
747773
end

0 commit comments

Comments
 (0)