Skip to content

Commit

Permalink
Fix SymbolProc autocorrect with multiple offenses
Browse files Browse the repository at this point in the history
When there were multiple offenses for the SymbolProc cop (e.g.
`coll.map { |s| s.upcase }.map { |s| s.downcase }`) the cop would
raise an error: `Parser::Source::Rewriter detected clobbering`.

Digging into relevant commits I found some inspiration from
@jonas054 (rubocop#569). This commit changes the autocorrect for the
`SymbolProc` cop to be a little more precise in determining what to
correct. There's a new spec that I wrote that fails on master and
passes on this branch.
  • Loading branch information
jeffcarbs committed Nov 23, 2014
1 parent f1f70ef commit ed95a43
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [#1181](https://github.com/bbatsov/rubocop/issues/1181): *(fix again)* `Style/StringLiterals` cop stays away from strings inside interpolated expressions. ([@jonas054][])
* [#1441](https://github.com/bbatsov/rubocop/issues/1441): Correct the logic used by `Style/Blocks` and other cops to determine if an auto-correction would alter the meaning of the code. ([@jonas054][])
* [#1449](https://github.com/bbatsov/rubocop/issues/1449): Handle the case in `MultilineOperationIndentation` where instances of both correct style and unrecognized (plain wrong) style are detected during an `--auto-gen-config` run. ([@jonas054][])
* [#1456](https://github.com/bbatsov/rubocop/pull/1456): Fix autocorrect in `SymbolProc` when there are multiple offenses on the same line. ([@jcarbo][])

## 0.27.1 (08/11/2014)

Expand Down Expand Up @@ -1161,3 +1162,4 @@
[@mvz]: https://github.com/mvz
[@jfelchner]: https://github.com/jfelchner
[@janraasch]: https://github.com/janraasch
[@jcarbo]: https://github.com/jcarbo
11 changes: 7 additions & 4 deletions lib/rubocop/cop/style/symbol_proc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@ def on_block(node)

def autocorrect(node)
@corrections << lambda do |corrector|
block_method, _block_args, block_body = *node
_block_method, _block_args, block_body = *node
_receiver, method_name, _args = *block_body

replacement = "#{block_method.loc.expression.source}" \
"(&:#{method_name})"
block_range =
Parser::Source::Range.new(node.loc.expression.source_buffer,
node.loc.begin.begin_pos,
node.loc.end.end_pos)

corrector.replace(node.loc.expression, replacement)
corrector.replace(range_with_surrounding_space(block_range, :left),
"(&:#{method_name})")
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/symbol_proc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
expect(corrected).to eq 'coll.map(&:upcase)'
end

it 'autocorrects multiple aliases with symbols as proc' do
corrected = autocorrect_source(cop, ['coll.map { |s| s.upcase }' \
'.map { |s| s.downcase }'])
expect(corrected).to eq 'coll.map(&:upcase).map(&:downcase)'
end

it 'does not crash with a bare method call' do
run = -> { inspect_source(cop, ['coll.map { |s| bare_method }']) }
expect(&run).not_to raise_error
Expand Down

0 comments on commit ed95a43

Please sign in to comment.