Skip to content

Commit

Permalink
Fix an error for Style/Semicolon
Browse files Browse the repository at this point in the history
This PR fixes the following error for `Style/Semicolon`
when using a comment containing a semicolon before a block:

```ruby
# ;
foo {
}
```

```console
% bundle exec rubocop --only Style/Semicolon
(snip)

  NoMethodError:
    undefined method `semicolon?' for nil:NilClass
```

No changelog entry because this is a bug by #11465 and not released.
  • Loading branch information
koic committed Jan 20, 2023
1 parent 081f520 commit 3a542a9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rubocop/cop/style/semicolon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def exist_semicolon_before_right_curly_brace?(tokens)
end

def exist_semicolon_after_left_curly_brace?(tokens)
tokens[1]&.left_curly_brace? && tokens[2].semicolon?
tokens[1]&.left_curly_brace? && tokens[2]&.semicolon?
end

def register_semicolon(line, column, after_expression, token_before_semicolon = nil)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/style/semicolon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ module Foo; end
RUBY
end

it 'does not register an offense when using a comment containing a semicolon before a block' do
expect_no_offenses(<<~RUBY)
# ;
foo {
}
RUBY
end

it 'registers an offense for range (`1..42`) with semicolon' do
expect_offense(<<~RUBY)
1..42;
Expand Down

0 comments on commit 3a542a9

Please sign in to comment.