Skip to content

Commit

Permalink
New cop EndBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
bbatsov committed Jun 27, 2013
1 parent a9eef6f commit 080be42
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -27,6 +27,7 @@
* New cop `EndInMethod` tracks uses of `END` in method definitions.
* New cop `LiteralInCondition` tracks uses of literals in the conditions of `if/while/until`.
* New cop `BeginBlock` tracks uses of `BEGIN` blocks.
* New cop `EndBlock` tracks uses of `END` blocks.
* Add support for auto-correction of some offences with `-a`/`--auto-correct`.

### Changes
Expand Down
4 changes: 4 additions & 0 deletions config/enabled.yml
Expand Up @@ -348,6 +348,10 @@ CharacterLiteral:
BeginBlock:
Enabled: true

# Avoid the use of END blocks.
EndBlock:
Enabled: true

## Warnings

# Don't use assignment in conditions.
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop.rb
Expand Up @@ -53,6 +53,7 @@
require 'rubocop/cop/style/empty_lines'
require 'rubocop/cop/style/empty_literal'
require 'rubocop/cop/style/encoding'
require 'rubocop/cop/style/end_block'
require 'rubocop/cop/style/end_of_line'
require 'rubocop/cop/style/favor_join'
require 'rubocop/cop/style/favor_modifier'
Expand Down
18 changes: 18 additions & 0 deletions lib/rubocop/cop/style/end_block.rb
@@ -0,0 +1,18 @@
# encoding: utf-8

module Rubocop
module Cop
module Style
# This cop checks for BEGIN blocks.
class EndBlock < Cop
MSG = 'Avoid the use of END blocks. Use `Kernel#at_exit` instead.'

def on_postexe(node)
add_offence(:convention, node.loc.expression, MSG)

super
end
end
end
end
end
19 changes: 19 additions & 0 deletions spec/rubocop/cops/style/end_block_spec.rb
@@ -0,0 +1,19 @@
# encoding: utf-8

require 'spec_helper'

module Rubocop
module Cop
module Style
describe EndBlock do
let(:cop) { EndBlock.new }

it 'reports an offence for an END block' do
src = ['END { test }']
inspect_source(cop, src)
expect(cop.offences.size).to eq(1)
end
end
end
end
end

0 comments on commit 080be42

Please sign in to comment.