class RSpec::Support::Source::Token
@private A wrapper for Ripper token which is generated with ‘Ripper.lex`.
Constants
- CLOSING_KEYWORDS_BY_OPENING_KEYWORD
- CLOSING_TYPES_BY_OPENING_TYPE
Attributes
Public Class Methods
Source
# File lib/rspec/support/source/token.rb, line 29 def initialize(ripper_token) @token = ripper_token.freeze end
Source
# File lib/rspec/support/source/token.rb, line 25 def self.tokens_from_ripper_tokens(ripper_tokens) ripper_tokens.map { |ripper_token| new(ripper_token) }.freeze end
Public Instance Methods
Source
# File lib/rspec/support/source/token.rb, line 45 def ==(other) token == other.token end
Also aliased as: eql?
Source
# File lib/rspec/support/source/token.rb, line 67 def closed_by?(other) delimiter_closed_by?(other) || keyword_closed_by?(other) end
Source
# File lib/rspec/support/source/token.rb, line 59 def equals_operator? type == :on_op && string == '=' end
Source
# File lib/rspec/support/source/token.rb, line 51 def inspect "#<#{self.class} #{type} #{string.inspect}>" end
Source
# File lib/rspec/support/source/token.rb, line 55 def keyword? type == :on_kw end
Source
# File lib/rspec/support/source/token.rb, line 33 def location @location ||= Location.new(*token[0]) end
Source
# File lib/rspec/support/source/token.rb, line 63 def opening? opening_delimiter? || opening_keyword? end
Private Instance Methods
Source
# File lib/rspec/support/source/token.rb, line 82 def delimiter_closed_by?(other) other.type == CLOSING_TYPES_BY_OPENING_TYPE[type] end
Source
# File lib/rspec/support/source/token.rb, line 86 def keyword_closed_by?(other) return false unless keyword? return true if other.string == CLOSING_KEYWORDS_BY_OPENING_KEYWORD[string] # Ruby 3's `end`-less method definition: `def method_name = body` string == 'def' && other.equals_operator? && location.line == other.location.line end
Source
# File lib/rspec/support/source/token.rb, line 73 def opening_delimiter? CLOSING_TYPES_BY_OPENING_TYPE.key?(type) end
Source
# File lib/rspec/support/source/token.rb, line 77 def opening_keyword? return false unless keyword? CLOSING_KEYWORDS_BY_OPENING_KEYWORD.key?(string) end