Skip to content

Commit

Permalink
Improve FrozenStringLiteralComment autocorrection.
Browse files Browse the repository at this point in the history
By inserting before the whole document, the insertion will happen before all corrections
except possibly those that also act on the whole document.
  • Loading branch information
marcandre committed May 13, 2020
1 parent 18bb337 commit d525092
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/frozen_string_literal_comment.rb
Expand Up @@ -214,7 +214,7 @@ def insert_comment(corrector)
if comment
corrector.insert_after(line_range(comment.line), following_comment)
else
corrector.insert_before(line_range(1), preceding_comment)
corrector.insert_before(processed_source.buffer.source_range, preceding_comment)
end
end

Expand Down
7 changes: 5 additions & 2 deletions spec/rubocop/cli/cli_disable_uncorrectable_spec.rb
Expand Up @@ -19,6 +19,7 @@
YAML
create_file('example.rb', <<~RUBY)
ip('1.2.3.4')
# last line
RUBY
end
let(:max_length) { 46 }
Expand Down Expand Up @@ -59,6 +60,7 @@
# frozen_string_literal: true
ip('1.2.3.4') # rubocop:todo Style/IpAddresses
# last line
RUBY
end

Expand Down Expand Up @@ -171,16 +173,17 @@ def choose_move(who_to_move) # rubocop:todo Metrics/CyclomaticComplexity
== example.rb ==
C: 1: 1: [Corrected] Style/FrozenStringLiteralComment: Missing frozen string literal comment.
C: 1: 4: [Todo] Style/IpAddresses: Do not hardcode IP addresses.
C: 3: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
C: 2: 1: [Corrected] Layout/EmptyLineAfterMagicComment: Add an empty line after magic comments.
1 file inspected, 3 offenses detected, 3 offenses corrected
OUTPUT
expect(IO.read('example.rb')).to eq(<<~RUBY)
# rubocop:todo Style/IpAddresses
# frozen_string_literal: true
# rubocop:todo Style/IpAddresses
ip('1.2.3.4')
# rubocop:enable Style/IpAddresses
# last line
RUBY
end
end
Expand Down

0 comments on commit d525092

Please sign in to comment.