Skip to content

Rake/MethodDefinitionInTask is 66.6% incorrect #42

@pboling

Description

@pboling

The good code is bad, and the bad code is good.

Methods should be defined inside tasks (only)

(but not namespaces), as it is the only way they will not be globally scoped, and thereby silently conflict with all other same-named globally scoped methods.

Obviously it is also fine to define methods in classes or modules... that's outside the scope of this issue.

definition location scope rubocop-rake should consider rubocop-rake currently considers
top-level global bad 🛑 good ✅
namespace block global bad 🛑 bad 🛑
task block task good ✅ bad 🛑

"Good Code" example for task:

namespace :foo_namespace do
  desc 'foo task'
  task :foo do
    def greet
      puts 'Hello from foo.rake'
    end
 
    puts 'In foo task'
    greet
  end
end

"Bad Code" example for namespace:

namespace :foo_namespace do
  desc 'foo task'
  task :foo do
    puts 'In foo task'
    greet
  end
 
  def greet
    puts 'Hello from foo.rake'
  end
end

"Bad Code" example for top-level:

desc 'foo task'
task :foo do
  puts 'In foo task'
  greet
end
 
def greet
  puts 'Hello from foo.rake'
end

reference: https://www.albertoalmagro.com/en/ruby-methods-defined-in-rake-tasks/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions