Sha256: 6612b28a0af44bb1df57b4c20a8ebdcc68aff82a3c6ed26efc8efadda56035b4

Contents?: true

Size: 1.35 KB

Versions: 15

Compression:

Stored size: 1.35 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    # Common functionality for cops checking for space before
    # punctuation.
    module SpaceBeforePunctuation
      MSG = 'Space found before %s.'.freeze

      def investigate(processed_source)
        each_missing_space(processed_source.tokens) do |token, pos_before|
          add_offense(pos_before, pos_before, format(MSG, kind(token)))
        end
      end

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

          pos_before_punctuation = range_between(t1.pos.end_pos,
                                                 t2.pos.begin_pos)

          yield t2, pos_before_punctuation
        end
      end

      def space_missing?(t1, t2)
        t1.pos.line == t2.pos.line && t2.pos.begin_pos > t1.pos.end_pos
      end

      def space_required_after?(token)
        token.type == :tLCURLY && space_required_after_lcurly?
      end

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

      def autocorrect(pos_before_punctuation)
        ->(corrector) { corrector.remove(pos_before_punctuation) }
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_before_punctuation.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_before_punctuation.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_before_punctuation.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_before_punctuation.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_before_punctuation.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_before_punctuation.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-0.48.1 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-0.48.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-0.47.1 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-0.47.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-0.46.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-0.45.0 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-0.44.1 lib/rubocop/cop/mixin/space_before_punctuation.rb
rubocop-0.44.0 lib/rubocop/cop/mixin/space_before_punctuation.rb