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 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.
Constant Summary
-
METHODS =
# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 48%i[start_with? end_with?].to_set
-
METHODS_WITH_ACTIVE_SUPPORT =
# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 49%i[start_with? starts_with? end_with? ends_with?].to_set
-
MSG =
# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 46'Use `%<replacement>s` instead of `%<original_code>s`.'
Instance Attribute Summary
- #check_for_active_support_aliases? ⇒ Boolean readonly private
Instance Method Summary
- #on_and(node)
- #on_or(node)
- #autocorrect(corrector, first_call_args, second_call_args, combined_args) private
- #check(node, receiver, method, first_call_args, second_call_args) private
- #combine_args(first_call_args, second_call_args) private
- #message(node, receiver, method, combined_args) private
- #methods private
Instance Attribute Details
#check_for_active_support_aliases? ⇒ Boolean
(readonly, private)
[ GitHub ]
# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 104
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 75
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
#check(node, receiver, method, first_call_args, second_call_args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 65
def check(node, receiver, method, first_call_args, second_call_args) return unless receiver && second_call_args.all?(&:pure?) combined_args = combine_args(first_call_args, second_call_args) add_offense(node, message: (node, receiver, method, combined_args)) do |corrector| autocorrect(corrector, first_call_args, second_call_args, combined_args) end end
#combine_args(first_call_args, second_call_args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 100
def combine_args(first_call_args, second_call_args) (first_call_args + second_call_args).map(&:source).join(', ') end
#message(node, receiver, method, combined_args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 91
def (node, receiver, method, combined_args) parent = receiver.parent grandparent = parent.parent dot = parent.send_type? ? '.' : '&.' bang = grandparent.send_type? && grandparent.prefix_bang? ? '!' : '' replacement = "#{bang}#{receiver.source}#{dot}#{method}(#{combined_args})" format(MSG, replacement: replacement, original_code: node.source) end
#methods (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/double_start_end_with.rb', line 83
def methods if check_for_active_support_aliases? METHODS_WITH_ACTIVE_SUPPORT else METHODS end end