Skip to content

Commit

Permalink
Return global offenses for Style/Copyright when the file is empty.
Browse files Browse the repository at this point in the history
Very similar to #12802

This adds a check that the returned offenses have a location that formatters can display.
While the standard formatter catched the `IndexError`, barely any others did.

Since it is not possible to autocorrect global offenses, just don't do that. Adding autocorrect support would be a bunch more work.
  • Loading branch information
Earlopain authored and bbatsov committed Mar 25, 2024
1 parent 47696e6 commit 8dab836
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
@@ -0,0 +1 @@
* [#12804](https://github.com/rubocop/rubocop/pull/12804): Return global offenses for `Style/Copyright` when the file is empty. ([@earlopain][])
27 changes: 16 additions & 11 deletions lib/rubocop/cop/style/copyright.rb
Expand Up @@ -28,18 +28,27 @@ class Copyright < Base
def on_new_investigation
return if notice.empty? || notice_found?(processed_source)

add_offense(offense_range, message: format(MSG, notice: notice)) do |corrector|
verify_autocorrect_notice!

token = insert_notice_before(processed_source)
range = token.nil? ? range_between(0, 0) : token.pos

corrector.insert_before(range, "#{autocorrect_notice}\n")
verify_autocorrect_notice!
message = format(MSG, notice: notice)
if processed_source.blank?
add_global_offense(message)
else
offense_range = source_range(processed_source.buffer, 1, 0)
add_offense(offense_range, message: message) do |corrector|
autocorrect(corrector)
end
end
end

private

def autocorrect(corrector)
token = insert_notice_before(processed_source)
range = token.nil? ? range_between(0, 0) : token.pos

corrector.insert_before(range, "#{autocorrect_notice}\n")
end

def notice
cop_config['Notice']
end
Expand All @@ -48,10 +57,6 @@ def autocorrect_notice
cop_config['AutocorrectNotice']
end

def offense_range
source_range(processed_source.buffer, 1, 0)
end

def verify_autocorrect_notice!
raise Warning, AUTOCORRECT_EMPTY_WARNING if autocorrect_notice.empty?

Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/formatter/clang_style_formatter.rb
Expand Up @@ -24,14 +24,10 @@ def report_offense(file, offense)
message: message(offense)
)

begin
return unless valid_line?(offense)
return unless valid_line?(offense)

report_line(offense.location)
report_highlighted_area(offense.highlighted_area)
rescue IndexError
# range is not on a valid line; perhaps the source file is empty
end
report_line(offense.location)
report_highlighted_area(offense.highlighted_area)
end

def valid_line?(offense)
Expand Down
10 changes: 3 additions & 7 deletions lib/rubocop/formatter/tap_formatter.rb
Expand Up @@ -53,14 +53,10 @@ def report_offense(file, offense)
message: message(offense)
)

begin
return unless valid_line?(offense)
return unless valid_line?(offense)

report_line(offense.location)
report_highlighted_area(offense.highlighted_area)
rescue IndexError
# range is not on a valid line; perhaps the source file is empty
end
report_line(offense.location)
report_highlighted_area(offense.highlighted_area)
end

def annotate_message(msg)
Expand Down
8 changes: 8 additions & 0 deletions lib/rubocop/rspec/expect_offense.rb
Expand Up @@ -111,6 +111,7 @@ def format_offense(source, **replacements)
source
end

# rubocop:disable Metrics/AbcSize
def expect_offense(source, file = nil, severity: nil, chomp: false, **replacements)
expected_annotations = parse_annotations(source, **replacements)
source = expected_annotations.plain_source
Expand All @@ -123,8 +124,15 @@ def expect_offense(source, file = nil, severity: nil, chomp: false, **replacemen
expect(actual_annotations).to eq(expected_annotations), ''
expect(@offenses.map(&:severity).uniq).to eq([severity]) if severity

# Validate that all offenses have a range that formatters can display
expect do
@offenses.each { |offense| offense.location.source_line }
end.not_to raise_error, 'One of the offenses has a misconstructed range, for ' \
'example if the offense is on line 1 and the source is empty'

@offenses
end
# rubocop:enable Metrics/AbcSize

# rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
def expect_correction(correction, loop: true, source: nil)
Expand Down
6 changes: 2 additions & 4 deletions spec/rubocop/cop/style/copyright_spec.rb
Expand Up @@ -93,12 +93,10 @@
cop_config['AutocorrectNotice'] = '# Copyright (c) 2015 Acme Inc.'

expect_offense(<<~RUBY)
^ Include a copyright notice matching [...]
^{} Include a copyright notice matching [...]
RUBY

expect_correction(<<~RUBY)
# Copyright (c) 2015 Acme Inc.
RUBY
expect_no_corrections
end
end

Expand Down

0 comments on commit 8dab836

Please sign in to comment.