Sha256: 66acc5a751cfca7950ffa477ab36d64c21e97ead368c303a91c955d6009197ce

Contents?: true

Size: 1.38 KB

Versions: 37

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for cops checking for space before
    # punctuation.
    module SpaceBeforePunctuation
      include RangeHelp

      MSG = 'Space found before %<token>s.'

      def on_new_investigation
        each_missing_space(processed_source.sorted_tokens) do |token, pos_before|
          add_offense(pos_before, message: format(MSG, token: kind(token))) do |corrector|
            PunctuationCorrector.remove_space(corrector, pos_before)
          end
        end
      end

      private

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

          pos_before_punctuation = range_between(token1.end_pos, token2.begin_pos)

          yield token2, pos_before_punctuation
        end
      end

      def space_missing?(token1, token2)
        same_line?(token1, token2) && token2.begin_pos > token1.end_pos
      end

      def space_required_after?(token)
        (token.left_curly_brace? || token.type == :tLAMBEG) && space_required_after_lcurly?
      end

      def space_required_after_lcurly?
        cfg = config.for_cop('Layout/SpaceInsideBlockBraces')
        style = cfg['EnforcedStyle'] || 'space'
        style == 'space'
      end
    end
  end
end

Version data entries

37 entries across 37 versions & 8 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.69.2 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.69.1 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.69.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.68.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.67.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.66.1 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.66.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.65.1 lib/rubocop/cop/mixin/space_before_punctuation.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.65.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
katalyst-govuk-formbuilder-1.9.2 vendor/bundle/ruby/3.3.0/gems/rubocop-1.64.1/lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.64.1 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.63.4 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.63.3 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.63.2 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.63.1 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.63.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
bison-0.1.0 vendor/bundle/ruby/3.2.0/gems/rubocop-1.62.1/lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-1.62.1 lib/rubocop/cop/mixin/space_before_punctuation.rb