From 91c23e5896e71dd478fffaaf6a8b62639ddb5f9a Mon Sep 17 00:00:00 2001 From: Tim Downey Date: Sun, 2 Oct 2016 00:06:17 -0700 Subject: [PATCH] [Fix #3103] Make Style/ExtraSpacing not ignore single line hash 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. --- CHANGELOG.md | 2 ++ lib/rubocop/cop/style/extra_spacing.rb | 8 +++++--- spec/rubocop/cop/style/extra_spacing_spec.rb | 19 +++++++++++++++++++ 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe7c4a006074..215e8df89486 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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][]) @@ -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 diff --git a/lib/rubocop/cop/style/extra_spacing.rb b/lib/rubocop/cop/style/extra_spacing.rb index cd22f30274b3..11afea30c312 100644 --- a/lib/rubocop/cop/style/extra_spacing.rb +++ b/lib/rubocop/cop/style/extra_spacing.rb @@ -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) diff --git a/spec/rubocop/cop/style/extra_spacing_spec.rb b/spec/rubocop/cop/style/extra_spacing_spec.rb index 0887b3982518..e55a93d8e7ca 100644 --- a/spec/rubocop/cop/style/extra_spacing_spec.rb +++ b/spec/rubocop/cop/style/extra_spacing_spec.rb @@ -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]}']