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

Tweak offense range for Layout/SpaceInLambdaLiteral #10246

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
16 changes: 11 additions & 5 deletions lib/rubocop/cop/layout/space_in_lambda_literal.rb
Expand Up @@ -30,15 +30,17 @@ class SpaceInLambdaLiteral < Base
def on_send(node)
return unless arrow_lambda_with_args?(node)

lambda_node = range_of_offense(node)

if style == :require_space && !space_after_arrow?(node)
lambda_node = range_of_offense(node)

add_offense(lambda_node, message: MSG_REQUIRE_SPACE) do |corrector|
corrector.insert_before(node.parent.children[1], ' ')
corrector.insert_before(lambda_arguments(node), ' ')
end
elsif style == :require_no_space && space_after_arrow?(node)
add_offense(lambda_node, message: MSG_REQUIRE_NO_SPACE) do |corrector|
corrector.remove(space_after_arrow(node))
space = space_after_arrow(node)

add_offense(space, message: MSG_REQUIRE_NO_SPACE) do |corrector|
corrector.remove(space)
end
end
end
Expand Down Expand Up @@ -66,6 +68,10 @@ def range_of_offense(node)
node.parent.arguments.loc.expression.end_pos
)
end

def lambda_arguments(node)
node.parent.children[1]
end
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/rubocop/cop/layout/space_in_lambda_literal_spec.rb
Expand Up @@ -73,7 +73,7 @@
it 'registers an offense and corrects a space between -> and (' do
expect_offense(<<~RUBY)
a = -> (b, c) { b + c }
^^^^^^^^^ Do not use spaces between `->` and `(` in lambda literals.
^ Do not use spaces between `->` and `(` in lambda literals.
RUBY

expect_correction(<<~RUBY)
Expand Down Expand Up @@ -101,7 +101,7 @@
it 'registers an offense and corrects spaces between -> and (' do
expect_offense(<<~RUBY)
a = -> (b, c) { b + c }
^^^^^^^^^^^ Do not use spaces between `->` and `(` in lambda literals.
^^^ Do not use spaces between `->` and `(` in lambda literals.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -112,7 +112,7 @@
it 'registers an offense and corrects a space in the inner nested lambda' do
expect_offense(<<~RUBY)
a = ->(b = -> (c) {}, d) { b + d }
^^^^^^ Do not use spaces between `->` and `(` in lambda literals.
^ Do not use spaces between `->` and `(` in lambda literals.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -123,7 +123,7 @@
it 'registers an offense and corrects a space in the outer nested lambda' do
expect_offense(<<~RUBY)
a = -> (b = ->(c) {}, d) { b + d }
^^^^^^^^^^^^^^^^^^^^ Do not use spaces between `->` and `(` in lambda literals.
^ Do not use spaces between `->` and `(` in lambda literals.
RUBY

expect_correction(<<~RUBY)
Expand All @@ -134,8 +134,8 @@
it 'register offenses and correct spaces in both lambdas when nested' do
expect_offense(<<~RUBY)
a = -> (b = -> (c) {}, d) { b + d }
^^^^^^ Do not use spaces between `->` and `(` in lambda literals.
^^^^^^^^^^^^^^^^^^^^^ Do not use spaces between `->` and `(` in lambda literals.
^ Do not use spaces between `->` and `(` in lambda literals.
^ Do not use spaces between `->` and `(` in lambda literals.
RUBY

expect_correction(<<~RUBY)
Expand Down