Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2017-02-09 01:09:16 +0300
committerRobert Speicher <rspeicher@gmail.com>2017-06-14 21:16:44 +0300
commit69ad827e829175bebb985c8afe76174f42fc60bc (patch)
tree635f61331140628d439497b36f2c67aeb570a648 /rubocop/cop
parent557cbba7df35e2f3a27995f1caf77d40a3c223fa (diff)
Add a custom RSpec/SingleLineHook cop
This cop adds an offense when `before`, `after`, or `around` are used as single-line blocks.
Diffstat (limited to 'rubocop/cop')
-rw-r--r--rubocop/cop/rspec/single_line_hook.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/rubocop/cop/rspec/single_line_hook.rb b/rubocop/cop/rspec/single_line_hook.rb
new file mode 100644
index 00000000000..a156b1d34cf
--- /dev/null
+++ b/rubocop/cop/rspec/single_line_hook.rb
@@ -0,0 +1,36 @@
+module RuboCop
+ module Cop
+ module RSpec
+ # This cop checks for single-line hook blocks
+ #
+ # @example
+ #
+ # # bad
+ # before { do_something }
+ # after(:each) { undo_something }
+ #
+ # # good
+ # before do
+ # do_something
+ # end
+ #
+ # after(:each) do
+ # undo_something
+ # end
+ class SingleLineHook < RuboCop::Cop::RSpec::Cop
+ MESSAGE = "Don't use single-line hook blocks.".freeze
+
+ def_node_search :rspec_hook?, <<~PATTERN
+ (send nil {:after :around :before} ...)
+ PATTERN
+
+ def on_block(node)
+ return unless rspec_hook?(node)
+ return unless node.single_line?
+
+ add_offense(node, :expression, MESSAGE)
+ end
+ end
+ end
+ end
+end