From d90867dfa0a73d3151a9e53ae40138208a9d83e4 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Wed, 26 Feb 2020 19:14:33 +0900 Subject: [PATCH] [Fix #7759] Fix an error for `Layout/LineLength` Fixes #7759. This PR fixes the following error for `Layout/LineLength` cop when using lambda syntax that argument is not enclosed in parentheses. ```ruby % cat example.rb -> x {} ``` ```console % bundle exec rubocop --only Layout/LineLength -d (snip) An error occurred while Layout/LineLength cop was inspecting /Users/koic/src/github.com/koic/rubocop-issues/7759/example.rb:1:0. undefined method `begin_pos' for nil:NilClass /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/layout/line_length.rb:127:in `check_for_breakable_block' /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/layout/line_length.rb:71:in `on_block' /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:57:in `block (2 levels) in trigger_responding_cops' /Users/koic/src/github.com/rubocop-hq/rubocop/lib/rubocop/cop/commissioner.rb:136:in `with_cop_error_handling' ``` --- CHANGELOG.md | 1 + lib/rubocop/cop/layout/line_length.rb | 2 +- spec/rubocop/cop/layout/line_length_spec.rb | 30 +++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 390288cee9c..91a43e2329b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * [#7709](https://github.com/rubocop-hq/rubocop/issues/7709): Fix correction of `Style/RedundantCondition` when the else branch contains a range. ([@rrosenblum][]) * [#7682](https://github.com/rubocop-hq/rubocop/issues/7682): Fix `Style/InverseMethods` autofix leaving parenthesis. ([@tejasbubane][]) * [#7745](https://github.com/rubocop-hq/rubocop/issues/7745): Suppress a pending cop warnings when pending cop's department is disabled. ([@koic][]) +* [#7759](https://github.com/rubocop-hq/rubocop/issues/7759): Fix an error for `Layout/LineLength` cop when using lambda syntax that argument is not enclosed in parentheses. ([@koic][]) ## 0.80.0 (2020-02-18) diff --git a/lib/rubocop/cop/layout/line_length.rb b/lib/rubocop/cop/layout/line_length.rb index 8f1b1e9e1b5..503ca44255b 100644 --- a/lib/rubocop/cop/layout/line_length.rb +++ b/lib/rubocop/cop/layout/line_length.rb @@ -131,7 +131,7 @@ def check_for_breakable_block(block_node) end def breakable_block_range(block_node) - if block_node.arguments? + if block_node.arguments? && !block_node.lambda? block_node.arguments.loc.end else block_node.loc.begin diff --git a/spec/rubocop/cop/layout/line_length_spec.rb b/spec/rubocop/cop/layout/line_length_spec.rb index 3f32300e4db..d8e60219539 100644 --- a/spec/rubocop/cop/layout/line_length_spec.rb +++ b/spec/rubocop/cop/layout/line_length_spec.rb @@ -791,6 +791,36 @@ def baz(bar) RUBY end end + + context 'lambda syntax' do + context 'when argument is enclosed in parentheses' do + it 'registers an offense and corrects' do + expect_offense(<<~RUBY) + ->(x) { fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [70/40] + RUBY + + expect_correction(<<~RUBY) + ->(x) { + fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo } + RUBY + end + end + + context 'when argument is not enclosed in parentheses' do + it 'registers an offense and corrects' do + expect_offense(<<~RUBY) + -> x { foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Line is too long. [70/40] + RUBY + + expect_correction(<<~RUBY) + -> x { + foooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo } + RUBY + end + end + end end context 'semicolon' do