Skip to content

Commit

Permalink
[Fix rubocop#3103] Make Style/ExtraSpacing not ignore single line has…
Browse files Browse the repository at this point in the history
…h literals

Cop was not creating offenses for extra spaces found within single line
hash literals since it  was assuming that Style/AlignHash would
catch any spacing issues.
  • Loading branch information
tcdowney authored and Neodelf committed Oct 15, 2016
1 parent 03bfe83 commit 91c23e5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

### Bug fixes

* [#3103](https://github.com/bbatsov/rubocop/pull/3103): Make `Style/ExtraSpacing` cop register an offense for extra spaces present in single-line hash literals. ([@tcdowney][])
* [#3513](https://github.com/bbatsov/rubocop/pull/3513): Fix false positive in `Style/TernaryParentheses` for a ternary with ranges. ([@dreyks][])
* [#3520](https://github.com/bbatsov/rubocop/issues/3520): Fix regression causing `Lint/AssignmentInCondition` false positive. ([@savef][])
* [#3514](https://github.com/bbatsov/rubocop/issues/3514): Make `Style/VariableNumber` cop not register an offense when valid normal case variable names have an integer after the first `_`. ([@b-t-g][])
Expand Down Expand Up @@ -2392,3 +2393,4 @@
[@b-t-g]: https://github.com/b-t-g
[@coorasse]: https://github.com/coorasse
[@scottmatthewman]: https://github.com/scottmatthewman
[@tcdowney]: https://github.com/tcdowney
8 changes: 5 additions & 3 deletions lib/rubocop/cop/style/extra_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,15 +140,17 @@ def unary_plus_non_offense?(range)
end

# Returns an array of ranges that should not be reported. It's the
# extra spaces between the keys and values in a hash, since those are
# handled by the Style/AlignHash cop.
# extra spaces between the keys and values in a multiline hash,
# since those are handled by the Style/AlignHash cop.
def ignored_ranges(ast)
return [] unless ast

@ignored_ranges ||= on_node(:pair, ast).map do |pair|
next if pair.parent.single_line?

key, value = *pair
key.source_range.end_pos...value.source_range.begin_pos
end
end.compact
end

def aligned_comments?(token)
Expand Down
19 changes: 19 additions & 0 deletions spec/rubocop/cop/style/extra_spacing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@
expect(cop.offenses).to be_empty
end

context 'when spaces are present in a single-line hash literal' do
it 'registers an offense for hashes with symbol keys' do
inspect_source(cop, 'hash = {a: 1, b: 2}')
expect(cop.offenses.size).to eq(3)
end

it 'registers an offense for hashes with hash rockets' do
source = [
'let(:single_line_hash) {',
' {"a" => "1", "b" => "2"}',
'}'
]

inspect_source(cop, source)
expect(cop.offenses.size).to eq(1)
expect(cop.offenses.first.line).to eq(2)
end
end

it 'can handle extra space before a float' do
source = ['{:a => "a",',
' :b => [nil, 2.5]}']
Expand Down

0 comments on commit 91c23e5

Please sign in to comment.