diff --git a/lib/rubocop/cop/style/frozen_string_literal_comment.rb b/lib/rubocop/cop/style/frozen_string_literal_comment.rb index 30b51608376..51bc65c0b22 100644 --- a/lib/rubocop/cop/style/frozen_string_literal_comment.rb +++ b/lib/rubocop/cop/style/frozen_string_literal_comment.rb @@ -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 diff --git a/spec/rubocop/cli/cli_disable_uncorrectable_spec.rb b/spec/rubocop/cli/cli_disable_uncorrectable_spec.rb index cf7980356e7..5dfc1f55b83 100644 --- a/spec/rubocop/cli/cli_disable_uncorrectable_spec.rb +++ b/spec/rubocop/cli/cli_disable_uncorrectable_spec.rb @@ -19,6 +19,7 @@ YAML create_file('example.rb', <<~RUBY) ip('1.2.3.4') + # last line RUBY end let(:max_length) { 46 } @@ -59,6 +60,7 @@ # frozen_string_literal: true ip('1.2.3.4') # rubocop:todo Style/IpAddresses + # last line RUBY end @@ -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