diff --git a/lib/rubocop/cop/rake/desc.rb b/lib/rubocop/cop/rake/desc.rb index a7b0d40..802d387 100644 --- a/lib/rubocop/cop/rake/desc.rb +++ b/lib/rubocop/cop/rake/desc.rb @@ -42,6 +42,7 @@ def on_task(node) private def task_with_desc?(node) parent, task = parent_and_task(node) return false unless parent + return true unless can_insert_desc_to?(parent) idx = parent.children.find_index(task) - 1 desc_candidate = parent.children[idx] @@ -63,6 +64,10 @@ def on_task(node) [parent, task_node] end end + + private def can_insert_desc_to?(parent) + parent.begin_type? || parent.block_type? || parent.kwbegin_type? + end end end end diff --git a/spec/rubocop/cop/rake/desc_spec.rb b/spec/rubocop/cop/rake/desc_spec.rb index 05fac96..0ee7523 100644 --- a/spec/rubocop/cop/rake/desc_spec.rb +++ b/spec/rubocop/cop/rake/desc_spec.rb @@ -34,6 +34,15 @@ RUBY end + it 'register an offense for task in kwbegin' do + expect_offense(<<~RUBY) + begin + task :foo + ^^^^^^^^^ Describe the task with `desc` method. + end + RUBY + end + it 'does not register an offense for task with desc' do expect_no_offenses(<<~RUBY) desc 'Do foo' @@ -45,11 +54,17 @@ RUBY end - it 'do not register an offense for the default task' do + it 'does not register an offense for the default task' do expect_no_offenses(<<~RUBY) task default: :spec task default: [:spec, :rubocop] RUBY end + + it 'does not register an offense when `task` is not a definition' do + expect_no_offenses(<<~RUBY) + task.name + RUBY + end end