Sha256: 1af36ecf277bee23656a888dbe5f1bf4d4a656e9145dff1cc8a8817956e5a1a6

Contents?: true

Size: 1.32 KB

Versions: 43

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Rails
      # Enforces the use of squiggly heredoc over `strip_heredoc`.
      #
      # @example
      #
      #   # bad
      #   <<EOS.strip_heredoc
      #     some text
      #   EOS
      #
      #   # bad
      #   <<-EOS.strip_heredoc
      #     some text
      #   EOS
      #
      #   # good
      #   <<~EOS
      #     some text
      #   EOS
      #
      class StripHeredoc < Base
        extend AutoCorrector
        extend TargetRubyVersion

        MSG = 'Use squiggly heredoc (`<<~`) instead of `strip_heredoc`.'
        RESTRICT_ON_SEND = %i[strip_heredoc].freeze

        minimum_target_ruby_version 2.3

        def on_send(node)
          return unless (receiver = node.receiver)
          return unless receiver.str_type? || receiver.dstr_type?
          return unless receiver.respond_to?(:heredoc?) && receiver.heredoc?

          register_offense(node, receiver)
        end

        private

        def register_offense(node, heredoc)
          add_offense(node) do |corrector|
            squiggly_heredoc = heredoc.source.sub(/\A<<(-|~)?/, '<<~')

            corrector.replace(heredoc, squiggly_heredoc)
            corrector.remove(node.loc.dot)
            corrector.remove(node.loc.selector)
          end
        end
      end
    end
  end
end

Version data entries

43 entries across 40 versions & 7 rubygems

Version Path
rubocop-rails-2.27.0 lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.26.2 lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.26.1 lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.26.0 lib/rubocop/cop/rails/strip_heredoc.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.25.1/lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.25.1 lib/rubocop/cop/rails/strip_heredoc.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/strip_heredoc.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/strip_heredoc.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/strip_heredoc.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-rails-2.25.0/lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.24.1 lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.24.0 lib/rubocop/cop/rails/strip_heredoc.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-rails-2.23.1/lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.23.1 lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.23.0 lib/rubocop/cop/rails/strip_heredoc.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.15.2/lib/rubocop/cop/rails/strip_heredoc.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-rails-2.20.0/lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.22.2 lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.22.1 lib/rubocop/cop/rails/strip_heredoc.rb
rubocop-rails-2.22.0 lib/rubocop/cop/rails/strip_heredoc.rb