Sha256: d4b6868c701646273895f6a31cc28eb0b2a1e1e100117f5ccfccda194b89ee0a

Contents?: true

Size: 1.19 KB

Versions: 3

Compression:

Stored size: 1.19 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for checking surrounding space.
    module SurroundingSpace
      def space_between?(t1, t2)
        between = Parser::Source::Range.new(t1.pos.source_buffer,
                                            t1.pos.end_pos,
                                            t2.pos.begin_pos).source

        # Check if the range between the tokens starts with a space. It can
        # contain other characters, e.g. a unary plus, but it must start with
        # space.
        between =~ /^\s/
      end

      def index_of_first_token(node)
        b = node.source_range.begin
        token_table[b.line][b.column]
      end

      def index_of_last_token(node)
        e = node.source_range.end
        (0...e.column).to_a.reverse_each do |c|
          ix = token_table[e.line][c]
          return ix if ix
        end
      end

      def token_table
        @token_table ||= begin
          table = {}
          @processed_source.tokens.each_with_index do |t, ix|
            table[t.pos.line] ||= {}
            table[t.pos.line][t.pos.column] = ix
          end
          table
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rubocop-0.37.1 lib/rubocop/cop/mixin/surrounding_space.rb
rubocop-0.37.0 lib/rubocop/cop/mixin/surrounding_space.rb
rubocop-0.36.0 lib/rubocop/cop/mixin/surrounding_space.rb