Sha256: 41ce448e0ee90ea627cb9c1c81f369957df24ee1324b9dc02ef3e8f95ff20849

Contents?: true

Size: 1.95 KB

Versions: 80

Compression:

Stored size: 1.95 KB

Contents

RSpec::Support.require_rspec_core "source/location"

module RSpec
  module Core
    class Source
      # @private
      # A wrapper for Ripper token which is generated with `Ripper.lex`.
      class Token
        CLOSING_TYPES_BY_OPENING_TYPE = {
          :on_lbracket    => :on_rbracket,
          :on_lparen      => :on_rparen,
          :on_lbrace      => :on_rbrace,
          :on_heredoc_beg => :on_heredoc_end
        }.freeze

        CLOSING_KEYWORDS_BY_OPENING_KEYWORD = {
          'def' => 'end',
          'do'  => 'end',
        }.freeze

        attr_reader :token

        def self.tokens_from_ripper_tokens(ripper_tokens)
          ripper_tokens.map { |ripper_token| new(ripper_token) }.freeze
        end

        def initialize(ripper_token)
          @token = ripper_token.freeze
        end

        def location
          @location ||= Location.new(*token[0])
        end

        def type
          token[1]
        end

        def string
          token[2]
        end

        def ==(other)
          token == other.token
        end

        alias_method :eql?, :==

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

        def keyword?
          type == :on_kw
        end

        def opening?
          opening_delimiter? || opening_keyword?
        end

        def closed_by?(other)
          closed_by_delimiter?(other) || closed_by_keyword?(other)
        end

        private

        def opening_delimiter?
          CLOSING_TYPES_BY_OPENING_TYPE.key?(type)
        end

        def opening_keyword?
          return false unless keyword?
          CLOSING_KEYWORDS_BY_OPENING_KEYWORD.key?(string)
        end

        def closed_by_delimiter?(other)
          other.type == CLOSING_TYPES_BY_OPENING_TYPE[type]
        end

        def closed_by_keyword?(other)
          return false unless other.keyword?
          other.string == CLOSING_KEYWORDS_BY_OPENING_KEYWORD[string]
        end
      end
    end
  end
end

Version data entries

80 entries across 72 versions & 16 rubygems

Version Path
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-2.1.1.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-2.0.4.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-2.0.3.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.4.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.5.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-2.0.0.1 vendor/bundle/ruby/2.4.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-1.9.8.1 vendor/bundle/ruby/2.4.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
vagrant-unbundled-1.9.7.1 vendor/bundle/ruby/2.4.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
tdiary-5.0.5 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
tdiary-5.0.5 vendor/bundle/gems/tdiary-5.0.4/vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
logstash-filter-csharp-0.1.0 vendor/bundle/jruby/2.3.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
etude_for_ruby-0.1.4 vendor/bundle/ruby/2.2.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
etude_for_ruby-0.1.4 vendor/bundle/ruby/2.4.0/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
lazy_record-0.2.1 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
lazy_record-0.2.0 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb
lazy_record-0.1.9 vendor/bundle/gems/rspec-core-3.5.4/lib/rspec/core/source/token.rb