Sha256: 75aa1341e3f59aa03fb98a9ae3048e5b0bc817974ef883186be165ae7b0a8261

Contents?: true

Size: 1.41 KB

Versions: 13

Compression:

Stored size: 1.41 KB

Contents

# 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

13 entries across 13 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.47.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.47.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.46.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.45.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.44.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.44.0 lib/rubocop/cop/mixin/space_after_punctuation.rb