Class: RuboCop::Cop::Performance::EmptyGsubReplacement
- Inherits:
-
RuboCop::Cop
- Object
- RuboCop::Cop
- RuboCop::Cop::Performance::EmptyGsubReplacement
- Defined in:
- lib/rubocop-aha/cop/performance/empty_gsub_replacement.rb
Overview
This cop identifies places where ‘gsub` can be replaced by `delete`.
Constant Summary collapse
- MSG =
'Use `%s` instead of `%s`.'.freeze
- DETERMINISTIC_REGEX =
/\A(?:#{LITERAL_REGEX})+\Z/
- DELETE =
'delete'.freeze
- BANG =
'!'.freeze
- SINGLE_QUOTE =
"'".freeze
Instance Method Summary collapse
- #autocorrect(node) ⇒ Object
- #on_send(node) ⇒ Object
- #replace_method(node, first, second, first_param, replacement) ⇒ Object
Instance Method Details
#autocorrect(node) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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) ⇒ Object
34 35 36 37 38 39 40 41 |
# 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) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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 |