Class: RuboCop::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/token.rb

Overview

A basic wrapper around Parser's tokens.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pos, type, text) ⇒ Token

Returns a new instance of Token



14
15
16
17
18
19
# 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

Instance Attribute Details

#posObject (readonly)

Returns the value of attribute pos



6
7
8
# File 'lib/rubocop/token.rb', line 6

def pos
  @pos
end

#textObject (readonly)

Returns the value of attribute text



6
7
8
# File 'lib/rubocop/token.rb', line 6

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type



6
7
8
# File 'lib/rubocop/token.rb', line 6

def type
  @type
end

Class Method Details

.from_parser_token(parser_token) ⇒ Object



8
9
10
11
12
# 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

Instance Method Details

#begin_posObject



29
30
31
# File 'lib/rubocop/token.rb', line 29

def begin_pos
  @pos.begin_pos
end

#columnObject



25
26
27
# File 'lib/rubocop/token.rb', line 25

def column
  @pos.column
end

#comma?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'lib/rubocop/token.rb', line 98

def comma?
  type == :tCOMMA
end

#comment?Boolean

Type Predicates

Returns:

  • (Boolean)


54
55
56
# File 'lib/rubocop/token.rb', line 54

def comment?
  type == :tCOMMENT
end

#end?Boolean

Returns:

  • (Boolean)


106
107
108
# File 'lib/rubocop/token.rb', line 106

def end?
  type == :kEND
end

#end_posObject



33
34
35
# File 'lib/rubocop/token.rb', line 33

def end_pos
  @pos.end_pos
end

#equal_sign?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/rubocop/token.rb', line 110

def equal_sign?
  %i[tEQL tOP_ASGN].include?(type)
end

#left_array_bracket?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/rubocop/token.rb', line 62

def left_array_bracket?
  type == :tLBRACK
end

#left_brace?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/rubocop/token.rb', line 78

def left_brace?
  type == :tLBRACE
end

#left_bracket?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/rubocop/token.rb', line 70

def left_bracket?
  %i[tLBRACK tLBRACK2].include?(type)
end

#left_curly_brace?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/rubocop/token.rb', line 82

def left_curly_brace?
  type == :tLCURLY
end

#left_parens?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/rubocop/token.rb', line 90

def left_parens?
  %i[tLPAREN tLPAREN2].include?(type)
end

#left_ref_bracket?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rubocop/token.rb', line 66

def left_ref_bracket?
  type == :tLBRACK2
end

#lineObject



21
22
23
# File 'lib/rubocop/token.rb', line 21

def line
  @pos.line
end

#rescue_modifier?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/rubocop/token.rb', line 102

def rescue_modifier?
  type == :kRESCUE_MOD
end

#right_bracket?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/rubocop/token.rb', line 74

def right_bracket?
  type == :tRBRACK
end

#right_curly_brace?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/rubocop/token.rb', line 86

def right_curly_brace?
  type == :tRCURLY
end

#right_parens?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/rubocop/token.rb', line 94

def right_parens?
  type == :tRPAREN
end

#semicolon?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rubocop/token.rb', line 58

def semicolon?
  type == :tSEMI
end

#space_after?Boolean

Checks if there is whitespace after token

Returns:

  • (Boolean)


42
43
44
# File 'lib/rubocop/token.rb', line 42

def space_after?
  pos.source_buffer.source.match(/\G\s/, end_pos)
end

#space_before?Boolean

Checks if there is whitespace before token

Returns:

  • (Boolean)


47
48
49
50
# 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_sObject



37
38
39
# File 'lib/rubocop/token.rb', line 37

def to_s
  "[[#{line}, #{column}], #{type}, #{text.inspect}]"
end