Skip to content

Commit

Permalink
Merge pull request #10246 from koic/tweak_offense_range_for_layout_sp…
Browse files Browse the repository at this point in the history
…ace_in_lambda_literal

Tweak offense range for `Layout/SpaceInLambdaLiteral`
  • Loading branch information
koic committed Nov 15, 2021
2 parents e152d25 + dc7ffcb commit bdb92d6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
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

0 comments on commit bdb92d6

Please sign in to comment.