Sha256: 0f73847938d3f4e2eca9cdcca4b3bcf7022a9601c3f15b475b127792248647b6

Contents?: true

Size: 1.22 KB

Versions: 1

Compression:

Stored size: 1.22 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
        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, "''")
            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

1 entries across 1 versions & 1 rubygems

Version Path
rubocop-1.32.0 lib/rubocop/cop/style/empty_heredoc.rb