Skip to content

Commit

Permalink
Merge pull request #7809 from tejasbubane/style-endblock-autocorrect
Browse files Browse the repository at this point in the history
Add auto-correction for `Style/EndBlock` cop
  • Loading branch information
koic committed Mar 22, 2020
2 parents 6d3cc00 + 5e77e57 commit 4393c90
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -13,6 +13,7 @@
* [#7784](https://github.com/rubocop-hq/rubocop/pull/7784): Support Ruby 2.7's numbered parameter for `Lint/SafeNavigationChain`. ([@koic][])
* [#7331](https://github.com/rubocop-hq/rubocop/issues/7331): Add `forbidden` option to `Style/ModuleFunction` cop. ([@weh][])
* [#7699](https://github.com/rubocop-hq/rubocop/pull/7699): Add new `Lint/StructNewOverride` cop. ([@ybiquitous][])
* [#7809](https://github.com/rubocop-hq/rubocop/pull/7809): Add auto-correction for `Style/EndBlock` cop. ([@tejasbubane][])

### Bug fixes

Expand Down
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -2711,6 +2711,7 @@ Style/EndBlock:
StyleGuide: '#no-END-blocks'
Enabled: true
VersionAdded: '0.9'
VersionChanged: '0.81'

Style/EvalWithLocation:
Description: 'Pass `__FILE__` and `__LINE__` to `eval` method, as they are used by backtraces.'
Expand Down
6 changes: 6 additions & 0 deletions lib/rubocop/cop/style/end_block.rb
Expand Up @@ -19,6 +19,12 @@ class EndBlock < Cop
def on_postexe(node)
add_offense(node, location: :keyword)
end

def autocorrect(node)
lambda do |corrector|
corrector.replace(node.loc.keyword, 'at_exit')
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion manual/cops_style.md
Expand Up @@ -1909,7 +1909,7 @@ This cop checks ensures source files have no utf-8 encoding comments.
Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChanged
--- | --- | --- | --- | ---
Enabled | Yes | No | 0.9 | -
Enabled | Yes | Yes | 0.9 | 0.81
This cop checks for END blocks.
Expand Down
12 changes: 11 additions & 1 deletion spec/rubocop/cop/style/end_block_spec.rb
Expand Up @@ -3,10 +3,20 @@
RSpec.describe RuboCop::Cop::Style::EndBlock do
subject(:cop) { described_class.new }

it 'reports an offense for an END block' do
it 'reports an offense and corrects END block' do
expect_offense(<<~RUBY)
END { test }
^^^ Avoid the use of `END` blocks. Use `Kernel#at_exit` instead.
RUBY

expect_correction(<<~RUBY)
at_exit { test }
RUBY
end

it 'does not report offenses for other blocks' do
expect_no_offenses(<<~RUBY)
end_block { test }
RUBY
end
end

0 comments on commit 4393c90

Please sign in to comment.