Skip to content

Commit

Permalink
[Fix rubocop#28] Fix Rake/Desc to avoid an error when task is not…
Browse files Browse the repository at this point in the history
… a task definition
  • Loading branch information
pocke committed Feb 8, 2020
1 parent 0d82e93 commit 936d936
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/rubocop/cop/rake/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion spec/rubocop/cop/rake/desc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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

0 comments on commit 936d936

Please sign in to comment.