Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rake/MethodDefinitionInTask is 66.6% incorrect #42

Open
pboling opened this issue Nov 17, 2021 · 3 comments · May be fixed by #44
Open

Rake/MethodDefinitionInTask is 66.6% incorrect #42

pboling opened this issue Nov 17, 2021 · 3 comments · May be fixed by #44

Comments

@pboling
Copy link

pboling commented Nov 17, 2021

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/

@pboling pboling changed the title Rake/MethodDefinitionInTask is 100% incorrect and bad Rake/MethodDefinitionInTask is 66.6% incorrect Nov 17, 2021
@kuahyeow
Copy link

kuahyeow commented Feb 17, 2023

I guess it is definitely correct. Maybe an additional NoGlobalMethodsInRake cop is needed ?

@pboling
Copy link
Author

pboling commented Feb 17, 2023

I don't think a new cop is needed, the current cop, Rake/MethodDefinitionInTask just needs to be fixed.

@kuahyeow
Copy link

I don't think a new cop is needed, the current cop, Rake/MethodDefinitionInTask just needs to be fixed.

oh, you are right, sorry. Indeed a method defined inside task do... does not pollute global

@kuahyeow kuahyeow linked a pull request Feb 17, 2023 that will close this issue
pboling added a commit to rubocop-lts/standard-rubocop-lts that referenced this issue Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants