Skip to content

Commit

Permalink
Add autocorrect to Rails/RakeEnvironment cop
Browse files Browse the repository at this point in the history
Closes #211
  • Loading branch information
tejasbubane committed Apr 21, 2020
1 parent c36151c commit d1ffdb7
Show file tree
Hide file tree
Showing 5 changed files with 58 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
18 changes: 18 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,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
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 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 ' \
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 d1ffdb7

Please sign in to comment.