Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SymbolProc autocorrect with multiple offenses #1456

Merged
merged 1 commit into from Nov 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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