Sha256: 22a33a71a7dbb3ac2b7fcf4153ea5b4c12e0473719d08993fe5d627e09f207dd

Contents?: true

Size: 881 Bytes

Versions: 2

Compression:

Stored size: 881 Bytes

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This hint 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

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rbhint-0.87.1.rc1 lib/rubocop/cop/layout/space_before_comment.rb
rbhint-0.85.1.rc2 lib/rubocop/cop/layout/space_before_comment.rb