Sha256: 91babdc8fb40c0afade16371cf2018311f51b90d16f98398caa12ff24ac88ceb

Contents?: true

Size: 1.43 KB

Versions: 7

Compression:

Stored size: 1.43 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

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

      def investigate(processed_source)
        each_missing_space(processed_source.tokens) do |token|
          add_offense(token, token.pos, format(MSG, kind(token)))
        end
      end

      def each_missing_space(tokens)
        tokens.each_cons(2) do |t1, t2|
          next unless kind(t1)
          next unless space_missing?(t1, t2)
          next unless space_required_before?(t2)

          yield t1
        end
      end

      def space_missing?(t1, t2)
        t1.pos.line == t2.pos.line && t2.pos.column == t1.pos.column + offset
      end

      def space_required_before?(token)
        !(allowed_type?(token) ||
          (token.type == :tRCURLY && space_forbidden_before_rcurly?))
      end

      def allowed_type?(token)
        [:tRPAREN, :tRBRACK, :tPIPE].include?(token.type)
      end

      def space_forbidden_before_rcurly?
        style = space_style_before_rcurly
        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

7 entries across 7 versions & 2 rubygems

Version Path
fluent-plugin-detect-memb-exceptions-0.0.2 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
fluent-plugin-detect-memb-exceptions-0.0.1 vendor/bundle/ruby/2.0.0/gems/rubocop-0.42.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.43.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.42.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.41.2 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.41.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.41.0 lib/rubocop/cop/mixin/space_after_punctuation.rb