Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Layout/SpaceBeforeBrackets
cop
Follow rubocop/ruby-style-guide#856. This PR adds new `Layout/SpaceBeforeBrackets` cop. ```ruby # bad collection [index_or_key] # good collection[index_or_key] ``` This cop is marked as unsafe because it can occur false positives for `do_something [this_is_an_array_literal_argument]` that take an array without parentheses as an argument. I noticed this issue below. https://github.com/rsim/oracle-enhanced/pull/2076/files# diff-0d21136da5aee9b25213dcf874f62418fa24a06a90237e069642a5af54e7b2efL494-R494
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#9231](https://github.com/rubocop-hq/rubocop/pull/9231): Add new `Layout/SpaceBeforeBrackets` cop. ([@koic][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Layout | ||
# Checks for space between the name of a receiver and a left | ||
# brackets. | ||
# | ||
# This cop is marked as unsafe because it can occur false positives | ||
# for `do_something [this_is_an_array_literal_argument]` that take | ||
# an array without parentheses as an argument. | ||
# | ||
# @example | ||
# | ||
# # bad | ||
# collection [index_or_key] | ||
# | ||
# # good | ||
# collection[index_or_key] | ||
# | ||
class SpaceBeforeBrackets < Base | ||
include RangeHelp | ||
extend AutoCorrector | ||
|
||
MSG = 'Remove the space before the opening brackets.' | ||
|
||
def_node_matcher :bad_method?, <<~PATTERN | ||
(send nil? :bad_method ...) | ||
PATTERN | ||
|
||
def on_send(node) | ||
return if node.parenthesized? || node.parent&.send_type? | ||
return unless (first_argument = node.first_argument) | ||
|
||
begin_pos = first_argument.source_range.begin_pos | ||
|
||
return unless (range = offense_range(node, first_argument, begin_pos)) | ||
|
||
register_offense(range) | ||
end | ||
|
||
private | ||
|
||
def offense_range(node, first_argument, begin_pos) | ||
if space_before_brackets?(node, first_argument) | ||
range_between(node.loc.selector.end_pos, begin_pos) | ||
elsif node.method?(:[]=) | ||
end_pos = node.receiver.source_range.end_pos | ||
|
||
return if begin_pos - end_pos == 1 | ||
|
||
range_between(end_pos, begin_pos - 1) | ||
end | ||
end | ||
|
||
def register_offense(range) | ||
add_offense(range) do |corrector| | ||
corrector.remove(range) | ||
end | ||
end | ||
|
||
def space_before_brackets?(node, first_argument) | ||
node.receiver.nil? && first_argument.array_type? && node.arguments.size == 1 | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Layout::SpaceBeforeBrackets, :config do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
context 'when referencing' do | ||
it 'registers an offense and corrects when using space between receiver and left brackets' do | ||
expect_offense(<<~RUBY) | ||
collection [index_or_key] | ||
^ Remove the space before the opening brackets. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
collection[index_or_key] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not using space between receiver and left brackets' do | ||
expect_no_offenses(<<~RUBY) | ||
collection[index_or_key] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when array literal argument is enclosed in parentheses' do | ||
expect_no_offenses(<<~RUBY) | ||
collection([index_or_key]) | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when it is used as a method argument' do | ||
expect_no_offenses(<<~RUBY) | ||
expect(offenses).to eq [] | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using multiple arguments' do | ||
expect_no_offenses(<<~RUBY) | ||
do_something [foo], bar | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when without receiver' do | ||
expect_no_offenses(<<~RUBY) | ||
[index_or_key] | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when assigning' do | ||
it 'registers an offense and corrects when using space between receiver and left brackets' do | ||
expect_offense(<<~RUBY) | ||
@correction [index_or_key] = :value | ||
^ Remove the space before the opening brackets. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
@correction[index_or_key] = :value | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not using space between receiver and left brackets' do | ||
expect_no_offenses(<<~RUBY) | ||
@correction[index_or_key] = :value | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when assigning an array' do | ||
expect_no_offenses(<<~RUBY) | ||
task.options = ['--no-output'] | ||
RUBY | ||
end | ||
end |