Sha256: b68c0c07458b7421532d127c332e3c758cadfdac6d13933e6e3a07f0f8381c35

Contents?: true

Size: 916 Bytes

Versions: 11

Compression:

Stored size: 916 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.'.freeze

        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

11 entries across 11 versions & 2 rubygems

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