From 0f11aa8a3aa69fd09965355efeee5b8491fe7148 Mon Sep 17 00:00:00 2001 From: Jan-Joost Spanjers Date: Sat, 30 Aug 2014 21:37:45 +0200 Subject: [PATCH] Correct whitespace braces around hash parameters --- CHANGELOG.md | 2 ++ .../style/braces_around_hash_parameters.rb | 31 ++++++++++++------- .../braces_around_hash_parameters_spec.rb | 27 ++++++++++++---- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 216009175ed2..dce834ebe026 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ * [#1211](https://github.com/bbatsov/rubocop/issues/1211): Fix false negative in `UselessAssignment` when there's a reference for the variable in an exclusive branch. ([@yujinakayama][]) * [#1307](https://github.com/bbatsov/rubocop/issues/1307): Fix auto-correction of `RedundantBegin` cop deletes new line. ([@yous][]) * [#1283](https://github.com/bbatsov/rubocop/issues/1283): Fix auto-correction of indented expressions in `PercentLiteralDelimiters`. ([@jonas054][]) +* [#1315](https://github.com/bbatsov/rubocop/pull/1315): `BracesAroundHashParameters` auto-correction removes whitespace around content inside braces. ([@jspanjers][]) ## 0.25.0 (15/08/2014) @@ -1078,3 +1079,4 @@ [@yous]: https://github.com/yous [@vrthra]: https://github.com/vrthra [@SkuliOskarsson]: https://github.com/SkuliOskarsson +[@jspanjers]: https://github.com/jspanjers diff --git a/lib/rubocop/cop/style/braces_around_hash_parameters.rb b/lib/rubocop/cop/style/braces_around_hash_parameters.rb index f271c516bfba..d4bafab6dcde 100644 --- a/lib/rubocop/cop/style/braces_around_hash_parameters.rb +++ b/lib/rubocop/cop/style/braces_around_hash_parameters.rb @@ -48,7 +48,8 @@ def autocorrect(node) if style == :no_braces corrector.remove(node.loc.begin) corrector.remove(node.loc.end) - remove_trailing_comma(node, corrector) + remove_leading_whitespace(node, corrector) + remove_trailing_comma_and_whitespace(node, corrector) elsif style == :braces corrector.insert_before(node.loc.expression, '{') corrector.insert_after(node.loc.expression, '}') @@ -56,18 +57,24 @@ def autocorrect(node) end end - def remove_trailing_comma(node, corrector) - sb = node.loc.end.source_buffer - pos_after_last_pair = node.children.last.loc.expression.end_pos - range_after_last_pair = - Parser::Source::Range.new(sb, pos_after_last_pair, - node.loc.end.begin_pos) - trailing_comma_offset = range_after_last_pair.source =~ /,/ - return unless trailing_comma_offset + def remove_leading_whitespace(node, corrector) + corrector.remove( + Parser::Source::Range.new( + node.loc.expression.source_buffer, + node.loc.begin.end_pos, + node.children.first.loc.expression.begin_pos + ) + ) + end - comma_begin = pos_after_last_pair + trailing_comma_offset - corrector.remove(Parser::Source::Range.new(sb, comma_begin, - comma_begin + 1)) + def remove_trailing_comma_and_whitespace(node, corrector) + corrector.remove( + Parser::Source::Range.new( + node.loc.expression.source_buffer, + node.children.last.loc.expression.end_pos, + node.loc.end.begin_pos + ) + ) end def non_empty_hash?(arg) diff --git a/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb b/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb index 2f212592d93a..6f68ea1f33e7 100644 --- a/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb +++ b/spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb @@ -137,34 +137,49 @@ describe 'auto-corrects' do it 'one non-hash parameter followed by a hash parameter with braces' do corrected = autocorrect_source(cop, ['where(1, { y: 2 })']) - expect(corrected).to eq 'where(1, y: 2 )' + expect(corrected).to eq 'where(1, y: 2)' end it 'one object method hash parameter with braces' do corrected = autocorrect_source(cop, ['x.func({ y: "z" })']) - expect(corrected).to eq 'x.func( y: "z" )' + expect(corrected).to eq 'x.func(y: "z")' end it 'one hash parameter with braces' do corrected = autocorrect_source(cop, ['where({ x: 1 })']) - expect(corrected).to eq 'where( x: 1 )' + expect(corrected).to eq 'where(x: 1)' end it 'one hash parameter with braces and separators' do corrected = autocorrect_source(cop, ['where( ', ' { x: 1 } )']) expect(corrected).to eq(['where( ', - ' x: 1 )'].join("\n")) + ' x: 1 )'].join("\n")) end it 'one hash parameter with braces and multiple keys' do corrected = autocorrect_source(cop, ['where({ x: 1, foo: "bar" })']) - expect(corrected).to eq 'where( x: 1, foo: "bar" )' + expect(corrected).to eq 'where(x: 1, foo: "bar")' + end + + it 'one hash parameter with braces and extra leading whitespace' do + corrected = autocorrect_source(cop, ['where({ x: 1, y: 2 })']) + expect(corrected).to eq 'where(x: 1, y: 2)' + end + + it 'one hash parameter with braces and extra trailing whitespace' do + corrected = autocorrect_source(cop, ['where({ x: 1, y: 2 })']) + expect(corrected).to eq 'where(x: 1, y: 2)' end it 'one hash parameter with braces and a trailing comma' do corrected = autocorrect_source(cop, ['where({ x: 1, y: 2, })']) - expect(corrected).to eq 'where( x: 1, y: 2 )' + expect(corrected).to eq 'where(x: 1, y: 2)' + end + + it 'one hash parameter with braces and trailing comma and whitespace' do + corrected = autocorrect_source(cop, ['where({ x: 1, y: 2, })']) + expect(corrected).to eq 'where(x: 1, y: 2)' end end end