class RubbyCop::Cop::Style::Encoding

This cop checks whether the source file has a utf-8 encoding comment or not. Setting this check to “always” and “when_needed” makes sense only for code that should support Ruby 1.9, since in 2.0+ utf-8 is the default source file encoding. There are three styles:

when_needed - only enforce an encoding comment if there are non ASCII

characters, otherwise report an offense

always - enforce encoding comment in all files never - enforce no encoding comment in all files

Constants

AUTO_CORRECT_ENCODING_COMMENT
ENCODING_PATTERN
MSG_MISSING
MSG_UNNECESSARY
SHEBANG

Public Instance Methods

autocorrect(range) click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 35
def autocorrect(range)
  if @message == MSG_MISSING
    raise encoding_mismatch_message unless matching_encoding?

    lambda do |corrector|
      corrector.insert_before(range, "#{encoding}\n")
    end
  else
    # Need to remove unnecessary encoding comment
    lambda do |corrector|
      corrector.remove(range_with_surrounding_space(range, :right))
    end
  end
end
investigate(processed_source) click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 25
def investigate(processed_source)
  return if processed_source.buffer.source.empty?

  line_number = encoding_line_number(processed_source)
  return unless (@message = offense(processed_source, line_number))

  range = processed_source.buffer.line_range(line_number + 1)
  add_offense(range, range, @message)
end

Private Instance Methods

encoding() click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 52
def encoding
  cop_config[AUTO_CORRECT_ENCODING_COMMENT]
end
encoding_line_number(processed_source) click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 84
def encoding_line_number(processed_source)
  line_number = 0
  line_number += 1 if processed_source[line_number].start_with?(SHEBANG)
  line_number
end
encoding_mismatch_message() click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 60
def encoding_mismatch_message
  "#{encoding} does not match #{ENCODING_PATTERN}"
end
encoding_omitable?() click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 78
def encoding_omitable?
  return true if style == :never

  style != :always && processed_source.buffer.source.ascii_only?
end
encoding_present?(line) click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 74
def encoding_present?(line)
  MagicComment.parse(line).encoding
end
matching_encoding?() click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 56
def matching_encoding?
  encoding =~ ENCODING_PATTERN
end
offense(processed_source, line_number) click to toggle source
# File lib/rubbycop/cop/style/encoding.rb, line 64
def offense(processed_source, line_number)
  line = processed_source[line_number]

  if !encoding_present?(line) && !encoding_omitable?
    MSG_MISSING
  elsif encoding_present?(line) && encoding_omitable?
    MSG_UNNECESSARY
  end
end