Skip to content

Commit

Permalink
[Fix rubocop#5914] Fix false negative for `Layout/SpaceInsideReferenc…
Browse files Browse the repository at this point in the history
…eBrackets`

Fixes rubocop#5914.

This PR makes `Layout/SpaceInsideReferenceBrackets` aware of `no_space`
when using nested refefence brackets.
  • Loading branch information
koic committed May 28, 2018
1 parent 8d59b65 commit 5d80c9b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* Fix the indentation of autocorrected closing squiggly heredocs. ([@garettarrowood][])
* [#5908](https://github.com/bbatsov/rubocop/pull/5908): Fix `Style/BracesAroundHashParameters` auto-correct going past the end of the file when the closing curly brace is on the last line of a file. ([@EiNSTeiN-][])
* Fix a bug where `Style/FrozenStringLiteralComment` would be added to the second line if the first line is empty. ([@rrosenblum][])
* [#5914](https://github.com/bbatsov/rubocop/issues/5914): Make `Layout/SpaceInsideReferenceBrackets` aware of `no_space` when using nested reference brackets. ([@koic][])

### Changes

Expand Down
13 changes: 11 additions & 2 deletions lib/rubocop/cop/layout/space_inside_reference_brackets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,14 @@ def bracket_method?(node)
end

def left_ref_bracket(node, tokens)
if node.method?(:[]=)
current_token = tokens.reverse.find(&:left_ref_bracket?)
previous_token = previous_token(current_token)

if node.method?(:[]=) ||
previous_token && !previous_token.right_bracket?
tokens.find(&:left_ref_bracket?)
else
tokens.reverse.find(&:left_ref_bracket?)
current_token
end
end

Expand All @@ -131,6 +135,11 @@ def closing_bracket(tokens, opening_bracket)
end
end

def previous_token(current_token)
index = processed_source.tokens.index(current_token)
index.nil? || index.zero? ? nil : processed_source.tokens[index - 1]
end

def empty_config
cop_config['EnforcedStyleForEmptyBrackets']
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ def Vector.[](*array)
.to eq(['Do not use space inside reference brackets.'])
end

it 'registers offense in outer ref brackets' do
inspect_source(<<-RUBY.strip_indent)
record[ options[:attribute] ]
RUBY
expect(cop.offenses.size).to eq(2)
expect(cop.messages.uniq)
.to eq(['Do not use space inside reference brackets.'])
end

context 'auto-correct' do
it 'fixes multiple offenses in one set of ref brackets' do
new_source = autocorrect_source(<<-RUBY.strip_indent)
Expand Down

0 comments on commit 5d80c9b

Please sign in to comment.