diff --git a/CHANGELOG.md b/CHANGELOG.md index a6fd406349..0e2585da4e 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 32e13eb94b..d9165a71a7 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..5bf20c8e90 100644 --- a/lib/rubocop/cop/rails/rake_environment.rb +++ b/lib/rubocop/cop/rails/rake_environment.rb @@ -41,8 +41,26 @@ def on_block(node) end end + def autocorrect(node) + lambda do |corrector| + task = node.arguments[0] + corrected = corrected_task_name(task.source) + + corrector.replace(task.loc.expression, corrected) + corrector.insert_after(node.loc.expression, ' :environment') + end + end + private + def corrected_task_name(source) + if source.match?(/\w+:\w+(.*)?/) + source + ' =>' + else + source.delete(':|\'|"') + ':' + 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 6b55b78987..856f4c8258 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..daf64e5369 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 names' 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 names' 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