Sha256: 744226a723ff2526917342d08adbe42dcec4eabf84b609860df3f30bb448112f

Contents?: true

Size: 1.42 KB

Versions: 64

Compression:

Stored size: 1.42 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 %<token>s.'

      def on_new_investigation
        each_missing_space(processed_source.tokens) do |token|
          add_offense(token.pos, message: format(MSG, token: kind(token))) do |corrector|
            PunctuationCorrector.add_space(corrector, token)
          end
        end
      end

      private

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

          yield token1
        end
      end

      def space_missing?(token1, token2)
        same_line?(token1, token2) && token2.column == token1.column + offset
      end

      def space_required_before?(token)
        !(allowed_type?(token) || (token.right_curly_brace? && space_forbidden_before_rcurly?))
      end

      def allowed_type?(token)
        %i[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
    end
  end
end

Version data entries

64 entries across 58 versions & 8 rubygems

Version Path
zilla-0.2.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.46.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.46.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.45.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.45.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.44.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.44.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.43.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.42.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.41.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.41.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.40.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.39.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.38.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.37.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.37.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.36.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.35.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.35.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.34.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.34.0 lib/rubocop/cop/mixin/space_after_punctuation.rb