diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-03 12:08:44 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2023-11-03 12:08:44 +0300 |
commit | b4c39709e346f437a85829f985f6596cb6209d35 (patch) | |
tree | 010b87019e3f523045141f3ce683a1f5d4141499 /rubocop | |
parent | ea044b0c4c74b91c5d48435254e7fa60aea064ff (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'rubocop')
-rw-r--r-- | rubocop/cop/style/inline_disable_annotation.rb | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/rubocop/cop/style/inline_disable_annotation.rb b/rubocop/cop/style/inline_disable_annotation.rb new file mode 100644 index 00000000000..c3db541fe82 --- /dev/null +++ b/rubocop/cop/style/inline_disable_annotation.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +module RuboCop + module Cop + module Style + # rubocop:disable Lint/RedundantCopDisableDirective -- For examples + # Checks that rubocop inline disabling is formatted according + # to guidelines. + # See: https://docs.gitlab.com/ee/development/rubocop_development_guide.html#disabling-rules-inline, + # https://gitlab.com/gitlab-org/gitlab/-/issues/428762 + # + # # bad + # # rubocop:disable Some/Cop, Another/Cop + # + # # good + # # rubocop:disable Some/Cop, Another/Cop -- Some reason + # + # rubocop:enable Lint/RedundantCopDisableDirective + class InlineDisableAnnotation < RuboCop::Cop::Base + include RangeHelp + + COP_DISABLE = '#\s*rubocop\s*:\s*(?:disable|todo)\s+' + BAD_DISABLE = %r{\A(?<line>(?<disabling>#{COP_DISABLE}(?:[\w/]+(?:\s*,\s*[\w/]+)*))\s*)(?!.*\s*--\s\S).*} + COP_DISABLE_LINE = /\A(?<line>#{COP_DISABLE}.*)\Z/ + MSG = <<~MESSAGE + Inline disabling a cop needs to follow the format of `%{disable} -- Some reason`. + See https://docs.gitlab.com/ee/development/rubocop_development_guide.html#disabling-rules-inline. + MESSAGE + + def on_new_investigation + processed_source.comments.each do |comment| + candidate_match = COP_DISABLE_LINE.match(comment.text) + # Pre-filter to ensure we are on a comment that is for a rubocop disabling + next unless candidate_match + + bad_match = BAD_DISABLE.match(comment.text) + # Only the badly formatted lines make it past this. + next unless bad_match + + add_offense( + source_range( + processed_source.buffer, comment.loc.line, comment.loc.column, candidate_match[:line].length + ), + message: format(MSG, disable: bad_match[:disabling]) + ) + end + end + end + end + end +end |