Sha256: 3d3b2e9acd5ef9cb33f4d7a958b5428d886ac4942b70fde17b445343216e8ca4

Contents?: true

Size: 1.13 KB

Versions: 49

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # Identifies places where `lstrip.rstrip` can be replaced by
      # `strip`.
      #
      # @example
      #   # bad
      #   'abc'.lstrip.rstrip
      #   'abc'.rstrip.lstrip
      #
      #   # good
      #   'abc'.strip
      class Strip < Base
        include RangeHelp
        extend AutoCorrector

        MSG = 'Use `strip` instead of `%<methods>s`.'
        RESTRICT_ON_SEND = %i[lstrip rstrip].freeze

        # @!method lstrip_rstrip(node)
        def_node_matcher :lstrip_rstrip, <<~PATTERN
          {
            (call $(call _ :rstrip) :lstrip)
            (call $(call _ :lstrip) :rstrip)
          }
        PATTERN

        def on_send(node)
          lstrip_rstrip(node) do |first_send|
            range = range_between(first_send.loc.selector.begin_pos, node.source_range.end_pos)
            message = format(MSG, methods: range.source)

            add_offense(range, message: message) do |corrector|
              corrector.replace(range, 'strip')
            end
          end
        end
        alias on_csend on_send
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 10 rubygems

Version Path
rubocop-1.74.0 lib/rubocop/cop/style/strip.rb
rubocop-1.73.2 lib/rubocop/cop/style/strip.rb
siteimprove_api_client-1.0.1 vendor/bundle/ruby/3.2.0/gems/rubocop-1.73.1/lib/rubocop/cop/style/strip.rb
rubocop-1.73.1 lib/rubocop/cop/style/strip.rb
rubocop-1.73.0 lib/rubocop/cop/style/strip.rb
rubocop-1.72.2 lib/rubocop/cop/style/strip.rb
rubocop-1.72.1 lib/rubocop/cop/style/strip.rb
rubocop-1.72.0 lib/rubocop/cop/style/strip.rb
rubocop-1.71.2 lib/rubocop/cop/style/strip.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-1.71.1/lib/rubocop/cop/style/strip.rb
rubocop-1.71.1 lib/rubocop/cop/style/strip.rb
rubocop-1.71.0 lib/rubocop/cop/style/strip.rb
rubocop-1.70.0 lib/rubocop/cop/style/strip.rb
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/style/strip.rb
rubocop-1.69.2 lib/rubocop/cop/style/strip.rb
rubocop-1.69.1 lib/rubocop/cop/style/strip.rb
rubocop-1.69.0 lib/rubocop/cop/style/strip.rb
rubocop-1.68.0 lib/rubocop/cop/style/strip.rb
rubocop-1.67.0 lib/rubocop/cop/style/strip.rb
rubocop-1.66.1 lib/rubocop/cop/style/strip.rb