class RuboCop::Cop::Performance::EmptyGsubReplacement

This cop identifies places where `gsub` can be replaced by `delete`.

@example

@bad
'abc'.gsub('b', 'd')
'abc'.gsub('a', '')
'abc'.gsub(/a/, 'd')
'abc'.gsub!('a', 'd')

@good
'abc'.gsub(/.*/, 'a')
'abc'.gsub(/a+/, 'd')
'a b c'.delete(' ')

Constants

BANG
DELETE
DETERMINISTIC_REGEX
MSG
SINGLE_QUOTE

Public Instance Methods

autocorrect(node) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 43
def autocorrect(node)
  _string, _method, first_param, second_param = *node
  first_source, = first_source(first_param)
  second_source, = *second_param

  unless first_param.str_type?
    first_source = interpret_string_escapes(first_source)
  end

  replacement_method =
    replacement_method(node, first_source, second_source)

  replace_method(node, first_source, second_source, first_param,
                 replacement_method)
end
on_send(node) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 34
def on_send(node)
  string_replacement?(node) do |first_param, second_param|
    return if accept_second_param?(second_param)
    return if accept_first_param?(first_param)
    
    offense(node, first_param, second_param)
  end
end
replace_method(node, first, second, first_param, replacement) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 59
def replace_method(node, first, second, first_param, replacement)
  lambda do |corrector|
    corrector.replace(node.loc.selector, replacement)
    unless first_param.str_type?
      corrector.replace(first_param.source_range,
                        to_string_literal(first))
    end

    if second.empty? && first.length == 1
      remove_second_param(corrector, node, first_param)
    end
  end
end

Private Instance Methods

accept_first_param?(first_param) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 80
def accept_first_param?(first_param)
  first_source, options = first_source(first_param)
  return true if first_source.nil?

  unless first_param.str_type?
    return true if options
    return true unless first_source =~ DETERMINISTIC_REGEX
    # This must be done after checking DETERMINISTIC_REGEX
    # Otherwise things like \s will trip us up
    first_source = interpret_string_escapes(first_source)
  end

  first_source.length != 1
end
accept_second_param?(second_param) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 75
def accept_second_param?(second_param)
  second_source, = *second_param
  second_source.length >= 1
end
first_source(first_param) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 106
def first_source(first_param)
  case first_param.type
  when :regexp
    source_from_regex_literal(first_param)
  when :send
    source_from_regex_constructor(first_param)
  when :str
    first_param.children.first
  end
end
message(node, first_source, second_source) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 148
def message(node, first_source, second_source)
  replacement_method =
    replacement_method(node, first_source, second_source)

  format(MSG, replacement_method, node.method_name)
end
method_suffix(node) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 155
def method_suffix(node)
  node.loc.end ? node.loc.end.source : ''
end
offense(node, first_param, second_param) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 95
def offense(node, first_param, second_param)
  first_source, = first_source(first_param)
  unless first_param.str_type?
    first_source = interpret_string_escapes(first_source)
  end
  second_source, = *second_param
  message = message(node, first_source, second_source)

  add_offense(node, range(node), message)
end
range(node) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 135
def range(node)
  range_between(node.loc.selector.begin_pos, node.source_range.end_pos)
end
remove_second_param(corrector, node, first_param) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 159
def remove_second_param(corrector, node, first_param)
  end_range = range_between(first_param.source_range.end_pos,
                            node.source_range.end_pos)

  corrector.replace(end_range, method_suffix(node))
end
replacement_method(node, first_source, second_source) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 139
def replacement_method(node, first_source, second_source)
  replacement = if second_source.empty? && first_source.length == 1
                  DELETE
                end

  add_bang = node.method_name.to_s.end_with?('!')
  "#{replacement}#{BANG if add_bang}"
end
source_from_regex_constructor(node) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 124
def source_from_regex_constructor(node)
  _const, _init, regex = *node
  case regex.type
  when :regexp
    source_from_regex_literal(regex)
  when :str
    source, = *regex
    source
  end
end
source_from_regex_literal(node) click to toggle source
# File lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb, line 117
def source_from_regex_literal(node)
  regex, options = *node
  source, = *regex
  options, = *options
  [source, options]
end