Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master (unreleased)

* [#49](https://github.com/rubocop/rubocop-rake/issues/49): Fix false negatives for `Rake/Desc` when a task has a single prerequisite in an array. ([@ydakuka][])

## 0.7.1 (2025-02-16)

### Bug fixes
Expand Down Expand Up @@ -78,3 +80,4 @@
[@jaruuuu]: https://github.com/jaruuuu
[@koic]: https://github.com/koic
[@tejasbubane]: https://github.com/tejasbubane
[@ydakuka]: https://github.com/ydakuka
18 changes: 14 additions & 4 deletions lib/rubocop/cop/rake/desc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,8 @@ class Desc < Base

def on_task(node)
return if task_with_desc?(node)
return if Helper::TaskName.task_name(node) == :default

requirements = prerequisites(node)
return if requirements&.array_type?
return if default_task?(node)
return if multiple_prerequisites?(node)

add_offense(node)
end
Expand Down Expand Up @@ -76,6 +74,18 @@ def on_task(node)
private def can_insert_desc_to?(parent)
parent.type?(:begin, :block, :kwbegin)
end

private def default_task?(node)
Helper::TaskName.task_name(node) == :default
end

private def multiple_prerequisites?(node)
requirements = prerequisites(node)
return false unless requirements&.array_type?

children = requirements.children
children && children.size > 1
end
end
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/rake/desc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,28 @@
expect_offense(<<~RUBY)
task release: 'changelog:check_clean'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Describe the task with `desc` method.

task release: :changelog
^^^^^^^^^^^^^^^^^^^^^^^^ Describe the task with `desc` method.

task release: %w[changelog]
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Describe the task with `desc` method.

task release: %i[changelog]
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Describe the task with `desc` method.

task "release" => :environment
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Describe the task with `desc` method.
RUBY
end

it 'does not register an offense for multiple prerequisite declarations' do
expect_no_offenses(<<~RUBY)
task release: ['changelog:check_clean', 'changelog:create']

task release: %w[changelog lint]

task release: %i[changelog lint]
RUBY
end
end