Class: RuboCop::Cop::Performance::RedundantStringChars
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
RangeHelp,
Base
|
|
Inherits: |
Base
|
Defined in: | lib/rubocop/cop/performance/redundant_string_chars.rb |
Overview
Checks for redundant String#chars
.
Constant Summary
-
MSG =
# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 47'Use `%<good_method>s` instead of `%<bad_method>s`.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 48%i[[] slice first last take length size empty?].freeze
Instance Method Summary
- #on_send(node)
- #build_bad_method(method, args) private
- #build_call_args(call_args_node) private
- #build_good_method(method, args) private
- #build_good_method_for_brackets_or_first_method(method, args) private
- #build_message(method, args) private
- #correction_range(receiver, node) private
- #offense_range(receiver, node) private
Instance Method Details
#build_bad_method(method, args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 112
def build_bad_method(method, args) case method when :[] "chars[#{build_call_args(args)}]" else if args.any? "chars.#{method}(#{build_call_args(args)})" else "chars.#{method}" end end end
#build_call_args(call_args_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 125
def build_call_args(call_args_node) call_args_node.map(&:source).join(', ') end
#build_good_method(method, args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 85
def build_good_method(method, args) case method when :slice "[#{build_call_args(args)}].chars" when :[], :first build_good_method_for_brackets_or_first_method(method, args) when :last '[-1]' when :take "[0...#{args.first.source}].chars" else ".#{method}" end end
#build_good_method_for_brackets_or_first_method(method, args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 100
def build_good_method_for_brackets_or_first_method(method, args) first_arg = args.first if first_arg&.range_type? "[#{build_call_args(args)}].chars" elsif method == :first && args.any? "[0...#{args.first.source}].chars" else first_arg ? "[#{first_arg.source}]" : '[0]' end end
#build_message(method, args) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 79
def (method, args) good_method = build_good_method(method, args) bad_method = build_bad_method(method, args) format(MSG, good_method: good_method, bad_method: bad_method) end
#correction_range(receiver, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 75
def correction_range(receiver, node) range_between(receiver.loc.dot.begin_pos, node.source_range.end_pos) end
#offense_range(receiver, node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 71
def offense_range(receiver, node) range_between(receiver.loc.selector.begin_pos, node.source_range.end_pos) end
#on_send(node)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_string_chars.rb', line 54
def on_send(node) return unless (receiver, method, args = redundant_chars_call?(node)) return if method == :last && !args.empty? range = offense_range(receiver, node) = (method, args) add_offense(range, message: ) do |corrector| range = correction_range(receiver, node) replacement = build_good_method(method, args) corrector.replace(range, replacement) end end