Skip to content

Commit

Permalink
Merge pull request #1315 from CO2Management/fix/correct-whitespace-br…
Browse files Browse the repository at this point in the history
…aces-around-hash-parameters

Correct whitespace braces around hash parameters
  • Loading branch information
bbatsov committed Aug 31, 2014
2 parents 8f429c5 + 0f11aa8 commit ff65412
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -1078,3 +1079,4 @@
[@yous]: https://github.com/yous
[@vrthra]: https://github.com/vrthra
[@SkuliOskarsson]: https://github.com/SkuliOskarsson
[@jspanjers]: https://github.com/jspanjers
31 changes: 19 additions & 12 deletions lib/rubocop/cop/style/braces_around_hash_parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,33 @@ 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, '}')
end
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)
Expand Down
27 changes: 21 additions & 6 deletions spec/rubocop/cop/style/braces_around_hash_parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff65412

Please sign in to comment.