lib/rubocop/cop/performance/start_with.rb in rubocop-performance-1.18.0 vs lib/rubocop/cop/performance/start_with.rb in rubocop-performance-1.19.0
- old
+ new
@@ -52,11 +52,11 @@
MSG = 'Use `String#start_with?` instead of a regex match anchored to the beginning of the string.'
RESTRICT_ON_SEND = %i[match =~ match?].freeze
def_node_matcher :redundant_regex?, <<~PATTERN
- {(send $!nil? {:match :=~ :match?} (regexp (str $#literal_at_start?) (regopt)))
+ {(call $!nil? {:match :=~ :match?} (regexp (str $#literal_at_start?) (regopt)))
(send (regexp (str $#literal_at_start?) (regopt)) {:match :match?} $_)
(match-with-lvasgn (regexp (str $#literal_at_start?) (regopt)) $_)}
PATTERN
def on_send(node)
@@ -64,15 +64,17 @@
add_offense(node) do |corrector|
receiver, regex_str = regex_str, receiver if receiver.is_a?(String)
regex_str = drop_start_metacharacter(regex_str)
regex_str = interpret_string_escapes(regex_str)
+ dot = node.loc.dot ? node.loc.dot.source : '.'
- new_source = "#{receiver.source}.start_with?(#{to_string_literal(regex_str)})"
+ new_source = "#{receiver.source}#{dot}start_with?(#{to_string_literal(regex_str)})"
corrector.replace(node, new_source)
end
end
+ alias on_csend on_send
alias on_match_with_lvasgn on_send
end
end
end
end