Skip to content

Commit

Permalink
Fix for rubocop#155. A bug in Semicolon#index_of_first_token_on_line.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas054 committed May 11, 2013
1 parent d65c59a commit f3b44b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

### Bugs fixed

* [#155](https://github.com/bbatsov/rubocop/issues/155) 'Do not use semicolons to terminate expressions.' is not implemented correctly

## 0.7.1 (05/11/2013)

### Bugs fixed
Expand Down
5 changes: 3 additions & 2 deletions lib/rubocop/cop/semicolon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ def inspect(file, source, tokens, sexp)
def index_of_first_token_on_line(ix, lineno)
# Index of last token on the previous line
prev_line_ix =
@tokens[0...ix].rindex { |t| t.pos.lineno < lineno } || 0
@tokens[0...ix].rindex { |t| t.pos.lineno < lineno } || -1
first = prev_line_ix + 1
# Index of first non-whitespace token on the current line.
prev_line_ix + @tokens[prev_line_ix..ix].index { |t| !whitespace?(t) }
first + @tokens[first..ix].index { |t| !whitespace?(t) }
end

def handle_exceptions_to_the_rule(token_1_ix)
Expand Down
16 changes: 14 additions & 2 deletions spec/rubocop/cops/semicolon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,20 @@ module Cop
it 'accepts one line empty class definitions' do
inspect_source(s,
'file.rb',
[' class Foo < Exception; end',
' class Bar; end'])
['# Prefer a single-line format for class ...',
'class Foo < Exception; end',
'',
'class Bar; end'])
expect(s.offences).to be_empty
end

it 'accepts one line empty method definitions' do
inspect_source(s,
'file.rb',
['# One exception to the rule are empty-body methods',
'def no_op; end',
'',
'def foo; end'])
expect(s.offences).to be_empty
end

Expand Down

0 comments on commit f3b44b2

Please sign in to comment.