class RuboCop::Cop::Style::RedundantRegexpArgument
Identifies places where argument can be replaced from a deterministic regexp to a string.
@example
# bad 'foo'.byteindex(/f/) 'foo'.byterindex(/f/) 'foo'.gsub(/f/, 'x') 'foo'.gsub!(/f/, 'x') 'foo'.partition(/f/) 'foo'.rpartition(/f/) 'foo'.scan(/f/) 'foo'.split(/f/) 'foo'.start_with?(/f/) 'foo'.sub(/f/, 'x') 'foo'.sub!(/f/, 'x') # good 'foo'.byteindex('f') 'foo'.byterindex('f') 'foo'.gsub('f', 'x') 'foo'.gsub!('f', 'x') 'foo'.partition('f') 'foo'.rpartition('f') 'foo'.scan('f') 'foo'.split('f') 'foo'.start_with?('f') 'foo'.sub('f', 'x') 'foo'.sub!('f', 'x')
Constants
- DETERMINISTIC_REGEX
- MSG
- RESTRICT_ON_SEND
- STR_SPECIAL_CHARS
Public Instance Methods
Source
# File lib/rubocop/cop/style/redundant_regexp_argument.rb, line 48 def on_send(node) return unless (regexp_node = node.first_argument) return unless regexp_node.regexp_type? return if !regexp_node.regopt.children.empty? || regexp_node.content == ' ' return unless determinist_regexp?(regexp_node) prefer = preferred_argument(regexp_node) message = format(MSG, prefer: prefer, current: regexp_node.source) add_offense(regexp_node, message: message) do |corrector| corrector.replace(regexp_node, prefer) end end
Also aliased as: on_csend
Private Instance Methods
Source
# File lib/rubocop/cop/style/redundant_regexp_argument.rb, line 65 def determinist_regexp?(regexp_node) DETERMINISTIC_REGEX.match?(regexp_node.source) end
Source
# File lib/rubocop/cop/style/redundant_regexp_argument.rb, line 69 def preferred_argument(regexp_node) new_argument = replacement(regexp_node) if new_argument.include?('"') new_argument.gsub!("'", "\\\\'") new_argument.gsub!('\"', '"') quote = "'" elsif new_argument.include?('\'') new_argument.gsub!("'", "\\\\'") quote = "'" elsif new_argument.include?('\\') quote = '"' else quote = enforce_double_quotes? ? '"' : "'" end "#{quote}#{new_argument}#{quote}" end
Source
# File lib/rubocop/cop/style/redundant_regexp_argument.rb, line 88 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