Sha256: fac9853fc638abd772ad2e0d645fb074bdbc1a7ec8d386356e39d79dff3e42fd

Contents?: true

Size: 1.33 KB

Versions: 11

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for handling Regexp literals.
    module RegexpLiteralHelp
      private

      def freespace_mode_regexp?(node)
        regopt = node.children.find(&:regopt_type?)

        regopt.children.include?(:x)
      end

      def pattern_source(node)
        freespace_mode = freespace_mode_regexp?(node)

        node.children.reject(&:regopt_type?).map do |child|
          source_with_comments_and_interpolations_blanked(child, freespace_mode)
        end.join
      end

      def source_with_comments_and_interpolations_blanked(child, freespace_mode)
        source = child.source

        # We don't want to consider the contents of interpolations or free-space mode comments as
        # part of the pattern source, but need to preserve their width, to allow offsets to
        # correctly line up with the original source: spaces have no effect, and preserve width.
        if child.begin_type?
          replace_match_with_spaces(source, /.*/m) # replace all content
        elsif freespace_mode
          replace_match_with_spaces(source, /(?<!\\)#.*/) # replace any comments
        else
          source
        end
      end

      def replace_match_with_spaces(source, pattern)
        source.sub(pattern) { ' ' * Regexp.last_match[0].length }
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/regexp_literal_help.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/regexp_literal_help.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/regexp_literal_help.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/regexp_literal_help.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/regexp_literal_help.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/regexp_literal_help.rb
rubocop-0.92.0 lib/rubocop/cop/mixin/regexp_literal_help.rb
rubocop-0.91.1 lib/rubocop/cop/mixin/regexp_literal_help.rb
rubocop-0.91.0 lib/rubocop/cop/mixin/regexp_literal_help.rb
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.90.0/lib/rubocop/cop/mixin/regexp_literal_help.rb
rubocop-0.90.0 lib/rubocop/cop/mixin/regexp_literal_help.rb