forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 2
/
empty_line_between_expect_offense_and_correction.rb
68 lines (59 loc) · 1.98 KB
/
empty_line_between_expect_offense_and_correction.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# frozen_string_literal: true
module RuboCop
module Cop
module InternalAffairs
# 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