Sha256: 09dcecab0f59a89ed5ffe9cb15a24597ae00c14790623ebb55047adf236769a8

Contents?: true

Size: 1.13 KB

Versions: 8

Compression:

Stored size: 1.13 KB

Contents

# encoding: utf-8

module RuboCop
  module Cop
    # Common functionality for cops checking for missing space after
    # punctuation.
    module SpaceAfterPunctuation
      MSG = 'Space missing after %s.'

      def investigate(processed_source)
        processed_source.tokens.each_cons(2) do |t1, t2|
          next unless kind(t1) && t1.pos.line == t2.pos.line &&
                      t2.pos.column == t1.pos.column + offset &&
                      ![:tRPAREN, :tRBRACK, :tPIPE].include?(t2.type) &&
                      !(t2.type == :tRCURLY && space_forbidden_before_rcurly?)

          add_offense(t1, t1.pos, format(MSG, kind(t1)))
        end
      end

      def space_forbidden_before_rcurly?
        cfg = config.for_cop('Style/SpaceInsideBlockBraces')
        style = cfg['Enabled'] ? cfg['EnforcedStyle'] : 'space'
        style == 'no_space'
      end

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

      def autocorrect(token)
        ->(corrector) { corrector.replace(token.pos, token.pos.source + ' ') }
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rubocop-0.35.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.35.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.34.2 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.34.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.34.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.33.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.32.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.32.0 lib/rubocop/cop/mixin/space_after_punctuation.rb