forked from rubocop/rubocop
/
department_name.rb
81 lines (61 loc) · 2.5 KB
/
department_name.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# frozen_string_literal: true
module RuboCop
module Cop
module Migration
# Check that cop names in rubocop:disable comments are given with
# department name.
class DepartmentName < Cop
include RangeHelp
MSG = 'Department name is missing.'
DISABLE_COMMENT_FORMAT =
/\A(# *rubocop *: *((dis|en)able|todo) +)(.*)/.freeze
# The token that makes up a disable comment.
# The allowed specification for comments after `# rubocop: disable` is
# `DepartmentName/CopName` or` all`.
DISABLING_COPS_CONTENT_TOKEN = %r{[A-z]+/[A-z]+|all}.freeze
def investigate(processed_source)
processed_source.each_comment do |comment|
next if comment.text !~ DISABLE_COMMENT_FORMAT
offset = Regexp.last_match(1).length
Regexp.last_match(4).scan(/[^,]+|[\W]+/) do |name|
trimmed_name = name.strip
check_cop_name(trimmed_name, comment, offset) unless valid_content_token?(trimmed_name)
break if contain_unexpected_character_for_department_name?(name)
offset += name.length
end
end
end
def autocorrect(range)
shall_warn = false
cop_name = range.source
qualified_cop_name = Cop.registry.qualified_cop_name(cop_name,
nil, shall_warn)
qualified_cop_name = qualified_legacy_cop_name(cop_name) unless qualified_cop_name.include?('/')
->(corrector) { corrector.replace(range, qualified_cop_name) }
end
private
def disable_comment_offset
Regexp.last_match(1).length
end
def check_cop_name(name, comment, offset)
start = comment.location.expression.begin_pos + offset
range = range_between(start, start + name.length)
add_offense(range, location: range)
end
def valid_content_token?(content_token)
!/\W+/.match(content_token).nil? ||
!DISABLING_COPS_CONTENT_TOKEN.match(content_token).nil?
end
def contain_unexpected_character_for_department_name?(name)
name.match?(%r{[^A-z/, ]})
end
def qualified_legacy_cop_name(cop_name)
legacy_cop_names = RuboCop::ConfigObsoletion::OBSOLETE_COPS.keys
legacy_cop_names.detect do |legacy_cop_name|
legacy_cop_name.split('/')[1] == cop_name
end
end
end
end
end
end