Sha256: 65dc27dc5b9f0047e045e5c2c2165271ebdb776382ef970e431e0f888297e50b

Contents?: true

Size: 1.81 KB

Versions: 3

Compression:

Stored size: 1.81 KB

Contents

# encoding: utf-8

# rubocop:disable SymbolName

module Rubocop
  module Cop
    module Style
      # Common functionality for cops checking for missing space after
      # punctuation.
      module SpaceAfterCommaEtc
        MSG = 'Space missing after %s.'

        def inspect(source_buffer, source, tokens, ast, comments)
          tokens.each_cons(2) do |t1, t2|
            if kind(t1) && t1.pos.line == t2.pos.line &&
                t2.pos.column == t1.pos.column + offset(t1)
              add_offence(:convention, t1.pos, sprintf(MSG, kind(t1)))
            end
          end
        end

        # The normal offset, i.e., the distance from the punctuation
        # token where a space should be, is 1.
        def offset(token)
          1
        end
      end

      # Checks for comma (,) not follwed by some kind of space.
      class SpaceAfterComma < Cop
        include SpaceAfterCommaEtc

        def kind(token)
          'comma' if token.type == :tCOMMA
        end
      end

      # Checks for semicolon (;) not follwed by some kind of space.
      class SpaceAfterSemicolon < Cop
        include SpaceAfterCommaEtc

        def kind(token)
          'semicolon' if token.type == :tSEMI
        end
      end

      # Checks for colon (:) not follwed by some kind of space.
      class SpaceAfterColon < Cop
        include SpaceAfterCommaEtc

        # The colon following a label will not appear in the token
        # array. Instad we get a tLABEL token, whose length we use to
        # calculate where we expect a space.
        def offset(token)
          case token.type
          when :tLABEL then token.text.length + 1
          when :tCOLON then 1
          end
        end

        def kind(token)
          case token.type
          when :tLABEL, :tCOLON then 'colon'
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
rubocop-0.9.1 lib/rubocop/cop/style/space_after_comma_etc.rb
sabat-rubocop-0.9.0 lib/rubocop/cop/style/space_after_comma_etc.rb
rubocop-0.9.0 lib/rubocop/cop/style/space_after_comma_etc.rb