Class: RuboCop::Cop::Performance::RedundantSplitRegexpArgument

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/performance/redundant_split_regexp_argument.rb

Overview

Identifies places where ‘split` argument can be replaced from a deterministic regexp to a string.

Examples:

# bad
'a,b,c'.split(/,/)

# good
'a,b,c'.split(',')

Constant Summary collapse

MSG =
'Use string as argument instead of regexp.'
RESTRICT_ON_SEND =
%i[split].freeze
DETERMINISTIC_REGEX =
/\A(?:#{LITERAL_REGEX})+\Z/.freeze
STR_SPECIAL_CHARS =
%w[\n \" \' \\\\ \t \b \f \r].freeze

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object Also known as: on_csend



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rubocop/cop/performance/redundant_split_regexp_argument.rb', line 27

def on_send(node)
  return unless (regexp_node = split_call_with_regexp?(node))
  return if regexp_node.ignore_case? || regexp_node.content == ' '
  return unless determinist_regexp?(regexp_node)

  add_offense(regexp_node) do |corrector|
    new_argument = replacement(regexp_node)

    corrector.replace(regexp_node, "\"#{new_argument}\"")
  end
end