Sha256: 768e6377ea084730a90026b7f1047243f2f836bd1045b345ff9fa738e2370d79

Contents?: true

Size: 1.56 KB

Versions: 175

Compression:

Stored size: 1.56 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, 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

        def preferred_string_literal
          enforce_double_quotes? ? '""' : "''"
        end

        def enforce_double_quotes?
          string_literals_config['EnforcedStyle'] == 'double_quotes'
        end

        def string_literals_config
          config.for_cop('Style/StringLiterals')
        end
      end
    end
  end
end

Version data entries

175 entries across 170 versions & 18 rubygems

Version Path
rubocop-1.60.2 lib/rubocop/cop/style/empty_heredoc.rb
study_line-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/empty_heredoc.rb
study_line-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/empty_heredoc.rb
study_line-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/empty_heredoc.rb
study_line-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/empty_heredoc.rb
study_line-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/empty_heredoc.rb
study_line-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.60.1 lib/rubocop/cop/style/empty_heredoc.rb
study_line-0.2.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/empty_heredoc.rb
study_line-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.0/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.60.0 lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
harbr-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb