Skip to content
This repository has been archived by the owner on Feb 19, 2024. It is now read-only.

Commit

Permalink
Add expect_offense mark for offense on empty line
Browse files Browse the repository at this point in the history
Part of rubocop#8003

Use `^{}` to denote an offense at an empty line in `expect_offense`.

Example:

```ruby
expect_offense(<<~RUBY)

  ^{} Missing frozen string literal comment.
  puts 1
RUBY
```

Enable pending frozen string literal specs

Correct which line the offense is added to (always the first line).

As the first line is non-empty in these cases, use a normal caret `^` to
mark the offense.
  • Loading branch information
biinari committed Jul 7, 2020
1 parent cebe471 commit 8a43530
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
13 changes: 12 additions & 1 deletion lib/rubocop/rspec/expect_offense.rb
Expand Up @@ -89,6 +89,16 @@ module RSpec
# end
# RUBY
# end
#
# If you need to specify an offense on a blank line, use the empty `^{}` marker:
#
# @example `^{}` empty line offense
#
# expect_offense(<<~RUBY)
#
# ^{} Missing frozen string literal comment.
# puts 1
# RUBY
module ExpectOffense
def format_offense(source, **replacements)
replacements.each do |keyword, value|
Expand Down Expand Up @@ -177,7 +187,7 @@ def expect_no_offenses(source, file = nil)

# Parsed representation of code annotated with the `^^^ Message` style
class AnnotatedSource
ANNOTATION_PATTERN = /\A\s*\^+ /.freeze
ANNOTATION_PATTERN = /\A\s*(\^+|\^{}) /.freeze

# @param annotated_source [String] string passed to the matchers
#
Expand Down Expand Up @@ -261,6 +271,7 @@ def with_offense_annotations(offenses)
offenses.map do |offense|
indent = ' ' * offense.column
carets = '^' * offense.column_length
carets = '^{}' if offense.column_length.zero?

[offense.line, "#{indent}#{carets} #{offense.message}\n"]
end
Expand Down
20 changes: 4 additions & 16 deletions spec/rubocop/cop/style/frozen_string_literal_comment_spec.rb
Expand Up @@ -273,12 +273,9 @@
end

it 'registers an offense for an extra first empty line' do
pending 'There is a flaw that skips adding caret symbol in this case, ' \
'making it impossible to use `expect_offense` matcher'

expect_offense(<<~RUBY)
^ Missing magic comment `# frozen_string_literal: true`.
^{} Missing frozen string literal comment.
puts 1
RUBY

Expand Down Expand Up @@ -580,12 +577,9 @@
end

it 'registers an offense for an extra first empty line' do
pending 'There is a flaw that skips adding caret symbol in this case, ' \
'making it impossible to use `expect_offense` matcher'

expect_offense(<<~RUBY)
^ Missing magic comment `# frozen_string_literal: true`.
^{} Missing magic comment `# frozen_string_literal: true`.
puts 1
RUBY

Expand Down Expand Up @@ -854,14 +848,11 @@

it 'registers an offense for not having a frozen string literal comment ' \
'under a shebang, an encoding comment, and extra space' do
pending 'There is a flaw that skips adding caret symbol in this case, ' \
'making it impossible to use `expect_offense` matcher'

expect_offense(<<~RUBY)
#!/usr/bin/env ruby
^ Missing magic comment `# frozen_string_literal: true`.
# encoding: utf-8
^ Missing magic comment `# frozen_string_literal: true`.
puts 1
RUBY

Expand Down Expand Up @@ -927,13 +918,10 @@

it 'registers an offense for not having a frozen string literal comment ' \
'under an encoding comment and extra space' do
pending 'There is a flaw that skips adding caret symbol in this case, ' \
'making it impossible to use `expect_offense` matcher'

expect_offense(<<~RUBY)
# encoding: utf-8
^ Missing magic comment `# frozen_string_literal: true`.
puts 1
RUBY

Expand Down

0 comments on commit 8a43530

Please sign in to comment.