Class: RuboCop::Cop::Performance::DoubleStartEndWith
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/double_start_end_with.rb |
Overview
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.
IncludeActiveSupportAliases
configuration option is used to check for
starts_with?
and ends_with?
. These methods are defined by Active Support.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 44'Use `%<replacement>s` instead of `%<original_code>s`.'
Instance Attribute Summary
- #check_for_active_support_aliases? ⇒ Boolean readonly private
Instance Method Summary
Instance Attribute Details
#check_for_active_support_aliases? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 86
def check_for_active_support_aliases? cop_config['IncludeActiveSupportAliases'] end
Instance Method Details
#autocorrect(corrector, first_call_args, second_call_args, combined_args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 60
def autocorrect(corrector, first_call_args, second_call_args, combined_args) first_argument = first_call_args.first.source_range last_argument = second_call_args.last.source_range range = first_argument.join(last_argument) corrector.replace(range, combined_args) end
#combine_args(first_call_args, second_call_args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 82
def combine_args(first_call_args, second_call_args) (first_call_args + second_call_args).map(&:source).join(', ') end
#message(node, receiver, first_call_args, method, combined_args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 76
def (node, receiver, first_call_args, method, combined_args) dot = first_call_args.first.parent.send_type? ? '.' : '&.' replacement = "#{receiver.source}#{dot}#{method}(#{combined_args})" format(MSG, replacement: replacement, original_code: node.source) end
#on_or(node)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 46
def on_or(node) receiver, method, first_call_args, second_call_args = process_source(node) return unless receiver && second_call_args.all?(&:pure?) combined_args = combine_args(first_call_args, second_call_args) add_offense(node, message: (node, receiver, first_call_args, method, combined_args)) do |corrector| autocorrect(corrector, first_call_args, second_call_args, combined_args) end end
#process_source(node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 68
def process_source(node) if check_for_active_support_aliases? check_with_active_support_aliases(node) else two_start_end_with_calls(node) end end