class RuboCop::Token

A basic wrapper around Parser's tokens.

Attributes

pos[R]
text[R]
type[R]

Public Class Methods

from_parser_token(parser_token) click to toggle source
# File lib/rubocop/token.rb, line 8
def self.from_parser_token(parser_token)
  type, details = parser_token
  text, range = details
  new(range, type, text)
end
new(pos, type, text) click to toggle source
# File lib/rubocop/token.rb, line 14
def initialize(pos, type, text)
  @pos = pos
  @type = type
  # Parser token "text" may be an Integer
  @text = text.to_s
end

Public Instance Methods

begin_pos() click to toggle source
# File lib/rubocop/token.rb, line 29
def begin_pos
  @pos.begin_pos
end
column() click to toggle source
# File lib/rubocop/token.rb, line 25
def column
  @pos.column
end
comma?() click to toggle source
# File lib/rubocop/token.rb, line 98
def comma?
  type == :tCOMMA
end
comment?() click to toggle source

Type Predicates

# File lib/rubocop/token.rb, line 54
def comment?
  type == :tCOMMENT
end
end?() click to toggle source
# File lib/rubocop/token.rb, line 106
def end?
  type == :kEND
end
end_pos() click to toggle source
# File lib/rubocop/token.rb, line 33
def end_pos
  @pos.end_pos
end
equal_sign?() click to toggle source
# File lib/rubocop/token.rb, line 110
def equal_sign?
  %i[tEQL tOP_ASGN].include?(type)
end
left_array_bracket?() click to toggle source
# File lib/rubocop/token.rb, line 62
def left_array_bracket?
  type == :tLBRACK
end
left_brace?() click to toggle source
# File lib/rubocop/token.rb, line 78
def left_brace?
  type == :tLBRACE
end
left_bracket?() click to toggle source
# File lib/rubocop/token.rb, line 70
def left_bracket?
  %i[tLBRACK tLBRACK2].include?(type)
end
left_curly_brace?() click to toggle source
# File lib/rubocop/token.rb, line 82
def left_curly_brace?
  type == :tLCURLY
end
left_parens?() click to toggle source
# File lib/rubocop/token.rb, line 90
def left_parens?
  %i[tLPAREN tLPAREN2].include?(type)
end
left_ref_bracket?() click to toggle source
# File lib/rubocop/token.rb, line 66
def left_ref_bracket?
  type == :tLBRACK2
end
line() click to toggle source
# File lib/rubocop/token.rb, line 21
def line
  @pos.line
end
rescue_modifier?() click to toggle source
# File lib/rubocop/token.rb, line 102
def rescue_modifier?
  type == :kRESCUE_MOD
end
right_bracket?() click to toggle source
# File lib/rubocop/token.rb, line 74
def right_bracket?
  type == :tRBRACK
end
right_curly_brace?() click to toggle source
# File lib/rubocop/token.rb, line 86
def right_curly_brace?
  type == :tRCURLY
end
right_parens?() click to toggle source
# File lib/rubocop/token.rb, line 94
def right_parens?
  type == :tRPAREN
end
semicolon?() click to toggle source
# File lib/rubocop/token.rb, line 58
def semicolon?
  type == :tSEMI
end
space_after?() click to toggle source

Checks if there is whitespace after token

# File lib/rubocop/token.rb, line 42
def space_after?
  pos.source_buffer.source.match(/\G\s/, end_pos)
end
space_before?() click to toggle source

Checks if there is whitespace before token

# File lib/rubocop/token.rb, line 47
def space_before?
  position = begin_pos.zero? ? begin_pos : begin_pos - 1
  pos.source_buffer.source.match(/\G\s/, position)
end
to_s() click to toggle source
# File lib/rubocop/token.rb, line 37
def to_s
  "[[#{line}, #{column}], #{type}, #{text.inspect}]"
end