diff --git a/CHANGELOG.md b/CHANGELOG.md index af6c7f24e6..8cd37370d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ ### New features * [#51](https://github.com/rubocop-hq/rubocop-rails/issues/51): Add allowed receiver class names option for `Rails/DynamicFindBy`. ([@tejasbubane][]) +* [#211](https://github.com/rubocop-hq/rubocop-rails/issues/211): Add autocorrect to `Rails/RakeEnvironment` cop. ([@tejasbubane][]) ### Bug fixes diff --git a/config/default.yml b/config/default.yml index 95629e442b..d7cc7dbf8b 100644 --- a/config/default.yml +++ b/config/default.yml @@ -367,6 +367,7 @@ Rails/RakeEnvironment: Enabled: true Safe: false VersionAdded: '2.4' + VersionChanged: '2.6' Include: - '**/Rakefile' - '**/*.rake' diff --git a/lib/rubocop/cop/rails/rake_environment.rb b/lib/rubocop/cop/rails/rake_environment.rb index afbf9edbbc..b08b40ac44 100644 --- a/lib/rubocop/cop/rails/rake_environment.rb +++ b/lib/rubocop/cop/rails/rake_environment.rb @@ -41,8 +41,25 @@ def on_block(node) end end + def autocorrect(node) + lambda do |corrector| + task_name = node.arguments[0] + task_dependency = correct_task_dependency(task_name) + + corrector.replace(task_name.loc.expression, task_dependency) + end + end + private + def correct_task_dependency(task_name) + if task_name.sym_type? + task_name.source.delete(':|\'|"') + ': :environment' + else + "#{task_name.source} => :environment" + end + end + def task_name(node) first_arg = node.arguments[0] case first_arg&.type diff --git a/manual/cops_rails.md b/manual/cops_rails.md index ff807595b5..f0ff5f2fb9 100644 --- a/manual/cops_rails.md +++ b/manual/cops_rails.md @@ -1722,7 +1722,7 @@ UnlessBlank | `true` | Boolean Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged --- | --- | --- | --- | --- -Enabled | No | No | 2.4 | - +Enabled | No | Yes | 2.4 | 2.6 This cop checks for Rake tasks without the `:environment` task dependency. The `:environment` task loads application code for other diff --git a/spec/rubocop/cop/rails/rake_environment_spec.rb b/spec/rubocop/cop/rails/rake_environment_spec.rb index 141c20df8c..b619a2a3f9 100644 --- a/spec/rubocop/cop/rails/rake_environment_spec.rb +++ b/spec/rubocop/cop/rails/rake_environment_spec.rb @@ -11,6 +11,37 @@ ^^^^^^^^^ Include `:environment` task as a dependency for all Rake tasks. end RUBY + + expect_correction(<<~RUBY) + task foo: :environment do + end + RUBY + end + + it 'registers an offense for string task name' do + expect_offense(<<~RUBY) + task 'bar' do + ^^^^^^^^^^ Include `:environment` task as a dependency for all Rake tasks. + end + RUBY + + expect_correction(<<~RUBY) + task 'bar' => :environment do + end + RUBY + end + + it 'registers an offense for namespaced task name' do + expect_offense(<<~RUBY) + task 'foo:bar:baz' do + ^^^^^^^^^^^^^^^^^^ Include `:environment` task as a dependency for all Rake tasks. + end + RUBY + + expect_correction(<<~RUBY) + task 'foo:bar:baz' => :environment do + end + RUBY end it 'does not register an offense to task with :environment ' \ @@ -89,4 +120,10 @@ task(:foo).do_something RUBY end + + it 'does not register an offense to task with string name and arguments' do + expect_no_offenses(<<~RUBY) + task 'foo' => [dep, :bar] + RUBY + end end