Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-correction for Style/EndBlock cop #7809

Merged
merged 1 commit into from Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
tejasbubane marked this conversation as resolved.
Show resolved Hide resolved
end