Skip to content

Commit

Permalink
Merge pull request #1456 from jcarbo/fix-multiple-symbol-proc
Browse files Browse the repository at this point in the history
Fix SymbolProc autocorrect with multiple offenses
  • Loading branch information
bbatsov committed Nov 23, 2014
2 parents f1f70ef + ed95a43 commit df7415c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
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
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
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 df7415c

Please sign in to comment.