Sha256: 68cf6ca1dde22f7daf86e6009cd5d635589721d3d45efdcd03de0748faee9d6b

Contents?: true

Size: 1.95 KB

Versions: 338

Compression:

Stored size: 1.95 KB

Contents

RSpec::Support.require_rspec_support 'source/location'

module RSpec
  module Support
    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

338 entries across 269 versions & 68 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
svix-0.53.1 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
svix-0.53.0 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.0/lib/rspec/support/source/token.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.0/lib/rspec/support/source/token.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.10.0/lib/rspec/support/source/token.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.10.0/lib/rspec/support/source/token.rb
vagrant-unbundled-2.2.19.0 vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.10.0/lib/rspec/support/source/token.rb
tdiary-5.2.0 vendor/bundle/ruby/2.7.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rspec-support-3.10.0/lib/rspec/support/source/token.rb
vagrant-unbundled-2.2.18.0 vendor/bundle/ruby/3.0.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
ory-kratos-client-0.8.0.alpha2 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
ory-client-0.0.1.alpha21 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
ory-keto-client-0.7.0.alpha1 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
ory-keto-client-0.7.0.alpha0 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/rspec-support-3.8.2/lib/rspec/support/source/token.rb
ory-kratos-client-0.7.6.alpha7 vendor/bundle/ruby/2.5.0/gems/rspec-support-3.10.2/lib/rspec/support/source/token.rb