Class: RuboCop::Token
- Inherits:
-
Object
- Object
- RuboCop::Token
- Defined in:
- lib/rubocop/token.rb
Overview
A basic wrapper around Parser’s tokens.
Instance Attribute Summary collapse
-
#pos ⇒ Object
readonly
Returns the value of attribute pos.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #begin_pos ⇒ Object
- #column ⇒ Object
- #comma? ⇒ Boolean
- #comment? ⇒ Boolean
- #end_pos ⇒ Object
- #equal_sign? ⇒ Boolean
-
#initialize(pos, type, text) ⇒ Token
constructor
A new instance of Token.
- #left_array_bracket? ⇒ Boolean
- #left_brace? ⇒ Boolean
- #left_bracket? ⇒ Boolean
- #left_curly_brace? ⇒ Boolean
- #left_parens? ⇒ Boolean
- #left_ref_bracket? ⇒ Boolean
- #line ⇒ Object
- #right_bracket? ⇒ Boolean
- #right_curly_brace? ⇒ Boolean
- #right_parens? ⇒ Boolean
- #semicolon? ⇒ Boolean
-
#space_after? ⇒ Boolean
Checks if there is whitespace after token.
-
#space_before? ⇒ Boolean
Checks if there is whitespace before token.
- #to_s ⇒ Object
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
#pos ⇒ Object (readonly)
Returns the value of attribute pos.
6 7 8 |
# File 'lib/rubocop/token.rb', line 6 def pos @pos end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
6 7 8 |
# File 'lib/rubocop/token.rb', line 6 def text @text end |
#type ⇒ Object (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_pos ⇒ Object
29 30 31 |
# File 'lib/rubocop/token.rb', line 29 def begin_pos pos.begin_pos end |
#column ⇒ Object
25 26 27 |
# File 'lib/rubocop/token.rb', line 25 def column pos.column end |
#comma? ⇒ Boolean
81 82 83 |
# File 'lib/rubocop/token.rb', line 81 def comma? type == :tCOMMA end |
#comment? ⇒ Boolean
37 38 39 |
# File 'lib/rubocop/token.rb', line 37 def comment? type == :tCOMMENT end |
#end_pos ⇒ Object
33 34 35 |
# File 'lib/rubocop/token.rb', line 33 def end_pos pos.end_pos end |
#equal_sign? ⇒ Boolean
85 86 87 |
# File 'lib/rubocop/token.rb', line 85 def equal_sign? %i[tEQL tOP_ASGN].include?(type) end |
#left_array_bracket? ⇒ Boolean
45 46 47 |
# File 'lib/rubocop/token.rb', line 45 def left_array_bracket? type == :tLBRACK end |
#left_brace? ⇒ Boolean
61 62 63 |
# File 'lib/rubocop/token.rb', line 61 def left_brace? type == :tLBRACE end |
#left_bracket? ⇒ Boolean
53 54 55 |
# File 'lib/rubocop/token.rb', line 53 def left_bracket? %i[tLBRACK tLBRACK2].include?(type) end |
#left_curly_brace? ⇒ Boolean
65 66 67 |
# File 'lib/rubocop/token.rb', line 65 def left_curly_brace? type == :tLCURLY end |
#left_parens? ⇒ Boolean
73 74 75 |
# File 'lib/rubocop/token.rb', line 73 def left_parens? %i[tLPAREN tLPAREN2].include?(type) end |
#left_ref_bracket? ⇒ Boolean
49 50 51 |
# File 'lib/rubocop/token.rb', line 49 def left_ref_bracket? type == :tLBRACK2 end |
#line ⇒ Object
21 22 23 |
# File 'lib/rubocop/token.rb', line 21 def line pos.line end |
#right_bracket? ⇒ Boolean
57 58 59 |
# File 'lib/rubocop/token.rb', line 57 def right_bracket? type == :tRBRACK end |
#right_curly_brace? ⇒ Boolean
69 70 71 |
# File 'lib/rubocop/token.rb', line 69 def right_curly_brace? type == :tRCURLY end |
#right_parens? ⇒ Boolean
77 78 79 |
# File 'lib/rubocop/token.rb', line 77 def right_parens? type == :tRPAREN end |
#semicolon? ⇒ Boolean
41 42 43 |
# File 'lib/rubocop/token.rb', line 41 def semicolon? type == :tSEMI end |
#space_after? ⇒ Boolean
Checks if there is whitespace after token
94 95 96 |
# File 'lib/rubocop/token.rb', line 94 def space_after? pos.source_buffer.source.match(/\G\s/, end_pos) end |
#space_before? ⇒ Boolean
Checks if there is whitespace before token
99 100 101 |
# File 'lib/rubocop/token.rb', line 99 def space_before? pos.source_buffer.source.match(/\G\s/, begin_pos - 1) end |
#to_s ⇒ Object
89 90 91 |
# File 'lib/rubocop/token.rb', line 89 def to_s "[[#{@pos.line}, #{@pos.column}], #{@type}, #{@text.inspect}]" end |