Sha256: e473e24c7fce3cb3943166dc8b1eb665e7041e2af4652c6cdac982467778b799

Contents?: true

Size: 1.27 KB

Versions: 48

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop identifies places where `lstrip.rstrip` can be replaced by
      # `strip`.
      #
      # @example
      #   # bad
      #   'abc'.lstrip.rstrip
      #   'abc'.rstrip.lstrip
      #
      #   # good
      #   'abc'.strip
      class Strip < Cop
        include RangeHelp

        MSG = 'Use `strip` instead of `%<methods>s`.'

        def_node_matcher :lstrip_rstrip, <<~PATTERN
          {(send $(send _ $:rstrip) $:lstrip)
           (send $(send _ $:lstrip) $:rstrip)}
        PATTERN

        def on_send(node)
          lstrip_rstrip(node) do |first_send, method_one, method_two|
            range = range_between(first_send.loc.selector.begin_pos,
                                  node.source_range.end_pos)
            add_offense(node,
                        location: range,
                        message: format(MSG,
                                        methods: "#{method_one}.#{method_two}"))
          end
        end

        def autocorrect(node)
          range = range_between(node.receiver.loc.selector.begin_pos,
                                node.source_range.end_pos)

          ->(corrector) { corrector.replace(range, 'strip') }
        end
      end
    end
  end
end

Version data entries

48 entries across 37 versions & 5 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/strip.rb
rubocop-0.89.1 lib/rubocop/cop/style/strip.rb
rubocop-0.89.0 lib/rubocop/cop/style/strip.rb
rubocop-0.88.0 lib/rubocop/cop/style/strip.rb
rbhint-0.87.1.rc1 lib/rubocop/cop/style/strip.rb
rubocop-0.87.1 lib/rubocop/cop/style/strip.rb
rubocop-0.87.0 lib/rubocop/cop/style/strip.rb
rubocop-0.86.0 lib/rubocop/cop/style/strip.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/rubocop-0.85.1/lib/rubocop/cop/style/strip.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/style/strip.rb
rbhint-0.85.1.rc1 lib/rubocop/cop/style/strip.rb
rubocop-0.85.1 lib/rubocop/cop/style/strip.rb
rbhint-0.8.5.rc1 lib/rubocop/cop/style/strip.rb
rubocop-0.85.0 lib/rubocop/cop/style/strip.rb
rubocop-0.84.0 lib/rubocop/cop/style/strip.rb
rubocop-0.83.0 lib/rubocop/cop/style/strip.rb
rubocop-0.82.0 lib/rubocop/cop/style/strip.rb
rubocop-0.81.0 lib/rubocop/cop/style/strip.rb
rubocop-0.80.1 lib/rubocop/cop/style/strip.rb
rubocop-0.80.0 lib/rubocop/cop/style/strip.rb