Sha256: 1dcd5473a17043362d2b28d1d83838c94c1b774b9db9879b29908376ab2d016b

Contents?: true

Size: 1.51 KB

Versions: 14

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Layout
      # This cops checks for indentation of the first non-blank non-comment
      # line in a file.
      #
      # @example
      #   # bad
      #      class A
      #        def foo; end
      #      end
      #
      #   # good
      #   class A
      #     def foo; end
      #   end
      #
      class InitialIndentation < Cop
        include RangeHelp

        MSG = 'Indentation of first line in file detected.'.freeze

        def investigate(_processed_source)
          space_before(first_token) do |space|
            add_offense(space, location: first_token.pos)
          end
        end

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

        private

        def first_token
          processed_source.find_token { |t| !t.text.start_with?('#') }
        end

        def space_before(token)
          return unless token
          return if token.column.zero?

          space_range =
            range_with_surrounding_space(range: token.pos,
                                         side: :left,
                                         newlines: false)
          # If the file starts with a byte order mark (BOM), the column can be
          # non-zero, but then we find out here if there's no space to the left
          # of the first token.
          return if space_range == token.pos

          yield range_between(space_range.begin_pos, token.begin_pos)
        end
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

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