Sha256: c4dfadc60263948323e87c784d58a88b73f2888c2aea7606a40f138a3d93fcc0

Contents?: true

Size: 1.86 KB

Versions: 105

Compression:

Stored size: 1.86 KB

Contents

RSpec::Support.require_rspec_core "source/location"

module RSpec
  module Core
    class Source
      # @private
      # A wrapper for Ripper AST node which is generated with `Ripper.sexp`.
      class Node
        include Enumerable

        attr_reader :sexp, :parent

        def self.sexp?(array)
          array.is_a?(Array) && array.first.is_a?(Symbol)
        end

        def initialize(ripper_sexp, parent=nil)
          @sexp = ripper_sexp.freeze
          @parent = parent
        end

        def type
          sexp[0]
        end

        def args
          @args ||= raw_args.map do |raw_arg|
            if Node.sexp?(raw_arg)
              Node.new(raw_arg, self)
            elsif Location.location?(raw_arg)
              Location.new(*raw_arg)
            elsif raw_arg.is_a?(Array)
              GroupNode.new(raw_arg, self)
            else
              raw_arg
            end
          end.freeze
        end

        def children
          @children ||= args.select { |arg| arg.is_a?(Node) }.freeze
        end

        def location
          @location ||= args.find { |arg| arg.is_a?(Location) }
        end

        def each(&block)
          return to_enum(__method__) unless block_given?

          yield self

          children.each do |child|
            child.each(&block)
          end
        end

        def each_ancestor
          return to_enum(__method__) unless block_given?

          current_node = self

          while (current_node = current_node.parent)
            yield current_node
          end
        end

        def inspect
          "#<#{self.class} #{type}>"
        end

      private

        def raw_args
          sexp[1..-1] || []
        end
      end

      # @private
      class GroupNode < Node
        def type
          :group
        end

      private

        def raw_args
          sexp
        end
      end
    end
  end
end

Version data entries

105 entries across 96 versions & 21 rubygems

Version Path
lazy_record-0.1.8 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
lazy_record-0.1.7 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
lazy_record-0.1.6 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
lazy_record-0.1.4 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
tdiary-5.0.4 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
logstash-filter-zabbix-0.1.2 vendor/bundle/jruby/1.9/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
lazy_record-0.1.3 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
lazy_record-0.1.2 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
lazy_record-0.1.1 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
lazy_record-0.1.0 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
logstash-filter-zabbix-0.1.1 vendor/bundle/jruby/1.9/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
fucknumber-0.1.3 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
logstash-input-fifo-0.9.1 vendor/bundle/jruby/1.9/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
logstash-input-fifo-0.9.0 vendor/bundle/jruby/1.9/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
abaci-0.3.0 vendor/bundle/gems/rspec-core-3.5.1/lib/rspec/core/source/node.rb
abaci-0.3.0 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/rspec-core-3.5.4/lib/rspec/core/source/node.rb