Skip to content

Commit

Permalink
Add autocorrect to Rails/RakeEnvironment cop
Browse files Browse the repository at this point in the history
  • Loading branch information
tejasbubane committed Apr 19, 2020
1 parent 2c860fc commit 49dbeb8
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

### New features

* [#211](https://github.com/rubocop-hq/rubocop-rails/issues/211): Add autocorrect to `Rails/RakeEnvironment` cop. ([@tejasbubane][])

### Bug fixes

* [#12](https://github.com/rubocop-hq/rubocop-rails/issues/12): Fix a false positive for `Rails/SkipsModelValidations` when passing a boolean literal to `touch`. ([@eugeneius][])
Expand Down Expand Up @@ -178,3 +182,4 @@
[@djudd]: https://github.com/djudd
[@sunny]: https://github.com/sunny
[@hoshinotsuyoshi]: https://github.com/hoshinotsuyoshi
[@tejasbubane]: https://github.com/tejasbubane
1 change: 1 addition & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ Rails/RakeEnvironment:
Enabled: true
Safe: false
VersionAdded: '2.4'
VersionChanged: '2.5.2'
Include:
- '**/Rakefile'
- '**/*.rake'
Expand Down
12 changes: 12 additions & 0 deletions lib/rubocop/cop/rails/rake_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ def on_block(node)
end
end

def autocorrect(node)
lambda do |corrector|
task_name = node.arguments[0]
corrected_task_name =
task_name.source.delete(':').delete("'").delete('"')
corrected_task_name += ':'

corrector.replace(task_name.loc.expression, corrected_task_name)
corrector.insert_after(node.loc.expression, ' :environment')
end
end

private

def task_name(node)
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_rails.md
Original file line number Diff line number Diff line change
Expand Up @@ -1710,7 +1710,7 @@ UnlessBlank | `true` | Boolean

Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | No | No | 2.4 | -
Enabled | No | Yes | 2.4 | 2.5.2

This cop checks for Rake tasks without the `:environment` task
dependency. The `:environment` task loads application code for other
Expand Down
24 changes: 24 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,24 @@
^^^^^^^^^ 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 'does not register an offense to task with :environment ' \
Expand Down Expand Up @@ -89,4 +107,10 @@
task(:foo).do_something
RUBY
end

it 'does not register an offense to task with string name & arguments' do
expect_no_offenses(<<~RUBY)
task 'foo' => [dep, :bar]
RUBY
end
end

0 comments on commit 49dbeb8

Please sign in to comment.