Skip to content

Commit

Permalink
Merge pull request #235 from tejasbubane/fix-211
Browse files Browse the repository at this point in the history
Add autocorrect to `Rails/RakeEnvironment` cop
  • Loading branch information
koic committed Apr 27, 2020
2 parents f5cbb50 + a850c2e commit 8c2f945
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ Rails/RakeEnvironment:
Enabled: true
Safe: false
VersionAdded: '2.4'
VersionChanged: '2.6'
Include:
- '**/Rakefile'
- '**/*.rake'
Expand Down
17 changes: 17 additions & 0 deletions lib/rubocop/cop/rails/rake_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions spec/rubocop/cop/rails/rake_environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 ' \
Expand Down Expand Up @@ -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

0 comments on commit 8c2f945

Please sign in to comment.