-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
InternalAffairs/EmptyLineBetweenExpectOffenseAndCorrection
cop
This PR adds new `InternalAffairs/EmptyLineBetweenExpectOffenseAndCorrection` cop. It checks whether `expect_offense` and correction expectation methods (i.e. `expect_correction` and `expect_no_corrections`) are separated by empty line. ```ruby # bad it 'registers and corrects an offense' do expect_offense(<<~RUBY) bad_method ^^^^^^^^^^ Use `good_method`. RUBY expect_correction(<<~RUBY) good_method RUBY end # good it 'registers and corrects an offense' do expect_offense(<<~RUBY) bad_method ^^^^^^^^^^ Use `good_method`. RUBY expect_correction(<<~RUBY) good_method RUBY end ``` No changelog entry has been added to the changelog due to this cop is for use inside development.
- Loading branch information
Showing
55 changed files
with
567 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module InternalAffairs | ||
# This cop checks whether `expect_offense` and correction expectation methods | ||
# (i.e. `expect_correction` and `expect_no_corrections`) are separated by empty line. | ||
# | ||
# @example | ||
# # bad | ||
# it 'registers and corrects an offense' do | ||
# expect_offense(<<~RUBY) | ||
# bad_method | ||
# ^^^^^^^^^^ Use `good_method`. | ||
# RUBY | ||
# expect_correction(<<~RUBY) | ||
# good_method | ||
# RUBY | ||
# end | ||
# | ||
# # good | ||
# it 'registers and corrects an offense' do | ||
# expect_offense(<<~RUBY) | ||
# bad_method | ||
# ^^^^^^^^^^ Use `good_method`. | ||
# RUBY | ||
# | ||
# expect_correction(<<~RUBY) | ||
# good_method | ||
# RUBY | ||
# end | ||
# | ||
class EmptyLineBetweenExpectOffenseAndCorrection < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Add empty line between `expect_offense` and `%<expect_correction>s`.' | ||
RESTRICT_ON_SEND = %i[expect_offense].freeze | ||
CORRECTION_EXPECTATION_METHODS = %i[expect_correction expect_no_corrections].freeze | ||
|
||
def on_send(node) | ||
return unless (next_sibling = node.right_sibling) && next_sibling.send_type? | ||
|
||
method_name = next_sibling.method_name | ||
return unless CORRECTION_EXPECTATION_METHODS.include?(method_name) | ||
|
||
range = offense_range(node) | ||
return unless range.last_line + 1 == next_sibling.loc.line | ||
|
||
add_offense(range, message: format(MSG, expect_correction: method_name)) do |corrector| | ||
corrector.insert_after(range, "\n") | ||
end | ||
end | ||
|
||
private | ||
|
||
def offense_range(node) | ||
first_argument = node.first_argument | ||
|
||
if first_argument.respond_to?(:heredoc?) && first_argument.heredoc? | ||
first_argument.loc.heredoc_end | ||
else | ||
node | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
93 changes: 93 additions & 0 deletions
93
spec/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::InternalAffairs::EmptyLineBetweenExpectOffenseAndCorrection, :config do | ||
it 'registers and corrects an offense when using no empty line between `expect_offense` and `expect_correction` ' \ | ||
'with heredoc argument' do | ||
expect_offense(<<~RUBY) | ||
expect_offense(<<~CODE) | ||
bad_method | ||
CODE | ||
^^^^ Add empty line between `expect_offense` and `expect_correction`. | ||
expect_correction(<<~CODE) | ||
good_good | ||
CODE | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect_offense(<<~CODE) | ||
bad_method | ||
CODE | ||
expect_correction(<<~CODE) | ||
good_good | ||
CODE | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when using no empty line between `expect_offense` and `expect_correction`' \ | ||
'with variable argument' do | ||
expect_offense(<<~RUBY) | ||
expect_offense(code) | ||
^^^^^^^^^^^^^^^^^^^^ Add empty line between `expect_offense` and `expect_correction`. | ||
expect_correction(code) | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect_offense(code) | ||
expect_correction(code) | ||
RUBY | ||
end | ||
|
||
it 'registers and corrects an offense when using no empty line between `expect_offense` and `expect_no_corrections`' do | ||
expect_offense(<<~RUBY) | ||
expect_offense('bad_method') | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Add empty line between `expect_offense` and `expect_no_corrections`. | ||
expect_no_corrections | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
expect_offense('bad_method') | ||
expect_no_corrections | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using only `expect_offense`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_offense(<<~CODE) | ||
bad_method | ||
CODE | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using empty line between `expect_offense` and `expect_correction` ' \ | ||
'with heredoc argument' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_offense(<<~CODE) | ||
bad_method | ||
CODE | ||
expect_correction(<<~CODE) | ||
good_method | ||
CODE | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using empty line between `expect_offense` and `expect_correction`' \ | ||
'with variable argument' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_offense(bad_method) | ||
expect_correction(good_method) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using empty line between `expect_offense` and `expect_no_corrections`' do | ||
expect_no_offenses(<<~RUBY) | ||
expect_offense('bad_method') | ||
expect_no_corrections | ||
RUBY | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ | |
^ #{message} | ||
^ #{message} | ||
RUBY | ||
|
||
expect_correction("#{code_example('\ a b c\ ')}\n") | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,6 +87,7 @@ | |
RUBY | ||
|
||
expect_correction("x = 0\n") | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.