Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a DisableComment Cop #18842

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Library/Homebrew/rubocops/all.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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"
Expand Down
32 changes: 32 additions & 0 deletions Library/Homebrew/rubocops/disable_comment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# typed: strict
koddsson marked this conversation as resolved.
Show resolved Hide resolved
# 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"

def on_new_investigation
koddsson marked this conversation as resolved.
Show resolved Hide resolved
super

processed_source.comments.each do |comment|
next unless disable_comment?(comment)
next if comment?(processed_source[comment.loc.line - 2])
Copy link
Member

@issyl0 issyl0 Jan 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed when I was fixing the offenses for this cop in #19084 that this passes if the comment line is only β€œ#” with no words. Is that intentional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@issyl0 Whoops, don't think so!


add_offense(comment)
end
end

private

def disable_comment?(comment)
koddsson marked this conversation as resolved.
Show resolved Hide resolved
comment.text.start_with? "# rubocop:disable"
end

koddsson marked this conversation as resolved.
Show resolved Hide resolved
def comment?(line)
line.strip.start_with? "#"
end
end
end
end
36 changes: 36 additions & 0 deletions Library/Homebrew/test/rubocops/disable_comment_spec.rb
Original file line number Diff line number Diff line change
@@ -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 "register a offencse" do
koddsson marked this conversation as resolved.
Show resolved Hide resolved
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 offencse" do
koddsson marked this conversation as resolved.
Show resolved Hide resolved
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