Sha256: fd7615a30ec4137d9c9622e4431ef8d479d46277cccb150627f038f14352f78e

Contents?: true

Size: 1.19 KB

Versions: 15

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cops checks for indentation of the first non-blank non-comment
      # line in a file.
      class InitialIndentation < Cop
        MSG = 'Indentation of first line in file detected.'.freeze

        def investigate(_processed_source)
          token = first_token
          space_before(token) { |space| add_offense(space, token.pos) }
        end

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

        private

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

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

          token_with_space =
            range_with_surrounding_space(token.pos, :left, 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 token_with_space == token.pos

          yield range_between(token_with_space.begin_pos, token.pos.begin_pos)
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 2 rubygems

Version Path
dirwatch-0.0.9 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/initial_indentation.rb
dirwatch-0.0.8 vendor/bundle/ruby/2.5.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/initial_indentation.rb
dirwatch-0.0.6 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/initial_indentation.rb
dirwatch-0.0.5 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/initial_indentation.rb
dirwatch-0.0.4 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/initial_indentation.rb
dirwatch-0.0.3 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/initial_indentation.rb
dirwatch-0.0.2 vendor/bundle/ruby/2.3.0/gems/rubocop-0.46.0/lib/rubocop/cop/style/initial_indentation.rb
rubocop-0.48.1 lib/rubocop/cop/style/initial_indentation.rb
rubocop-0.48.0 lib/rubocop/cop/style/initial_indentation.rb
rubocop-0.47.1 lib/rubocop/cop/style/initial_indentation.rb
rubocop-0.47.0 lib/rubocop/cop/style/initial_indentation.rb
rubocop-0.46.0 lib/rubocop/cop/style/initial_indentation.rb
rubocop-0.45.0 lib/rubocop/cop/style/initial_indentation.rb
rubocop-0.44.1 lib/rubocop/cop/style/initial_indentation.rb
rubocop-0.44.0 lib/rubocop/cop/style/initial_indentation.rb