forked from rubocop/rubocop-rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redundant_travel_back.rb
57 lines (51 loc) · 1.29 KB
/
redundant_travel_back.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
# frozen_string_literal: true
module RuboCop
module Cop
module Rails
# Checks for redundant `travel_back` calls.
# Since Rails 5.2, `travel_back` is automatically called at the end of the test.
#
# @example
#
# # bad
# def teardown
# do_something
# travel_back
# end
#
# # good
# def teardown
# do_something
# end
#
# # bad
# after do
# do_something
# travel_back
# end
#
# # good
# after do
# do_something
# end
#
class RedundantTravelBack < Base
include RangeHelp
extend AutoCorrector
extend TargetRailsVersion
minimum_target_rails_version 5.2
MSG = 'Redundant `travel_back` detected.'
RESTRICT_ON_SEND = %i[travel_back].freeze
def on_send(node)
return unless node.each_ancestor(:def, :block).any? do |ancestor|
method_name = ancestor.def_type? ? :teardown : :after
ancestor.method?(method_name)
end
add_offense(node) do |corrector|
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
end
end
end
end
end
end