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
token[R]
Public Class Methods
new(ripper_token)
click to toggle source
# File lib/rspec/support/source/token.rb, line 27 def initialize(ripper_token) @token = ripper_token.freeze end
tokens_from_ripper_tokens(ripper_tokens)
click to toggle source
# File lib/rspec/support/source/token.rb, line 23 def self.tokens_from_ripper_tokens(ripper_tokens) ripper_tokens.map { |ripper_token| new(ripper_token) }.freeze end
Public Instance Methods
==(other)
click to toggle source
# File lib/rspec/support/source/token.rb, line 43 def ==(other) token == other.token end
Also aliased as: eql?
closed_by?(other)
click to toggle source
# File lib/rspec/support/source/token.rb, line 65 def closed_by?(other) delimiter_closed_by?(other) || keyword_closed_by?(other) end
equals_operator?()
click to toggle source
# File lib/rspec/support/source/token.rb, line 57 def equals_operator? type == :on_op && string == '=' end
inspect()
click to toggle source
# File lib/rspec/support/source/token.rb, line 49 def inspect "#<#{self.class} #{type} #{string.inspect}>" end
keyword?()
click to toggle source
# File lib/rspec/support/source/token.rb, line 53 def keyword? type == :on_kw end
location()
click to toggle source
# File lib/rspec/support/source/token.rb, line 31 def location @location ||= Location.new(*token[0]) end
opening?()
click to toggle source
# File lib/rspec/support/source/token.rb, line 61 def opening? opening_delimiter? || opening_keyword? end
string()
click to toggle source
# File lib/rspec/support/source/token.rb, line 39 def string token[2] end
type()
click to toggle source
# File lib/rspec/support/source/token.rb, line 35 def type token[1] end
Private Instance Methods
delimiter_closed_by?(other)
click to toggle source
# File lib/rspec/support/source/token.rb, line 80 def delimiter_closed_by?(other) other.type == CLOSING_TYPES_BY_OPENING_TYPE[type] end
keyword_closed_by?(other)
click to toggle source
# File lib/rspec/support/source/token.rb, line 84 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
opening_delimiter?()
click to toggle source
# File lib/rspec/support/source/token.rb, line 71 def opening_delimiter? CLOSING_TYPES_BY_OPENING_TYPE.key?(type) end
opening_keyword?()
click to toggle source
# File lib/rspec/support/source/token.rb, line 75 def opening_keyword? return false unless keyword? CLOSING_KEYWORDS_BY_OPENING_KEYWORD.key?(string) end