Sha256: d1361d8d392f49194f0463ebaae6915dc8b342aa60c6f7ebfd133b3561d19e4a

Contents?: true

Size: 1.85 KB

Versions: 88

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Performance
      # Identifies places where `split` argument can be replaced from
      # a deterministic regexp to a string.
      #
      # @example
      #   # bad
      #   'a,b,c'.split(/,/)
      #
      #   # good
      #   'a,b,c'.split(',')
      class RedundantSplitRegexpArgument < Base
        extend AutoCorrector

        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

        def_node_matcher :split_call_with_regexp?, <<~PATTERN
          {(call !nil? :split $regexp)}
        PATTERN

        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
        alias on_csend on_send

        private

        def determinist_regexp?(regexp_node)
          DETERMINISTIC_REGEX.match?(regexp_node.source)
        end

        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
      end
    end
  end
end

Version data entries

88 entries across 88 versions & 6 rubygems

Version Path
rubocop-performance-1.24.0 lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
tailscale_middleware-0.0.3 vendor/cache/ruby/3.4.0/gems/rubocop-performance-1.23.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
rubocop-performance-1.23.1 lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
rubocop-performance-1.23.0 lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
rubocop-performance-1.22.1 lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
rubocop-performance-1.22.0 lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
rubocop-performance-1.21.1 lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-performance-1.21.0/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
rubocop-performance-1.21.0 lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.20.2/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-0.2.10 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
rubocop-performance-1.20.2 lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-0.2.9 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-0.2.8 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-0.2.7 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-0.2.6 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-0.2.5 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-0.2.4 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb
harbr-0.2.3 vendor/bundle/ruby/3.2.0/gems/rubocop-performance-1.19.1/lib/rubocop/cop/performance/redundant_split_regexp_argument.rb