Class: RuboCop::Cop::Performance::DoubleStartEndWith

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/performance/double_start_end_with.rb

Overview

Checks for consecutive ‘#start_with?` or `#end_with?` calls. These methods accept multiple arguments, so in some cases like when they are separated by `||`, they can be combined into a single method call.

‘IncludeActiveSupportAliases` configuration option is used to check for `starts_with?` and `ends_with?`. These methods are defined by Active Support.

Examples:

# bad
str.start_with?("a") || str.start_with?(Some::CONST)
str.start_with?("a", "b") || str.start_with?("c")
!str.start_with?(foo) && !str.start_with?(bar)
str.end_with?(var1) || str.end_with?(var2)

# good
str.start_with?("a", Some::CONST)
str.start_with?("a", "b", "c")
!str.start_with?(foo, bar)
str.end_with?(var1, var2)

IncludeActiveSupportAliases: false (default)

# good
str.starts_with?("a", "b") || str.starts_with?("c")
str.ends_with?(var1) || str.ends_with?(var2)

str.starts_with?("a", "b", "c")
str.ends_with?(var1, var2)

IncludeActiveSupportAliases: true

# bad
str.starts_with?("a", "b") || str.starts_with?("c")
str.ends_with?(var1) || str.ends_with?(var2)

# good
str.starts_with?("a", "b", "c")
str.ends_with?(var1, var2)

Constant Summary collapse

MSG =
'Use `%<replacement>s` instead of `%<original_code>s`.'
METHODS =
i[start_with? end_with?].to_set
METHODS_WITH_ACTIVE_SUPPORT =
i[start_with? starts_with? end_with? ends_with?].to_set

Instance Method Summary collapse

Instance Method Details

#on_and(node) ⇒ Object



57
58
59
60
61
# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 57

def on_and(node)
  two_start_end_with_calls_negated(node, methods_to_check: methods) do |*matched|
    check(node, *matched)
  end
end

#on_or(node) ⇒ Object



51
52
53
54
55
# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 51

def on_or(node)
  two_start_end_with_calls(node, methods_to_check: methods) do |*matched|
    check(node, *matched)
  end
end