Class: RSpec::Support::Source::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/support/source/token.rb

Overview

A wrapper for Ripper token which is generated with ‘Ripper.lex`.

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ripper_token) ⇒ Token

Returns a new instance of Token.



29
30
31
# File 'lib/rspec/support/source/token.rb', line 29

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

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



23
24
25
# File 'lib/rspec/support/source/token.rb', line 23

def token
  @token
end

Class Method Details

.tokens_from_ripper_tokens(ripper_tokens) ⇒ Object



25
26
27
# 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

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



45
46
47
# File 'lib/rspec/support/source/token.rb', line 45

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

#closed_by?(other) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rspec/support/source/token.rb', line 67

def closed_by?(other)
  delimiter_closed_by?(other) || keyword_closed_by?(other)
end

#equals_operator?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/rspec/support/source/token.rb', line 59

def equals_operator?
  type == :on_op && string == '='
end

#inspectObject



51
52
53
# File 'lib/rspec/support/source/token.rb', line 51

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

#keyword?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/rspec/support/source/token.rb', line 55

def keyword?
  type == :on_kw
end

#locationObject



33
34
35
# File 'lib/rspec/support/source/token.rb', line 33

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

#opening?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/rspec/support/source/token.rb', line 63

def opening?
  opening_delimiter? || opening_keyword?
end

#stringObject



41
42
43
# File 'lib/rspec/support/source/token.rb', line 41

def string
  token[2]
end

#typeObject



37
38
39
# File 'lib/rspec/support/source/token.rb', line 37

def type
  token[1]
end