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
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/space_after_punctuation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.35.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.36.0/lib/rubocop/cop/mixin/space_after_punctuation.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.31.2/lib/rubocop/cop/mixin/space_after_punctuation.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/rubocop-1.50.2/lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.50.2 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.50.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.50.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.49.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
call_your_name-0.1.0 vendor/bundle/ruby/3.1.0/gems/rubocop-1.48.1/lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.48.1 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.48.0 lib/rubocop/cop/mixin/space_after_punctuation.rb
rubocop-1.47.0 lib/rubocop/cop/mixin/space_after_punctuation.rb