Skip to content

Commit

Permalink
Merge pull request #12378 from koic/fix_a_false_negative_for_style_se…
Browse files Browse the repository at this point in the history
…micolon

Fix a false negative for `Style/Semicolon`
  • Loading branch information
koic committed Nov 10, 2023
2 parents 95b0290 + cee4694 commit e17d887
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_a_false_negative_for_style_semicolon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#12378](https://github.com/rubocop/rubocop/pull/12378): Fix a false negative for `Style/Semicolon` when a semicolon at the beginning of a lambda block. ([@koic][])
8 changes: 8 additions & 0 deletions lib/rubocop/cop/style/semicolon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def tokens_for_lines
processed_source.tokens.group_by(&:line)
end

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def semicolon_position(tokens)
if tokens.last.semicolon?
-1
Expand All @@ -90,10 +91,13 @@ def semicolon_position(tokens)
elsif exist_semicolon_after_left_curly_brace?(tokens) ||
exist_semicolon_after_left_string_interpolation_brace?(tokens)
2
elsif exist_semicolon_after_left_lambda_curly_brace?(tokens)
3
elsif exist_semicolon_before_right_string_interpolation_brace?(tokens)
-4
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

def exist_semicolon_before_right_curly_brace?(tokens)
tokens[-2]&.right_curly_brace? && tokens[-3]&.semicolon?
Expand All @@ -103,6 +107,10 @@ def exist_semicolon_after_left_curly_brace?(tokens)
tokens[1]&.left_curly_brace? && tokens[2]&.semicolon?
end

def exist_semicolon_after_left_lambda_curly_brace?(tokens)
tokens[2]&.type == :tLAMBEG && tokens[3]&.semicolon?
end

def exist_semicolon_before_right_string_interpolation_brace?(tokens)
tokens[-3]&.type == :tSTRING_DEND && tokens[-4]&.semicolon?
end
Expand Down
11 changes: 11 additions & 0 deletions spec/rubocop/cop/style/semicolon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ module Foo; end
RUBY
end

it 'registers an offense for a semicolon at the beginning of a lambda block' do
expect_offense(<<~RUBY)
foo -> {; bar }
^ Do not use semicolons to terminate expressions.
RUBY

expect_correction(<<~RUBY)
foo -> { bar }
RUBY
end

it 'registers an offense for a semicolon at the end of a block' do
expect_offense(<<~RUBY)
foo { bar; }
Expand Down

0 comments on commit e17d887

Please sign in to comment.