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

[Fix #1062] Correct rake tasks with arguments #1063

Merged
merged 1 commit into from
Aug 10, 2023

Conversation

fastjames
Copy link
Contributor

When auto-correcting rake tasks to include the :environment prerequisite, account for rake tasks that accept arguments.

closes #1062


Before submitting the PR make sure the following are checked:

  • The PR relates to only one subject with a clear title and description in grammatically correct, complete sentences.
  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Ran bundle exec rake default. It executes all tests and runs RuboCop on its own code.
  • Added an entry (file) to the changelog folder named {change_type}_{change_description}.md if the new code introduces user-observable changes. See changelog entry format for details.
  • If this is a new cop, consider making a corresponding update to the Rails Style Guide.

end
end
end

private

def correct_task_arguments_dependency(task_method)
"#{task_arguments(task_method).source} => [:environment]"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tweak the autocorrection code? It seems to work with symbol instead of array.

Suggested change
"#{task_arguments(task_method).source} => [:environment]"
"#{task_arguments(task_method).source} => :environment"

Comment on lines 49 to 50
args_deps_hash = correct_task_arguments_dependency(task_method)
corrector.replace(task_method.arguments[1], args_deps_hash)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you refactor?

Suggested change
args_deps_hash = correct_task_arguments_dependency(task_method)
corrector.replace(task_method.arguments[1], args_deps_hash)
new_task_dependency = correct_task_arguments_dependency(task_method)
corrector.replace(task_arguments(task_method), new_task_dependency)

Comment on lines 52 to 54
task_name = task_method.arguments[0]
task_dependency = correct_task_dependency(task_name)
corrector.replace(task_name, task_dependency)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
task_name = task_method.arguments[0]
task_dependency = correct_task_dependency(task_name)
corrector.replace(task_name, task_dependency)
task_name = task_method.first_argument
new_task_dependency = correct_task_dependency(task_name)
corrector.replace(task_name, new_task_dependency)

RUBY

expect_correction(<<~RUBY)
task :foo, [:arg] => [:environment] do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
task :foo, [:arg] => [:environment] do
task :foo, [:arg] => :environment do

@koic
Copy link
Member

koic commented Aug 10, 2023

Looks good to me. Can you squash your commits into one?

Account for rake tasks that accept arguments.
@fastjames fastjames force-pushed the rake_environment_autocorrect_args branch from 23149d7 to dd17d2d Compare August 10, 2023 16:39
@fastjames
Copy link
Contributor Author

Looks good to me. Can you squash your commits into one?

I was hoping that you would ask! I have squashed my fixes back into the original commit.

@koic koic merged commit fc1be54 into rubocop:master Aug 10, 2023
4 checks passed
@koic
Copy link
Member

koic commented Aug 10, 2023

Thanks!

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 this pull request may close these issues.

Rails/RakeEnvironment autocorrect does not handle parameters
2 participants