File tree Expand file tree Collapse file tree 7 files changed +193
-0
lines changed Expand file tree Collapse file tree 7 files changed +193
-0
lines changed Original file line number Diff line number Diff line change 66
77* [ #521 ] ( https://github.com/rubocop/rubocop-rails/pull/521 ) : Support auto-correction for ` Rails/Output ` . ([ @koic ] [ ] )
88* [ #520 ] ( https://github.com/rubocop/rubocop-rails/pull/520 ) : Support auto-correction for ` Rails/ScopeArgs ` . ([ @koic ] [ ] )
9+ * [ #524 ] ( https://github.com/rubocop/rubocop-rails/pull/524 ) : Add new ` Rails/RedundantTravelBack ` cop. ([ @koic ] [ ] )
910
1011## 2.11.3 (2021-07-11)
1112
Original file line number Diff line number Diff line change @@ -608,6 +608,14 @@ Rails/RedundantReceiverInWithOptions:
608608 Enabled : true
609609 VersionAdded : ' 0.52'
610610
611+ Rails/RedundantTravelBack :
612+ Description : This cop checks for redundant `travel_back` calls.
613+ Enabled : pending
614+ VersionAdded : ' 2.12'
615+ Include :
616+ - spec/**/*.rb
617+ - test/**/*.rb
618+
611619Rails/ReflectionClassName :
612620 Description : ' Use a string for `class_name` option value in the definition of a reflection.'
613621 Enabled : true
Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ based on the https://rails.rubystyle.guide/[Rails Style Guide].
8383* xref:cops_rails.adoc#railsredundantallownil[Rails/RedundantAllowNil]
8484* xref:cops_rails.adoc#railsredundantforeignkey[Rails/RedundantForeignKey]
8585* xref:cops_rails.adoc#railsredundantreceiverinwithoptions[Rails/RedundantReceiverInWithOptions]
86+ * xref:cops_rails.adoc#railsredundanttravelback[Rails/RedundantTravelBack]
8687* xref:cops_rails.adoc#railsreflectionclassname[Rails/ReflectionClassName]
8788* xref:cops_rails.adoc#railsrefutemethods[Rails/RefuteMethods]
8889* xref:cops_rails.adoc#railsrelativedateconstant[Rails/RelativeDateConstant]
Original file line number Diff line number Diff line change @@ -3452,6 +3452,58 @@ with_options options: false do |merger|
34523452end
34533453----
34543454
3455+ == Rails/RedundantTravelBack
3456+
3457+ |===
3458+ | Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
3459+
3460+ | Pending
3461+ | Yes
3462+ | Yes
3463+ | 2.12
3464+ | -
3465+ |===
3466+
3467+ This cop checks for redundant `travel_back` calls.
3468+ Since Rails 5.2, `travel_back` is automatically called at the end of the test.
3469+
3470+ === Examples
3471+
3472+ [source,ruby]
3473+ ----
3474+ # bad
3475+ def teardown
3476+ do_something
3477+ travel_back
3478+ end
3479+
3480+ # good
3481+ def teardown
3482+ do_something
3483+ end
3484+
3485+ # bad
3486+ after do
3487+ do_something
3488+ travel_back
3489+ end
3490+
3491+ # good
3492+ after do
3493+ do_something
3494+ end
3495+ ----
3496+
3497+ === Configurable attributes
3498+
3499+ |===
3500+ | Name | Default value | Configurable values
3501+
3502+ | Include
3503+ | `spec/**/*.rb`, `test/**/*.rb`
3504+ | Array
3505+ |===
3506+
34553507== Rails/ReflectionClassName
34563508
34573509|===
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ module RuboCop
4+ module Cop
5+ module Rails
6+ # This cop checks for redundant `travel_back` calls.
7+ # Since Rails 5.2, `travel_back` is automatically called at the end of the test.
8+ #
9+ # @example
10+ #
11+ # # bad
12+ # def teardown
13+ # do_something
14+ # travel_back
15+ # end
16+ #
17+ # # good
18+ # def teardown
19+ # do_something
20+ # end
21+ #
22+ # # bad
23+ # after do
24+ # do_something
25+ # travel_back
26+ # end
27+ #
28+ # # good
29+ # after do
30+ # do_something
31+ # end
32+ #
33+ class RedundantTravelBack < Base
34+ include RangeHelp
35+ extend AutoCorrector
36+ extend TargetRailsVersion
37+
38+ minimum_target_rails_version 5.2
39+
40+ MSG = 'Redundant `travel_back` detected.'
41+ RESTRICT_ON_SEND = %i[ travel_back ] . freeze
42+
43+ def on_send ( node )
44+ return unless node . each_ancestor ( :def , :block ) . any? do |ancestor |
45+ method_name = ancestor . def_type? ? :teardown : :after
46+
47+ ancestor . method? ( method_name )
48+ end
49+
50+ add_offense ( node ) do |corrector |
51+ corrector . remove ( range_by_whole_lines ( node . source_range , include_final_newline : true ) )
52+ end
53+ end
54+ end
55+ end
56+ end
57+ end
Original file line number Diff line number Diff line change 7272require_relative 'rails/redundant_allow_nil'
7373require_relative 'rails/redundant_foreign_key'
7474require_relative 'rails/redundant_receiver_in_with_options'
75+ require_relative 'rails/redundant_travel_back'
7576require_relative 'rails/reflection_class_name'
7677require_relative 'rails/refute_methods'
7778require_relative 'rails/relative_date_constant'
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ RSpec . describe RuboCop ::Cop ::Rails ::RedundantTravelBack , :config do
4+ context '>= Rails 5.2' , :rails52 do
5+ it 'registers and corrects an offense when using `travel_back` in `teardown` method' do
6+ expect_offense ( <<~RUBY )
7+ def teardown
8+ do_something
9+ travel_back
10+ ^^^^^^^^^^^ Redundant `travel_back` detected.
11+ end
12+ RUBY
13+
14+ expect_correction ( <<~RUBY )
15+ def teardown
16+ do_something
17+ end
18+ RUBY
19+ end
20+
21+ it 'registers and corrects an offense when using `travel_back` in `after` block' do
22+ expect_offense ( <<~RUBY )
23+ after do
24+ do_something
25+ travel_back
26+ ^^^^^^^^^^^ Redundant `travel_back` detected.
27+ end
28+ RUBY
29+
30+ expect_correction ( <<~RUBY )
31+ after do
32+ do_something
33+ end
34+ RUBY
35+ end
36+
37+ it 'does not register an offense when using `travel_back` outside of `teardown` method' do
38+ expect_no_offenses ( <<~RUBY )
39+ def do_something
40+ travel_back
41+ end
42+ RUBY
43+ end
44+
45+ it 'does not register an offense when using `travel_back` outside of `after` block' do
46+ expect_no_offenses ( <<~RUBY )
47+ do_something do
48+ travel_back
49+ end
50+ RUBY
51+ end
52+ end
53+
54+ context '<= Rails 5.1' , :rails51 do
55+ it 'does not register an offense when using `travel_back` in `teardown` method' do
56+ expect_no_offenses ( <<~RUBY )
57+ def teardown
58+ do_something
59+ travel_back
60+ end
61+ RUBY
62+ end
63+
64+ it 'does not register an offense when using `travel_back` in `after` block' do
65+ expect_no_offenses ( <<~RUBY )
66+ after do
67+ do_something
68+ travel_back
69+ end
70+ RUBY
71+ end
72+ end
73+ end
You can’t perform that action at this time.
0 commit comments