/
disable_cops_within_source_code_directive.rb
49 lines (42 loc) · 1.25 KB
/
disable_cops_within_source_code_directive.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
# frozen_string_literal: true
# rubocop:disable Lint/RedundantCopDisableDirective
module RuboCop
module Cop
module Style
# Detects comments to enable/disable RuboCop.
# This is useful if want to make sure that every RuboCop error gets fixed
# and not quickly disabled with a comment.
#
# @example
# # bad
# # rubocop:disable Metrics/AbcSize
# def f
# end
# # rubocop:enable Metrics/AbcSize
#
# # good
# def fixed_method_name_and_no_rubocop_comments
# end
#
class DisableCopsWithinSourceCodeDirective < Cop
# rubocop:enable Lint/RedundantCopDisableDirective
MSG = 'Comment to disable/enable RuboCop.'
def investigate(processed_source)
processed_source.comments.each do |comment|
next unless rubocop_directive_comment?(comment)
add_offense(comment)
end
end
def autocorrect(comment)
lambda do |corrector|
corrector.replace(comment.loc.expression, '')
end
end
private
def rubocop_directive_comment?(comment)
comment.text =~ CommentConfig::COMMENT_DIRECTIVE_REGEXP
end
end
end
end
end