Skip to content

Commit

Permalink
[Fix rubocop#12835] Allow global offenses to be disabled by directive…
Browse files Browse the repository at this point in the history
… comments

`Naming/FileName` now returns global offenses but there are quite a few projects that
disable the cop with a comment. The change to global offenses broke that behaviour.
  • Loading branch information
Earlopain committed Apr 12, 2024
1 parent c6e0d4b commit b0eae84
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_global_offenses_directive_comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12835](https://github.com/rubocop/rubocop/issues/12835): Allow global offenses to be disabled by directive comments. ([@earlopain][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ def message(_range = nil)
def add_global_offense(message = nil, severity: nil)
severity = find_severity(nil, severity)
message = find_message(nil, message)
current_offenses << Offense.new(severity, Offense::NO_LOCATION, message, name, :unsupported)
range = Offense::NO_LOCATION
status = enabled_line?(range.line) ? :unsupported : :disabled
current_offenses << Offense.new(severity, range, message, name, status)
end

# Adds an offense on the specified range (or node with an expression)
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,25 @@ def and_with_args
$stdout.string.include?('F: 2: 7: Lint/Syntax: unexpected token tINTEGER')
).to be(true)
end

it '`Naming/FileName` must be be disabled for global offenses' do
create_file('Example.rb', <<~RUBY)
# rubocop:disable Naming/FileName
RUBY

expect(cli.run(['--format', 'simple', 'Example.rb'])).to eq(1)
expect($stdout.string.include?('Naming/FileName')).to be(false)
end

it '`Naming/FileName` must be be enabled if directive comment is on unrelated line' do
create_file('Example.rb', <<~RUBY)
# Prelude
# rubocop:disable Naming/FileName
RUBY

expect(cli.run(['--format', 'simple', 'Example.rb'])).to eq(1)
expect($stdout.string.include?('Naming/FileName')).to be(true)
end
end

describe 'rubocop:disable comment' do
Expand Down

0 comments on commit b0eae84

Please sign in to comment.