class RuboCop::Cop::Layout::InitialIndentation

This cops checks for indentation of the first non-blank non-comment line in a file.

Constants

MSG

Public Instance Methods

autocorrect(range) click to toggle source
# File lib/rubocop/cop/layout/initial_indentation.rb, line 16
def autocorrect(range)
  ->(corrector) { corrector.remove(range) }
end
investigate(_processed_source) click to toggle source
# File lib/rubocop/cop/layout/initial_indentation.rb, line 10
def investigate(_processed_source)
  space_before(first_token) do |space|
    add_offense(space, location: first_token.pos)
  end
end

Private Instance Methods

first_token() click to toggle source
# File lib/rubocop/cop/layout/initial_indentation.rb, line 22
def first_token
  processed_source.tokens.find { |t| !t.text.start_with?('#') }
end
space_before(token) { |range_between(begin_pos, pos.begin_pos)| ... } click to toggle source
# File lib/rubocop/cop/layout/initial_indentation.rb, line 26
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