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 #6305] Fix infinite loop for Layout/EmptyLinesAroundAccessModifier #6307

Conversation

koic
Copy link
Member

@koic koic commented Sep 19, 2018

Fixes #6305.

This is a problem that occurs after #6236.

This PR fixes infinite loop for Layout/EmptyLinesAroundAccessModifier and Layout/EmptyLinesAroundClassBody when specifying a superclass that breaks the line.

class AVerryLongClassNameDoYouLikeIt <
  ItEvenInheritsFromALongClassNameSOINeedToAddALineBrake

  protected

  def foobar
  end
end

With this PR, Layout/EmptyLinesAroundAccessModifier cop makes aware of the above case that specifying a superclass that breaks the line. This is the same behavior as writing a class definition on a single line.

As a result, Layout/EmptyLinesAroundAccessModifier and Layout/EmptyLinesAroundClassBody will not conflict with the handling of empty line under the class definition.

This problem was caused by using the regular expression in EmptyLinesAroundAccessModifier#class_def? method. This PR will change it to use AST.


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Run rake default. It executes all tests and RuboCop for itself, and generates the documentation.

…ssModifier`

Fixes rubocop#6305.

This is a problem that occurs after rubocop#6236.

This PR fixes infinite loop for `Layout/EmptyLinesAroundAccessModifier`
and `Layout/EmptyLinesAroundClassBody` when specifying a superclass
that breaks the line.

```ruby
class AVerryLongClassNameDoYouLikeIt <
  ItEvenInheritsFromALongClassNameSOINeedToAddALineBrake

  protected

  def foobar
  end
end
```

With this PR, `Layout/EmptyLinesAroundAccessModifier` cop makes
aware of the above case that specifying a superclass that
breaks the line. This is the same behavior as writing a class
definition on a single line.

As a result, `Layout/EmptyLinesAroundAccessModifier` and
`Layout/EmptyLinesAroundClassBody` will not conflict with
the handling of empty line under the class definition.

This problem was caused by using the regular expression
in `EmptyLinesAroundAccessModifier#class_def?` method.
This PR will change it to use AST.
@bbatsov bbatsov merged commit fce51ea into rubocop:master Sep 19, 2018
@bbatsov
Copy link
Collaborator

bbatsov commented Sep 19, 2018

This problem was caused by using the regular expression in EmptyLinesAroundAccessModifier#class_def? method. This PR will change it to use AST.

Ah, regular expressions bite us again. :-)

Great work, Koichi!

@koic koic deleted the fix_infinite_loop_empty_lines_around_access_modifier branch September 19, 2018 16:31
@koic
Copy link
Member Author

koic commented Sep 19, 2018

Thank you, Bozhidar!
This cop still has places that use regular expression. I'd like to refactor it when the opportunity comes 😃

koic added a commit to koic/rubocop that referenced this pull request Oct 5, 2018
…of regexp

Follow up of rubocop#6307 (comment).

This PR refactors `Layout/EmptyLinesAroundAccessModifier` to use AST instread of regexp.
bbatsov pushed a commit that referenced this pull request Oct 6, 2018
…of regexp

Follow up of #6307 (comment).

This PR refactors `Layout/EmptyLinesAroundAccessModifier` to use AST instread of regexp.
koic added a commit to koic/rubocop that referenced this pull request Dec 14, 2018
…ccessModifier`

Fixes rubocop#6566.

This PR fixes a false positive for `Layout/EmptyLinesAroundAccessModifier`
when at the end of specifying a superclass is missing blank line.

The following is originally no offense code.
(It is probably a regression by rubocop#6307)

```console
% cat example.rb
class SomeController < SomeOtherController
  def index; end

  private
end

% rubocop example.rb --only Layout/EmptyLinesAroundAccessModifier -a
Inspecting 1 file
C

Offenses:

example.rb:4:3: C: [Corrected] Layout/EmptyLinesAroundAccessModifier:
Keep a blank line before and after private.
  private
  ^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
```

Auto-correct adds a blank line after `private`. This is due to false positives.

```diff
 % cat example.rb
 class SomeController < SomeOtherController
   def index; end

   private
