Skip to content

Commit

Permalink
Merge pull request #6801 from pocke/Style/Lambda
Browse files Browse the repository at this point in the history
Fix auto-correction for `Style/Lambda` with no-space argument
  • Loading branch information
pocke committed Mar 3, 2019
2 parents 47a97b7 + a75bda2 commit dc69686
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [#6802](https://github.com/rubocop-hq/rubocop/pull/6802): Fix auto-correction for `Style/SymbolArray` with array contains interpolation when `EnforcedStyle` is `brackets`. ([@pocke][])
* [#6797](https://github.com/rubocop-hq/rubocop/pull/6797): Fix false negative for Layout/SpaceAroundBlockParameters on block parameter with parens. ([@pocke][])
* [#6803](https://github.com/rubocop-hq/rubocop/pull/6803): Fix error for `Style/NumericLiterals` on a literal that contains spaces. ([@pocke][])
* [#6801](https://github.com/rubocop-hq/rubocop/pull/6801): Fix auto-correction for `Style/Lambda` with no-space argument. ([@pocke][])

### Changes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def call(corrector)
attr_reader :block_node, :method, :arguments

def remove_unparenthesized_whitespace(corrector)
return unless !arguments.empty? && !arguments.parenthesized_call?
return if arguments.empty? || arguments.parenthesized_call?

remove_leading_whitespace(corrector)
remove_trailing_whitespace(corrector)
Expand Down Expand Up @@ -69,10 +69,8 @@ def remove_leading_whitespace(corrector)
end

def remove_trailing_whitespace(corrector)
corrector.remove_preceding(
block_begin,
block_begin.begin_pos - arguments.source_range.end_pos - 1
)
size = block_begin.begin_pos - arguments.source_range.end_pos - 1
corrector.remove_preceding(block_begin, size) if size > 0
end

def replace_delimiters(corrector)
Expand Down
8 changes: 8 additions & 0 deletions spec/rubocop/cop/style/lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
'Use the `lambda` method for all lambdas.'
it_behaves_like 'auto-correct', 'f = lambda { x }'
end

context 'without argument parens and spaces' do
let(:source) { 'f = ->x{ p x }' }

it_behaves_like 'registers an offense',
'Use the `lambda` method for all lambdas.'
it_behaves_like 'auto-correct', 'f = lambda{ |x| p x }'
end
end

context 'with a multiline lambda literal' do
Expand Down

0 comments on commit dc69686

Please sign in to comment.