-
-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1503 from r7kamura/feature/freeze-time-around
Add `RSpec/Rails/TravelAround` and `RSpec/RedundantAround` cops
- Loading branch information
Showing
11 changed files
with
509 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
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
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
module Rails | ||
# Prefer to travel in `before` rather than `around`. | ||
# | ||
# @safety | ||
# This cop is unsafe because the automatic `travel_back` is only run | ||
# on test cases that are considered as Rails related. | ||
# | ||
# And also, this cop's autocorrection is unsafe because the order of | ||
# execution will change if other steps exist before traveling in | ||
# `around`. | ||
# | ||
# @example | ||
# # bad | ||
# around do |example| | ||
# freeze_time do | ||
# example.run | ||
# end | ||
# end | ||
# | ||
# # good | ||
# before { freeze_time } | ||
class TravelAround < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Prefer to travel in `before` rather than `around`.' | ||
|
||
TRAVEL_METHOD_NAMES = %i[ | ||
freeze_time | ||
travel | ||
travel_to | ||
].to_set.freeze | ||
|
||
# @!method extract_run_in_travel(node) | ||
def_node_matcher :extract_run_in_travel, <<~PATTERN | ||
(block | ||
$(send nil? TRAVEL_METHOD_NAMES ...) | ||
(args ...) | ||
(send _ :run) | ||
) | ||
PATTERN | ||
|
||
# @!method match_around_each?(node) | ||
def_node_matcher :match_around_each?, <<~PATTERN | ||
(block | ||
(send _ :around (sym :each)?) | ||
... | ||
) | ||
PATTERN | ||
|
||
def on_block(node) | ||
run_node = extract_run_in_travel(node) | ||
return unless run_node | ||
|
||
around_node = extract_surrounding_around_block(run_node) | ||
return unless around_node | ||
|
||
add_offense(node) do |corrector| | ||
autocorrect(corrector, node, run_node, around_node) | ||
end | ||
end | ||
alias on_numblock on_block | ||
|
||
private | ||
|
||
def autocorrect(corrector, node, run_node, around_node) | ||
corrector.replace( | ||
node, | ||
node.body.source | ||
) | ||
corrector.insert_before( | ||
around_node, | ||
"before { #{run_node.source} }\n\n" | ||
) | ||
end | ||
|
||
# @param node [RuboCop::AST::BlockNode] | ||
# @return [RuboCop::AST::BlockNode, nil] | ||
def extract_surrounding_around_block(node) | ||
node.each_ancestor(:block).find do |ancestor| | ||
match_around_each?(ancestor) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module RSpec | ||
# Remove redundant `around` hook. | ||
# | ||
# @example | ||
# # bad | ||
# around do |example| | ||
# example.run | ||
# end | ||
# | ||
# # good | ||
# | ||
class RedundantAround < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove redundant `around` hook.' | ||
|
||
RESTRICT_ON_SEND = %i[around].freeze | ||
|
||
def on_block(node) | ||
return unless match_redundant_around_hook_block?(node) | ||
|
||
add_offense(node) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
alias on_numblock on_block | ||
|
||
def on_send(node) | ||
return unless match_redundant_around_hook_send?(node) | ||
|
||
add_offense(node) do |corrector| | ||
autocorrect(corrector, node) | ||
end | ||
end | ||
|
||
private | ||
|
||
# @!method match_redundant_around_hook_block?(node) | ||
def_node_matcher :match_redundant_around_hook_block?, <<~PATTERN | ||
(block | ||
(send _ :around ...) | ||
(args _?) | ||
(send _ :run) | ||
) | ||
PATTERN | ||
|
||
# @!method match_redundant_around_hook_send?(node) | ||
def_node_matcher :match_redundant_around_hook_send?, <<~PATTERN | ||
(send | ||
_ | ||
:around | ||
... | ||
(block-pass | ||
(sym :run) | ||
) | ||
) | ||
PATTERN | ||
|
||
def autocorrect(corrector, node) | ||
corrector.remove(node) | ||
end | ||
end | ||
end | ||
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
Oops, something went wrong.