-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
base: master
Are you sure you want to change the base?
Add a DisableComment Cop #18842
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,32 @@ | ||||||||
# 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" | ||||||||
|
||||||||
def on_new_investigation | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
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 | ||||||||
|
||||||||
def disable_comment?(comment) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
comment.text.start_with? "# rubocop:disable" | ||||||||
end | ||||||||
|
||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
def comment?(line) | ||||||||
line.strip.start_with? "#" | ||||||||
end | ||||||||
end | ||||||||
end | ||||||||
end |
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit)
Suggested change
|
||||||
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (nit)
Suggested change
|
||||||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this is Sorbet
typed: strict
, Iβve added type signatures in-line.