Class: RuboCop::Cop::Performance::RedundantSplitRegexpArgument
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
AutoCorrector,
Base
|
|
Instance Chain:
self,
Base
|
|
Inherits: |
Base
|
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.
Constant Summary
-
DETERMINISTIC_REGEX =
# File 'lib/rubocop/cop/performance/redundant_split_regexp_argument.rb', line 20/\A(?:#{LITERAL_REGEX})+\Z/.freeze
-
MSG =
# File 'lib/rubocop/cop/performance/redundant_split_regexp_argument.rb', line 18'Use string as argument instead of regexp.'
-
RESTRICT_ON_SEND =
# File 'lib/rubocop/cop/performance/redundant_split_regexp_argument.rb', line 19%i[split].freeze
-
STR_SPECIAL_CHARS =
# File 'lib/rubocop/cop/performance/redundant_split_regexp_argument.rb', line 21%w[\n \" \' \\\\ \t \b \f \r].freeze
Instance Method Summary
-
#on_csend(node)
Alias for #on_send.
- #on_send(node) (also: #on_csend)
- #determinist_regexp?(regexp_node) ⇒ Boolean private
- #replacement(regexp_node) private
Instance Method Details
#determinist_regexp?(regexp_node) ⇒ Boolean
(private)
# File 'lib/rubocop/cop/performance/redundant_split_regexp_argument.rb', line 42
def determinist_regexp?(regexp_node) DETERMINISTIC_REGEX.match?(regexp_node.source) end
#on_csend(node)
Alias for #on_send.
# File 'lib/rubocop/cop/performance/redundant_split_regexp_argument.rb', line 38
alias on_csend on_send
#on_send(node) Also known as: #on_csend
[ GitHub ]# 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
#replacement(regexp_node) (private)
[ GitHub ]# File 'lib/rubocop/cop/performance/redundant_split_regexp_argument.rb', line 46
def replacement(regexp_node) regexp_content = regexp_node.content stack = [] chars = regexp_content.chars.each_with_object([]) do |char, strings| if stack.empty? && char == '\\' stack.push(char) else strings << "#{stack.pop}#{char}" end end chars.map do |char| char = char.dup char.delete!('\\') unless STR_SPECIAL_CHARS.include?(char) char end.join end