Add a DisableComment Cop
Co-authored-by: Issy Long <issyl0@github.com>
This commit is contained in:
parent
3b0794a884
commit
4acdcfcb37
@ -5,6 +5,7 @@ require_relative "../extend/array"
|
||||
require_relative "../extend/blank"
|
||||
require_relative "blank"
|
||||
require_relative "compact_blank"
|
||||
require_relative "disable_comment"
|
||||
require_relative "extend/mutable_constant_exclude_unfreezable"
|
||||
require_relative "io_read"
|
||||
require_relative "move_to_extend_os"
|
||||
|
35
Library/Homebrew/rubocops/disable_comment.rb
Normal file
35
Library/Homebrew/rubocops/disable_comment.rb
Normal file
@ -0,0 +1,35 @@
|
||||
# typed: strict
|
||||
# frozen_string_literal: true
|
||||
|
||||
module RuboCop
|
||||
module Cop
|
||||
# Checks if rubocop disable comments have a clarifying comment preceding them.
|
||||
class DisableComment < Base
|
||||
MSG = "Add a clarifying comment to the RuboCop disable comment"
|
||||
|
||||
sig { void }
|
||||
def on_new_investigation
|
||||
super
|
||||
|
||||
processed_source.comments.each do |comment|
|
||||
next unless disable_comment?(comment)
|
||||
next if comment?(processed_source[comment.loc.line - 2])
|
||||
|
||||
add_offense(comment)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
sig { params(comment: Parser::Source::Comment).returns(T::Boolean)
|
||||
def disable_comment?(comment)
|
||||
comment.text.start_with? "# rubocop:disable"
|
||||
end
|
||||
|
||||
sig { params(line: String).returns(T::Boolean) }
|
||||
def comment?(line)
|
||||
line.strip.start_with? "#"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
36
Library/Homebrew/test/rubocops/disable_comment_spec.rb
Normal file
36
Library/Homebrew/test/rubocops/disable_comment_spec.rb
Normal file
@ -0,0 +1,36 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "rubocops/disable_comment"
|
||||
|
||||
RSpec.describe RuboCop::Cop::DisableComment, :config do
|
||||
shared_examples "offense" do |source, correction, message|
|
||||
it "registers an offense and corrects" do
|
||||
expect_offense(<<~RUBY, source:, message:)
|
||||
#{source}
|
||||
^{source} #{message}
|
||||
RUBY
|
||||
|
||||
expect_correction(<<~RUBY)
|
||||
#{correction}
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
|
||||
it "registers an offense" do
|
||||
expect_offense(<<~RUBY)
|
||||
def something; end
|
||||
# rubocop:disable Naming/AccessorMethodName
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add a clarifying comment to the RuboCop disable comment
|
||||
def get_decrypted_io; end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "doesn't register an offense" do
|
||||
expect_no_offenses(<<~RUBY)
|
||||
def something; end
|
||||
# This is a upstream name that we cannot change.
|
||||
# rubocop:disable Naming/AccessorMethodName
|
||||
def get_decrypted_io; end
|
||||
RUBY
|
||||
end
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user