Sha256: 2822edce7d9f4d5121c4295e67e8c61df2aa49121804ed80e15f8ff54723060b

Contents?: true

Size: 888 Bytes

Versions: 6

Compression:

Stored size: 888 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module AST
    # A node extension for `regexp` nodes. This will be used in place of a plain
    # node when the builder constructs the AST, making its methods available
    # to all `regexp` nodes within RuboCop.
    class RegexpNode < Node
      OPTIONS = {
        x: Regexp::EXTENDED,
        i: Regexp::IGNORECASE,
        m: Regexp::MULTILINE,
        n: Regexp::NOENCODING
      }.freeze

      # @return [Regexp] a regexp of this node
      def to_regexp
        option = regopt.children.map { |opt| OPTIONS[opt] }.inject(:|)
        Regexp.new(content, option)
      end

      # @return [RuboCop::AST::Node] a regopt node
      def regopt
        children.last
      end

      # @return [String] a string of regexp content
      def content
        children.select(&:str_type?).map(&:str_content).join
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
rubocop-ast-0.0.1 lib/rubocop/ast/node/regexp_node.rb
rubocop-0.83.0 lib/rubocop/ast/node/regexp_node.rb
rubocop-0.82.0 lib/rubocop/ast/node/regexp_node.rb
rubocop-0.81.0 lib/rubocop/ast/node/regexp_node.rb
rubocop-0.80.1 lib/rubocop/ast/node/regexp_node.rb
rubocop-0.80.0 lib/rubocop/ast/node/regexp_node.rb