class RuboCop::Cop::Style::RedundantHeredocDelimiterQuotes

Checks for redundant heredoc delimiter quotes.

@example

# bad
do_something(<<~'EOS')
  no string interpolation style text
EOS

# good
do_something(<<~EOS)
  no string interpolation style text
EOS

do_something(<<~'EOS')
  #{string_interpolation_style_text_not_evaluated}
EOS

do_something(<<~'EOS')
  Preserve \
  newlines
EOS

Constants

MSG
STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN

Public Instance Methods

on_heredoc(node) click to toggle source
# File lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb, line 36
def on_heredoc(node)
  return if need_heredoc_delimiter_quotes?(node)

  replacement = "#{heredoc_type(node)}#{delimiter_string(node)}"

  add_offense(node, message: format(MSG, replacement: replacement)) do |corrector|
    corrector.replace(node, replacement)
  end
end

Private Instance Methods

need_heredoc_delimiter_quotes?(node) click to toggle source
# File lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb, line 48
def need_heredoc_delimiter_quotes?(node)
  heredoc_delimiter = node.source.delete(heredoc_type(node))
  return true unless heredoc_delimiter.start_with?("'", '"')

  node.loc.heredoc_end.source.strip.match?(/\W/) ||
    node.loc.heredoc_body.source.match?(STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN)
end