lib/rubocop/cop/performance/squeeze.rb in rubocop-performance-1.18.0 vs lib/rubocop/cop/performance/squeeze.rb in rubocop-performance-1.19.0
- old
+ new
@@ -25,18 +25,19 @@
RESTRICT_ON_SEND = %i[gsub gsub!].freeze
PREFERRED_METHODS = { gsub: :squeeze, gsub!: :squeeze! }.freeze
def_node_matcher :squeeze_candidate?, <<~PATTERN
- (send
+ (call
$!nil? ${:gsub :gsub!}
(regexp
(str $#repeating_literal?)
(regopt))
(str $_))
PATTERN
+ # rubocop:disable Metrics/AbcSize
def on_send(node)
squeeze_candidate?(node) do |receiver, bad_method, regexp_str, replace_str|
regexp_str = regexp_str[0..-2] # delete '+' from the end
regexp_str = interpret_string_escapes(regexp_str)
return unless replace_str == regexp_str
@@ -44,15 +45,17 @@
good_method = PREFERRED_METHODS[bad_method]
message = format(MSG, current: bad_method, prefer: good_method)
add_offense(node.loc.selector, message: message) do |corrector|
string_literal = to_string_literal(replace_str)
- new_code = "#{receiver.source}.#{good_method}(#{string_literal})"
+ new_code = "#{receiver.source}#{node.loc.dot.source}#{good_method}(#{string_literal})"
corrector.replace(node, new_code)
end
end
end
+ # rubocop:enable Metrics/AbcSize
+ alias on_csend on_send
private
def repeating_literal?(regex_str)
regex_str.match?(/\A(?:#{Util::LITERAL_REGEX})\+\z/o)