Skip to content

Commit

Permalink
[Fix rubocop#3142] Fix Style/Lambda whitespacing when auto-correct un…
Browse files Browse the repository at this point in the history
…parenthesized arguments (rubocop#3160)
  • Loading branch information
palkan authored and Neodelf committed Oct 15, 2016
1 parent 572ced6 commit fbcc8c3
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* [#3146](https://github.com/bbatsov/rubocop/pull/3146): Fix `NegatedIf` and `NegatedWhile` to ignore double negations. ([@natalzia-paperless][])
* [#3140](https://github.com/bbatsov/rubocop/pull/3140): `Style/FrozenStringLiteralComment` works with file doesn't have any tokens. ([@pocke][])
* [#3154](https://github.com/bbatsov/rubocop/issues/3154): Fix handling of `()` in `Style/RedundantParentheses`. ([@lumeet][])
* [#3160](https://github.com/bbatsov/rubocop/pull/3160): `Style/Lambda` fix whitespacing when auto-correcting unparenthesized arguments. ([@palkan][])

### Changes

Expand Down
22 changes: 22 additions & 0 deletions lib/rubocop/cop/style/lambda.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ def autocorrect(node)

def autocorrect_literal_to_method(corrector, node)
block_method, args = *node

# Check for unparenthesized args' preceding and trailing whitespaces.
remove_unparenthesized_whitespaces(corrector, node)

# Avoid correcting to `lambdado` by inserting whitespace
# if none exists before or after the lambda arguments.
if needs_whitespace?(block_method, args, node)
Expand Down Expand Up @@ -181,6 +185,24 @@ def arg_to_unparenthesized_call?(node)
index = parent.children.index { |c| c.equal?(node) }
index >= 2
end

def remove_unparenthesized_whitespaces(corrector, node)
block_method, args = *node
return unless unparenthesized_literal_args?(args)
# First, remove leading whitespaces (beetween arrow and args)
corrector.remove_preceding(
args.source_range,
args.source_range.begin_pos - block_method.source_range.end_pos
)

# Then, remove trailing whitespaces (beetween args and 'do')
delta = node.loc.begin.begin_pos - args.source_range.end_pos - 1
corrector.remove_preceding(node.loc.begin, delta)
end

def unparenthesized_literal_args?(args)
args.source_range && args.source_range.begin && !parentheses?(args)
end
end
end
end
Expand Down
42 changes: 42 additions & 0 deletions spec/rubocop/cop/style/lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,48 @@
' x',
'end'].join("\n")
end

context 'without parentheses' do
let(:source) do
['-> hello do',
' puts hello',
'end']
end

it_behaves_like 'registers an offense',
'Use the `lambda` method for multiline lambdas.'
it_behaves_like 'auto-correct', ['lambda do |hello|',
' puts hello',
'end'].join("\n")
end

context 'with no parentheses and bad spacing' do
let(:source) do
['-> hello do',
' puts hello',
'end']
end

it_behaves_like 'registers an offense',
'Use the `lambda` method for multiline lambdas.'
it_behaves_like 'auto-correct', ['lambda do |hello|',
' puts hello',
'end'].join("\n")
end

context 'with no parentheses and many args' do
let(:source) do
['-> hello, user do',
' puts hello',
'end']
end

it_behaves_like 'registers an offense',
'Use the `lambda` method for multiline lambdas.'
it_behaves_like 'auto-correct', ['lambda do |hello, user|',
' puts hello',
'end'].join("\n")
end
end
end

Expand Down

0 comments on commit fbcc8c3

Please sign in to comment.