From 69ad827e829175bebb985c8afe76174f42fc60bc Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Wed, 8 Feb 2017 17:09:16 -0500 Subject: Add a custom RSpec/SingleLineHook cop This cop adds an offense when `before`, `after`, or `around` are used as single-line blocks. --- rubocop/cop/rspec/single_line_hook.rb | 36 +++++++++++++++++++++++++++++++++++ rubocop/rubocop.rb | 1 + 2 files changed, 37 insertions(+) create mode 100644 rubocop/cop/rspec/single_line_hook.rb (limited to 'rubocop') 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 diff --git a/rubocop/rubocop.rb b/rubocop/rubocop.rb index 6b8d127dde6..55d7708fa8c 100644 --- a/rubocop/rubocop.rb +++ b/rubocop/rubocop.rb @@ -15,3 +15,4 @@ require_relative 'cop/migration/remove_index' require_relative 'cop/migration/reversible_add_column_with_default' require_relative 'cop/migration/timestamps' require_relative 'cop/migration/update_column_in_batches' +require_relative 'cop/rspec/single_line_hook' -- cgit v1.2.1