Sha256: 7dcf0d22aabeca738bfb534554ea91c021b2408da424fe4c71f6c6dcbbe359b1

Contents?: true

Size: 879 Bytes

Versions: 19

Compression:

Stored size: 879 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 < Base
        extend AutoCorrector

        MSG = 'Put a space before an end-of-line comment.'

        def on_new_investigation
          processed_source.sorted_tokens.each_cons(2) do |token1, token2|
            next unless token2.comment?
            next unless same_line?(token1, token2)
            next unless token1.pos.end == token2.pos.begin

            range = token2.pos

            add_offense(range) { |corrector| corrector.insert_before(range, ' ') }
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 4 rubygems

Version Path
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_before_comment.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.29.1 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.29.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.28.2 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.28.1 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.28.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.27.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.26.1 lib/rubocop/cop/layout/space_before_comment.rb
op_connect-0.1.2 vendor/bundle/ruby/3.1.0/gems/rubocop-1.26.0/lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.26.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.25.1 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.25.0 lib/rubocop/cop/layout/space_before_comment.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/rubocop-1.24.0/lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.24.1 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.24.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.23.0 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.22.3 lib/rubocop/cop/layout/space_before_comment.rb
rubocop-1.22.2 lib/rubocop/cop/layout/space_before_comment.rb