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
synctera_ruby_sdk-1.1.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/empty_heredoc.rb
synctera_ruby_sdk-1.1.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/empty_heredoc.rb
synctera_ruby_sdk-1.1.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/empty_heredoc.rb
sampero-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.1/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.56.1 lib/rubocop/cop/style/empty_heredoc.rb
tursodb-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/empty_heredoc.rb
synctera_ruby_sdk-1.0.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.56.0/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.56.0 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.55.1 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.55.0 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.54.2 lib/rubocop/cop/style/empty_heredoc.rb
mlh-rubocop-config-1.0.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/empty_heredoc.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/empty_heredoc.rb
fablicop-1.10.3 vendor/bundle/ruby/3.2.0/gems/rubocop-1.54.1/lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.54.1 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.54.0 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.53.1 lib/rubocop/cop/style/empty_heredoc.rb
rubocop-1.53.0 lib/rubocop/cop/style/empty_heredoc.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/style/empty_heredoc.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.52.1/lib/rubocop/cop/style/empty_heredoc.rb