Skip to content

Commit

Permalink
[Fix rubocop#7759] Fix an error for Layout/LineLength
Browse files Browse the repository at this point in the history
Fixes rubocop#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'
```
  • Loading branch information
koic committed Feb 26, 2020
1 parent 7cce1fe commit d90867d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/layout/line_length.rb
Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/layout/line_length_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit d90867d

Please sign in to comment.