Sha256: b7725a8763d41428d1974d87abe61d9085cd13dfc830c01eba232060e78cc0a2

Contents?: true

Size: 1.82 KB

Versions: 13

Compression:

Stored size: 1.82 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # This cop checks for double `#start_with?` or `#end_with?` calls
      # separated by `||`. In some cases such calls can be replaced
      # with an single `#start_with?`/`#end_with?` call.
      #
      # @example
      #
      #   @bad
      #   str.start_with?("a") || str.start_with?(Some::CONST)
      #   str.start_with?("a", "b") || str.start_with?("c")
      #   var1 = ...
      #   var2 = ...
      #   str.end_with?(var1) || str.end_with?(var2)
      #
      #   @good
      #   str.start_with?("a", Some::CONST)
      #   str.start_with?("a", "b", "c")
      #   var1 = ...
      #   var2 = ...
      #   str.end_with?(var1, var2)
      class DoubleStartEndWith < Cop
        MSG = 'Use `%{receiver}.%{method}(%{combined_args})` ' \
              'instead of `%{original_code}`.'.freeze

        def on_or(node)
          receiver,
          method,
          first_call_args,
          second_call_args = two_start_end_with_calls(node)

          if receiver && second_call_args.all?(&:pure?)
            add_offense(
              node,
              :expression,
              format(
                MSG,
                receiver: receiver.source,
                method: method,
                combined_args: combine_args(first_call_args, second_call_args),
                original_code: node.source
              )
            )
          end
        end

        private

        def combine_args(first_call_args, second_call_args)
          (first_call_args + second_call_args).map(&:source).join(', ')
        end

        def_node_matcher :two_start_end_with_calls, <<-END
          (or
            (send $_recv [{:start_with? :end_with?} $_method] $...)
            (send _recv _method $...))
        END
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/double_start_end_with.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.42.0 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.41.2 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.41.1 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.41.0 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.40.0 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.39.0 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.38.0 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.37.2 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.37.1 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.37.0 lib/rubocop/cop/performance/double_start_end_with.rb
rubocop-0.36.0 lib/rubocop/cop/performance/double_start_end_with.rb