/
encoding.rb
63 lines (50 loc) · 1.67 KB
/
encoding.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks ensures source files have no utf-8 encoding comments.
# @example
# # bad
# # encoding: UTF-8
# # coding: UTF-8
# # -*- coding: UTF-8 -*-
class Encoding < Base
include RangeHelp
extend AutoCorrector
MSG = 'Unnecessary utf-8 encoding comment.'
ENCODING_PATTERN = /#.*coding\s?[:=]\s?(?:UTF|utf)-8/.freeze
SHEBANG = '#!'
def on_new_investigation
return if processed_source.buffer.source.empty?
comments.each do |line_number, comment|
next unless offense?(comment)
register_offense(line_number, comment)
end
end
private
def comments
processed_source.lines.each.with_index.with_object({}) do |(line, line_number), comments|
next if line.start_with?(SHEBANG)
comment = MagicComment.parse(line)
return comments unless comment.valid?
comments[line_number + 1] = comment
end
end
def offense?(comment)
comment.encoding_specified? && comment.encoding.casecmp('utf-8').zero?
end
def register_offense(line_number, comment)
range = processed_source.buffer.line_range(line_number)
add_offense(range) do |corrector|
text = comment.without(:encoding)
if text.blank?
corrector.remove(range_with_surrounding_space(range: range, side: :right))
else
corrector.replace(range, text)
end
end
end
end
end
end
end