Skip to content

Commit fcc31f2

Browse files
authored
Merge pull request #14192 from koic/fix_false_negatives_for_layout_space_before_brackets
Fix false negatives for `Layout/SpaceBeforeBrackets`
2 parents c73ed17 + b2d362c commit fcc31f2

File tree

5 files changed

+14
-7
lines changed

5 files changed

+14
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#14192](https://github.com/rubocop/rubocop/pull/14192): Fix negatives for `Layout/SpaceBeforeBrackets` when using space between method argument parentheses and left bracket. ([@koic][])

lib/rubocop/cop/layout/space_before_brackets.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ def on_send(node)
2626
selector_begin_pos = node.loc.selector.begin_pos
2727
return if receiver_end_pos >= selector_begin_pos
2828
return if dot_before_brackets?(node, receiver_end_pos, selector_begin_pos)
29-
return if !reference_variable_with_brackets?(node) && !node.method?(:[]=)
3029

3130
range = range_between(receiver_end_pos, selector_begin_pos)
3231

@@ -42,10 +41,6 @@ def dot_before_brackets?(node, receiver_end_pos, selector_begin_pos)
4241

4342
dot.begin_pos == receiver_end_pos && dot.end_pos == selector_begin_pos
4443
end
45-
46-
def reference_variable_with_brackets?(node)
47-
node.receiver&.variable? && node.method?(:[]) && node.arguments.size == 1
48-
end
4944
end
5045
end
5146
end

lib/rubocop/cop/style/command_literal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def default_delimiter
173173
end
174174

175175
def preferred_delimiters_config
176-
config.for_cop('Style/PercentLiteralDelimiters') ['PreferredDelimiters']
176+
config.for_cop('Style/PercentLiteralDelimiters')['PreferredDelimiters']
177177
end
178178
end
179179
end

lib/rubocop/cop/style/regexp_literal.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def slash_literal?(node)
155155
end
156156

157157
def preferred_delimiters
158-
config.for_cop('Style/PercentLiteralDelimiters') ['PreferredDelimiters']['%r'].chars
158+
config.for_cop('Style/PercentLiteralDelimiters')['PreferredDelimiters']['%r'].chars
159159
end
160160

161161
def allowed_omit_parentheses_with_percent_r_literal?(node)

spec/rubocop/cop/layout/space_before_brackets_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@
4848
RUBY
4949
end
5050

51+
it 'registers an offense and corrects when using space between method argument parentheses and left bracket' do
52+
expect_offense(<<~RUBY)
53+
collection.call(arg) [index_or_key]
54+
^ Remove the space before the opening brackets.
55+
RUBY
56+
57+
expect_correction(<<~RUBY)
58+
collection.call(arg)[index_or_key]
59+
RUBY
60+
end
61+
5162
it 'does not register an offense when using space between method call and left brackets' do
5263
expect_no_offenses(<<~RUBY)
5364
do_something [item_of_array_literal]

0 commit comments

Comments
 (0)