Sha256: 5ebd92d4ab8a7d535fff5a4a8c842962dac6e4f77c9e53ada15a65b41bb57dc6

Contents?: true

Size: 1.44 KB

Versions: 35

Compression:

Stored size: 1.44 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)
        token1.line == token2.line &&
          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

35 entries across 35 versions & 3 rubygems

Version Path
plaid-14.13.0 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
plaid-14.12.1 vendor/bundle/ruby/3.0.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
plaid-14.12.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
plaid-14.11.1 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
plaid-14.10.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
plaid-14.7.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.91.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.12.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.12.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.11.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.10.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.9.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.9.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.8.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.8.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.7.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.6.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.6.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.5.2 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.5.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.5.0 lib/rubocop/cop/mixin/space_after_punctuation.rb