class RuboCop::Cop::Style::EmptyHeredoc
Checks for using empty heredoc to reduce redundancy.
@example
# bad <<~EOS EOS <<-EOS EOS <<EOS EOS # good '' # bad do_something(<<~EOS) EOS do_something(<<-EOS) EOS do_something(<<EOS) EOS # good do_something('')
Constants
- MSG
Public Instance Methods
on_heredoc(node)
click to toggle source
# File lib/rubocop/cop/style/empty_heredoc.rb, line 43 def on_heredoc(node) heredoc_body = node.loc.heredoc_body return unless heredoc_body.source.empty? add_offense(node) do |corrector| heredoc_end = node.loc.heredoc_end corrector.replace(node, preferred_string_literal) corrector.remove(range_by_whole_lines(heredoc_body, include_final_newline: true)) corrector.remove(range_by_whole_lines(heredoc_end, include_final_newline: true)) end end
Private Instance Methods
enforce_double_quotes?()
click to toggle source
# File lib/rubocop/cop/style/empty_heredoc.rb, line 63 def enforce_double_quotes? string_literals_config['EnforcedStyle'] == 'double_quotes' end
preferred_string_literal()
click to toggle source
# File lib/rubocop/cop/style/empty_heredoc.rb, line 59 def preferred_string_literal enforce_double_quotes? ? '""' : "''" end
string_literals_config()
click to toggle source
# File lib/rubocop/cop/style/empty_heredoc.rb, line 67 def string_literals_config config.for_cop('Style/StringLiterals') end