Class: RuboCop::Cop::Style::RedundantHeredocDelimiterQuotes

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
Heredoc
Defined in:
lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb

Overview

Checks for redundant heredoc delimiter quotes.

Examples:


# 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

Constant Summary collapse

MSG =
'Remove the redundant heredoc delimiter quotes, use `%<replacement>s` instead.'
STRING_INTERPOLATION_OR_ESCAPED_CHARACTER_PATTERN =
/#(\{|@|\$)|\\/.freeze

Constants included from Heredoc

Heredoc::OPENING_DELIMITER

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods included from Heredoc

#on_str

Instance Method Details

#on_heredoc(node) ⇒ Object



36
37
38
39
40
41
42
43
44
# 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