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
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.65.1 lib/rubocop/cop/style/empty_heredoc.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.65.0 lib/rubocop/cop/style/empty_heredoc.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/empty_heredoc.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/empty_heredoc.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/style/empty_heredoc.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.64.1 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.63.4 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.63.3 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.63.2 lib/rubocop/cop/style/empty_heredoc.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.57.2/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.63.1 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.63.0 lib/rubocop/cop/style/empty_heredoc.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.62.1 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.62.0 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.61.0 lib/rubocop/cop/style/empty_heredoc.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.60.2/lib/rubocop/cop/style/empty_heredoc.rb