Sha256: 12f338e51da955f1f7a5af8578cb99288116a242a3b247e2e34a8be37d5c5568

Contents?: true

Size: 1.86 KB

Versions: 6835

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module RuboCop
  # A basic wrapper around Parser's tokens.
  class Token
    attr_reader :pos, :type, :text

    def self.from_parser_token(parser_token)
      type, details = parser_token
      text, range = details
      new(range, type, text)
    end

    def initialize(pos, type, text)
      @pos = pos
      @type = type
      # Parser token "text" may be an Integer
      @text = text.to_s
    end

    def line
      @pos.line
    end

    def column
      @pos.column
    end

    def begin_pos
      @pos.begin_pos
    end

    def end_pos
      @pos.end_pos
    end

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

    # Checks if there is whitespace after token
    def space_after?
      pos.source_buffer.source.match(/\G\s/, end_pos)
    end

    # Checks if there is whitespace before token
    def space_before?
      position = begin_pos.zero? ? begin_pos : begin_pos - 1
      pos.source_buffer.source.match(/\G\s/, position)
    end

    ## Type Predicates

    def comment?
      type == :tCOMMENT
    end

    def semicolon?
      type == :tSEMI
    end

    def left_array_bracket?
      type == :tLBRACK
    end

    def left_ref_bracket?
      type == :tLBRACK2
    end

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

    def right_bracket?
      type == :tRBRACK
    end

    def left_brace?
      type == :tLBRACE
    end

    def left_curly_brace?
      type == :tLCURLY
    end

    def right_curly_brace?
      type == :tRCURLY
    end

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

    def right_parens?
      type == :tRPAREN
    end

    def comma?
      type == :tCOMMA
    end

    def rescue_modifier?
      type == :kRESCUE_MOD
    end

    def end?
      type == :kEND
    end

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

Version data entries

6,835 entries across 6,810 versions & 28 rubygems

Version Path
cybrid_api_organization_ruby-0.123.124 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_id_ruby-0.123.124 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_bank_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_id_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_organization_ruby-0.123.123 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_bank_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_organization_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_id_ruby-0.123.122 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_bank_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_id_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_organization_ruby-0.123.121 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_bank_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_organization_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_id_ruby-0.123.120 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
avalara_sdk-24.12.0 vendor/bundle/ruby/2.7.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_bank_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_organization_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_id_ruby-0.123.119 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_bank_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb
cybrid_api_id_ruby-0.123.118 vendor/bundle/ruby/3.3.0/gems/rubocop-0.66.0/lib/rubocop/token.rb