Sha256: e6897d0d6820bf7244f62b0fa1e6f86f390d6426c6b5207b20bd14a286e1a3c0

Contents?: true

Size: 1.04 KB

Versions: 9

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Ext
    # Extensions to AST::RegexpNode for our cached parsed regexp info
    module RegexpNode
      ANY = Object.new
      def ANY.==(_)
        true
      end
      private_constant :ANY

      class << self
        attr_reader :parsed_cache
      end
      @parsed_cache = {}

      # @return [Regexp::Expression::Root, nil]
      def parsed_tree
        return if interpolation?

        str = content
        Ext::RegexpNode.parsed_cache[str] ||= begin
          Regexp::Parser.parse(str)
        rescue StandardError
          nil
        end
      end

      def each_capture(named: ANY)
        return enum_for(__method__, named: named) unless block_given?

        parsed_tree&.traverse do |event, exp, _index|
          yield(exp) if event == :enter &&
                        named == exp.respond_to?(:name) &&
                        exp.respond_to?(:capturing?) &&
                        exp.capturing?
        end

        self
      end

      AST::RegexpNode.include self
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/ext/regexp_node.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/ext/regexp_node.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/ext/regexp_node.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/ext/regexp_node.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/ext/regexp_node.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/ext/regexp_node.rb
rubocop-0.92.0 lib/rubocop/ext/regexp_node.rb
rubocop-0.91.1 lib/rubocop/ext/regexp_node.rb
rubocop-0.91.0 lib/rubocop/ext/regexp_node.rb