Skip to content

Commit

Permalink
Check newline in empty reference bracket on `Layout/SpaceInsideRefere…
Browse files Browse the repository at this point in the history
…nceBrackets`
  • Loading branch information
r7kamura authored and bbatsov committed Oct 30, 2022
1 parent 479e588 commit a469efb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
@@ -0,0 +1 @@
* [#11131](https://github.com/rubocop/rubocop/pull/11131): Check newline in empty reference bracket on `Layout/SpaceInsideReferenceBrackets`. ([@r7kamura][])
8 changes: 6 additions & 2 deletions lib/rubocop/cop/layout/space_inside_reference_brackets.rb
Expand Up @@ -38,6 +38,8 @@ module Layout
# # bad
# foo[ ]
# foo[ ]
# foo[
# ]
#
# # good
# foo[]
Expand All @@ -49,6 +51,8 @@ module Layout
# # bad
# foo[]
# foo[ ]
# foo[
# ]
#
# # good
# foo[ ]
Expand All @@ -64,8 +68,6 @@ class SpaceInsideReferenceBrackets < Base
RESTRICT_ON_SEND = %i[[] []=].freeze

def on_send(node)
return if node.multiline?

tokens = processed_source.tokens_within(node)
left_token = left_ref_bracket(node, tokens)
return unless left_token
Expand All @@ -76,6 +78,8 @@ def on_send(node)
return empty_offenses(node, left_token, right_token, EMPTY_MSG)
end

return if node.multiline?

if style == :no_space
no_space_offenses(node, left_token, right_token, MSG)
else
Expand Down
7 changes: 4 additions & 3 deletions lib/rubocop/cop/mixin/surrounding_space.rb
Expand Up @@ -118,14 +118,15 @@ def offending_empty_space?(config, left_token, right_token)
end

def offending_empty_no_space?(config, left_token, right_token)
config == 'no_space' && !no_space_between?(left_token, right_token)
config == 'no_space' && !no_character_between?(left_token, right_token)
end

def space_between?(left_bracket_token, right_bracket_token)
left_bracket_token.end_pos + 1 == right_bracket_token.begin_pos
left_bracket_token.end_pos + 1 == right_bracket_token.begin_pos &&
processed_source.buffer.source[left_bracket_token.end_pos] == ' '
end

def no_space_between?(left_bracket_token, right_bracket_token)
def no_character_between?(left_bracket_token, right_bracket_token)
left_bracket_token.end_pos == right_bracket_token.begin_pos
end
end
Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/layout/space_inside_reference_brackets_spec.rb
Expand Up @@ -29,6 +29,18 @@
a[]
RUBY
end

it 'registers an offense and corrects empty brackets with newline inside' do
expect_offense(<<~RUBY)
a[
^ Do not use space inside empty reference brackets.
]
RUBY

expect_correction(<<~RUBY)
a[]
RUBY
end
end

context 'with space inside empty braces allowed' do
Expand Down Expand Up @@ -59,6 +71,18 @@
a[ ]
RUBY
end

it 'registers offense and corrects empty brackets with newline inside' do
expect_offense(<<~RUBY)
a[
^ Use one space inside empty reference brackets.
]
RUBY

expect_correction(<<~RUBY)
a[ ]
RUBY
end
end

context 'when EnforcedStyle is no_space' do
Expand Down Expand Up @@ -93,6 +117,14 @@
RUBY
end

it 'does not register offense for non-empty brackets with newline inside' do
expect_no_offenses(<<-RUBY)
foo[
bar
]
RUBY
end

it 'registers an offense and corrects when a reference bracket with a ' \
'leading whitespace is assigned by another reference bracket' do
expect_offense(<<~RUBY)
Expand Down

0 comments on commit a469efb

Please sign in to comment.