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 auto-correction for Style/Lambda with no-space argument #6801

Merged
merged 1 commit into from
Mar 3, 2019
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
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?
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is just a refactoring.


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