Sha256: c88c203123d49d78e92cea0e682ecf9f1b92c4f238ed8883ae82b72eb326bed9

Contents?: true

Size: 1.27 KB

Versions: 8

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # 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('')
      #
      class EmptyHeredoc < Base
        include Heredoc
        include RangeHelp
        include StringLiteralsHelp
        extend AutoCorrector

        MSG = 'Use an empty string literal instead of heredoc.'

        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
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-1.70.0 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.69.2 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.69.1 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.69.0 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.68.0 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.67.0 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.66.1 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.66.0 lib/rubocop/cop/style/empty_heredoc.rb