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

Return global offenses for Style/Copyright when the file is empty #12804

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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