+
 end
```

This caused the infinite loop in `Layout/EmptyLinesAroundAccessModifier` and
`Layout/EmptyLinesAroundClassBody`.

```console
% rubocop example.rb --only Layout/EmptyLinesAroundClassBody -a
Inspecting 1 file
C

Offenses:

example.rb:5:1: C: [Corrected] Layout/EmptyLinesAroundClassBody: Extra
empty line detected at class body end.

1 file inspected, 1 offense detected, 1 offense corrected
```

Auto-correct removes the blank line from after of `private`.

```diff
 % cat example.rb
 class SomeController < SomeOtherController
   def index; end

   private
-
 end
```

It loops to the first code. This PR fixes the infinite loop.
bbatsov pushed a commit that referenced this pull request Dec 14, 2018
…difier`

Fixes #6566.

This PR fixes a false positive for `Layout/EmptyLinesAroundAccessModifier`
when at the end of specifying a superclass is missing blank line.

The following is originally no offense code.
(It is probably a regression by #6307)

```console
% cat example.rb
class SomeController < SomeOtherController
  def index; end

  private
end

% rubocop example.rb --only Layout/EmptyLinesAroundAccessModifier -a
Inspecting 1 file
C

Offenses:

example.rb:4:3: C: [Corrected] Layout/EmptyLinesAroundAccessModifier:
Keep a blank line before and after private.
  private
  ^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
```

Auto-correct adds a blank line after `private`. This is due to false positives.

```diff
 % cat example.rb
 class SomeController < SomeOtherController
   def index; end

   private
+
 end
```

This caused the infinite loop in `Layout/EmptyLinesAroundAccessModifier` and
`Layout/EmptyLinesAroundClassBody`.

```console
% rubocop example.rb --only Layout/EmptyLinesAroundClassBody -a
Inspecting 1 file
C

Offenses:

example.rb:5:1: C: [Corrected] Layout/EmptyLinesAroundClassBody: Extra
empty line detected at class body end.

1 file inspected, 1 offense detected, 1 offense corrected
```

Auto-correct removes the blank line from after of `private`.

```diff
 % cat example.rb
 class SomeController < SomeOtherController
   def index; end

   private
-
 end
```

It loops to the first code. This PR fixes the infinite loop.
Nyangawa pushed a commit to Nyangawa/rubocop that referenced this pull request Jan 24, 2019
…ccessModifier`

Fixes rubocop#6566.

This PR fixes a false positive for `Layout/EmptyLinesAroundAccessModifier`
when at the end of specifying a superclass is missing blank line.

The following is originally no offense code.
(It is probably a regression by rubocop#6307)

```console
% cat example.rb
class SomeController < SomeOtherController
  def index; end

  private
end

% rubocop example.rb --only Layout/EmptyLinesAroundAccessModifier -a
Inspecting 1 file
C

Offenses:

example.rb:4:3: C: [Corrected] Layout/EmptyLinesAroundAccessModifier:
Keep a blank line before and after private.
  private
  ^^^^^^^

1 file inspected, 1 offense detected, 1 offense corrected
```

Auto-correct adds a blank line after `private`. This is due to false positives.

```diff
 % cat example.rb
 class SomeController < SomeOtherController
   def index; end

   private
+
 end
```

This caused the infinite loop in `Layout/EmptyLinesAroundAccessModifier` and
`Layout/EmptyLinesAroundClassBody`.

```console
% rubocop example.rb --only Layout/EmptyLinesAroundClassBody -a
Inspecting 1 file
C

Offenses:

example.rb:5:1: C: [Corrected] Layout/EmptyLinesAroundClassBody: Extra
empty line detected at class body end.

1 file inspected, 1 offense detected, 1 offense corrected
```

Auto-correct removes the blank line from after of `private`.

```diff
 % cat example.rb
 class SomeController < SomeOtherController
   def index; end

   private
-
 end
```

It loops to the first code. This PR fixes the infinite loop.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Infinite loop: Layout/EmptyLinesAroundClassBody & Layout/EmptyLinesAroundAccessModifier
2 participants