Sha256: 3ad998e391679416989f81f01c59dc2c7bf43b91666b0eb4d4283ccd66798095

Contents?: true

Size: 1.42 KB

Versions: 6789

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.'.freeze

      def investigate(processed_source)
        each_missing_space(processed_source.tokens) do |token|
          add_offense(token, location: token.pos,
                             message: format(MSG, token: kind(token)))
        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

6,789 entries across 6,783 versions & 25 rubygems

Version Path
rubocop-0.58.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
vagrant-packet-0.1.1 vendor/bundle/ruby/2.4.0/gems/rubocop-0.57.2/lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.57.2 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.57.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.57.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.56.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.55.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.54.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-0.53.0 lib/rubocop/cop/mixin/space_after_punctuation.rb