Sha256: 6d4213e16fa45b30270019992955c595a88358bc1c7cdc7822a2207a2f0ae712

Contents?: true

Size: 910 Bytes

Versions: 48

Compression:

Stored size: 910 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cop checks for missing space between a token and a comment on the
      # same line.
      #
      # @example
      #   # bad
      #   1 + 1# this operation does ...
      #
      #   # good
      #   1 + 1 # this operation does ...
      class SpaceBeforeComment < Cop
        MSG = 'Put a space before an end-of-line comment.'

        def investigate(processed_source)
          processed_source.tokens.each_cons(2) do |token1, token2|
            next unless token2.comment?
            next unless token1.line == token2.line

            if token1.pos.end == token2.pos.begin
              add_offense(token2.pos, location: token2.pos)
            end
          end
        end

        def autocorrect(range)
          ->(corrector) { corrector.insert_before(range, ' ') }
        end
      end
    end
  end
end

Version data entries

48 entries across 29 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_before_comment.rb
rubocop-0.83.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-0.82.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-0.81.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-0.80.1 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-0.80.0 lib/rubocop/cop/layout/space_before_comment.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_before_comment.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/layout/space_before_comment.rb
rubocop-0.79.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-0.78.0 lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/layout/space_before_comment.rb
rubocop-0.77.0 lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/layout/space_before_comment.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/layout/space_before_comment.rb