Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions spec/ameba/rule/style/heredoc_escape_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,33 @@ module Ameba::Rule::Style
end

it "fails if a heredoc contains escaped interpolation" do
expect_issue subject, <<-'CRYSTAL'
source = expect_issue subject, <<-'CRYSTAL'
<<-HEREDOC
# ^^^^^^^^ error: Use an escaped heredoc marker: `<<-'HEREDOC'`
foo \#{:bar}
HEREDOC
CRYSTAL

expect_correction source, <<-'CRYSTAL'
<<-'HEREDOC'
foo \#{:bar}
HEREDOC
CRYSTAL
end

it "fails if a heredoc contains escaped interpolation and escaped escape sequences" do
expect_issue subject, <<-'CRYSTAL'
source = expect_issue subject, <<-'CRYSTAL'
<<-HEREDOC
# ^^^^^^^^ error: Use an escaped heredoc marker: `<<-'HEREDOC'`
foo \\t \#{:bar}
HEREDOC
CRYSTAL

expect_correction source, <<-'CRYSTAL'
<<-'HEREDOC'
foo \\t \#{:bar}
HEREDOC
CRYSTAL
end

it "passes if a heredoc contains normal and escaped escape sequences" do
Expand All @@ -71,12 +83,18 @@ module Ameba::Rule::Style
end

it "fails if a heredoc contains escaped escape sequences" do
expect_issue subject, <<-'CRYSTAL'
source = expect_issue subject, <<-'CRYSTAL'
<<-HEREDOC
# ^^^^^^^^ error: Use an escaped heredoc marker: `<<-'HEREDOC'`
\\t \\n
HEREDOC
CRYSTAL

expect_correction source, <<-'CRYSTAL'
<<-'HEREDOC'
\\t \\n
HEREDOC
CRYSTAL
end

it "passes if an escaped heredoc contains interpolation" do
Expand Down Expand Up @@ -123,12 +141,18 @@ module Ameba::Rule::Style
end

it "fails if an escaped heredoc doesn't contain interpolation" do
expect_issue subject, <<-CRYSTAL
source = expect_issue subject, <<-CRYSTAL
<<-'HEREDOC'
# ^^^^^^^^^^ error: Use an unescaped heredoc marker: `<<-HEREDOC`
foo
HEREDOC
CRYSTAL

expect_correction source, <<-CRYSTAL
<<-HEREDOC
foo
HEREDOC
CRYSTAL
end
end
end
10 changes: 8 additions & 2 deletions src/ameba/rule/style/heredoc_escape.cr
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,27 @@ module Ameba::Rule::Style
return unless code = node_source(node, source.lines)
return unless code.starts_with?("<<-")

body = code.lines[1..-2].join('\n')
lines = code.lines
body = lines[1..-2].join('\n')

if code.starts_with?("<<-'")
return if has_escape_sequence?(expr.value) || has_escaped_escape_sequence?(body)

marker = code.lchop("<<-'").match!(/^(\w+)/)[1]
msg = MSG_ESCAPE_NOT_NEEDED % marker
corrected_marker = "<<-#{marker}"
else
return if !has_escape_sequence?(expr.value) || has_escape_sequence?(body)

marker = code.lchop("<<-").match!(/^(\w+)/)[1]
msg = MSG_ESCAPE_NEEDED % marker
corrected_marker = "<<-'#{marker}'"
end

issue_for node, msg
issue_for node, msg do |corrector|
lines[0] = lines[0].sub(/^<<-'?#{marker}'?/, corrected_marker)
corrector.replace(node, lines.join('\n'))
end
end

private def has_escape_sequence?(value : String)
Expand Down
Loading