class RubbyCop::Cop::Layout::EndOfLine

This cop checks for Windows-style line endings in the source code.

Constants

MSG_DETECTED
MSG_MISSING

Public Instance Methods

investigate(processed_source) click to toggle source
# File lib/rubbycop/cop/layout/end_of_line.rb, line 13
def investigate(processed_source)
  last_token = processed_source.tokens.last
  last_line =
    last_token ? last_token.pos.line : processed_source.lines.length

  processed_source.raw_source.each_line.with_index do |line, index|
    break if index >= last_line
    msg = offense_message(line)
    next unless msg
    next if unimportant_missing_cr?(index, last_line, line)

    range =
      source_range(processed_source.buffer, index + 1, 0, line.length)
    add_offense(nil, range, msg)
    # Usually there will be carriage return characters on all or none
    # of the lines in a file, so we report only one offense.
    break
  end
end
offense_message(line) click to toggle source
# File lib/rubbycop/cop/layout/end_of_line.rb, line 38
def offense_message(line)
  effective_style = if style == :native
                      Platform.windows? ? :crlf : :lf
                    else
                      style
                    end
  case effective_style
  when :lf then MSG_DETECTED if line =~ /\r$/
  else MSG_MISSING if line !~ /\r$/
  end
end
unimportant_missing_cr?(index, last_line, line) click to toggle source

If there is no LF on the last line, we don't care if there's no CR.

# File lib/rubbycop/cop/layout/end_of_line.rb, line 34
def unimportant_missing_cr?(index, last_line, line)
  style == :crlf && index == last_line - 1 && line !~ /\n$/
end