Skip to content

Commit 6388b59

Browse files
authored
Merge pull request #32 from pocke/fix-28
[Fix #28] Fix `Rake/Desc` to avoid an error when `task` is not a task definition
2 parents 0d82e93 + 925e827 commit 6388b59

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

CHANGELOG.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,9 @@
22

33
## master (unreleased)
44

5-
<!--
6-
7-
### New features
8-
95
### Bug fixes
106

11-
### Changes
12-
13-
-->
7+
* [#28](https://github.com/rubocop-hq/rubocop-rake/issues/28): Fix `Rake/Desc` to avoid an error when `task` is not a task definition. ([@pocke][])
148

159
## 0.5.0 (2019-10-31)
1610

lib/rubocop/cop/rake/desc.rb

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def on_task(node)
4242
private def task_with_desc?(node)
4343
parent, task = parent_and_task(node)
4444
return false unless parent
45+
return true unless can_insert_desc_to?(parent)
4546

4647
idx = parent.children.find_index(task) - 1
4748
desc_candidate = parent.children[idx]
@@ -63,6 +64,10 @@ def on_task(node)
6364
[parent, task_node]
6465
end
6566
end
67+
68+
private def can_insert_desc_to?(parent)
69+
parent.begin_type? || parent.block_type? || parent.kwbegin_type?
70+
end
6671
end
6772
end
6873
end

spec/rubocop/cop/rake/desc_spec.rb

+16-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@
3434
RUBY
3535
end
3636

37+
it 'register an offense for task in kwbegin' do
38+
expect_offense(<<~RUBY)
39+
begin
40+
task :foo
41+
^^^^^^^^^ Describe the task with `desc` method.
42+
end
43+
RUBY
44+
end
45+
3746
it 'does not register an offense for task with desc' do
3847
expect_no_offenses(<<~RUBY)
3948
desc 'Do foo'
@@ -45,11 +54,17 @@
4554
RUBY
4655
end
4756

48-
it 'do not register an offense for the default task' do
57+
it 'does not register an offense for the default task' do
4958
expect_no_offenses(<<~RUBY)
5059
task default: :spec
5160
5261
task default: [:spec, :rubocop]
5362
RUBY
5463
end
64+
65+
it 'does not register an offense when `task` is not a definition' do
66+
expect_no_offenses(<<~RUBY)
67+
task.name
68+
RUBY
69+
end
5570
end

0 commit comments

Comments
 (0)