Skip to content

Commit

Permalink
[Fix #8855] Fix an error when using only access modifier
Browse files Browse the repository at this point in the history
Fixes #8855.

This PR fixes an error for `Layout/EmptyLinesAroundAccessModifier` and
`Style/AccessModifierDeclarations` when using only access modifier.
  • Loading branch information
koic committed Oct 6, 2020
1 parent d4ce6ce commit 415d3e8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -25,6 +25,7 @@
* [#8842](https://github.com/rubocop-hq/rubocop/issues/8842): Save actual status to cache, except corrected. ([@hatkyinc2][])
* [#8835](https://github.com/rubocop-hq/rubocop/issues/8835): Fix an incorrect autocorrect for `Style/RedundantInterpolation` when using string interpolation for non-operator methods. ([@koic][])
* [#7495](https://github.com/rubocop-hq/rubocop/issues/7495): Example for `Lint/AmbiguousBlockAssociation` cop. ([@AllanSiqueira][])
* [#8855](https://github.com/rubocop-hq/rubocop/issues/8855): Fix an error for `Layout/EmptyLinesAroundAccessModifier` and `Style/AccessModifierDeclarations` when using only access modifier. ([@koic][])

### Changes

Expand Down
13 changes: 6 additions & 7 deletions lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb
Expand Up @@ -84,7 +84,8 @@ def on_block(node)
end

def on_send(node)
return unless register_offense?(node)
return unless node.bare_access_modifier? && !node.parent&.block_type?
return if expected_empty_lines?(node)

message = message(node)
add_offense(node, message: message) do |corrector|
Expand All @@ -98,17 +99,15 @@ def on_send(node)

private

def register_offense?(node)
return false unless node.bare_access_modifier? && !node.parent.block_type?

def expected_empty_lines?(node)
case style
when :around
return false if empty_lines_around?(node)
return true if empty_lines_around?(node)
when :only_before
return false if allowed_only_before_style?(node)
return true if allowed_only_before_style?(node)
end

true
false
end

def allowed_only_before_style?(node)
Expand Down
8 changes: 6 additions & 2 deletions lib/rubocop/cop/style/access_modifier_declarations.rb
Expand Up @@ -83,8 +83,8 @@ class AccessModifierDeclarations < Base

def on_send(node)
return unless node.access_modifier?
return if node.parent.pair_type?
return if cop_config['AllowModifiersOnSymbols'] && access_modifier_with_symbol?(node)
return if node.parent&.pair_type?
return if allow_modifiers_on_symbols?(node)

if offense?(node)
add_offense(node.loc.selector) if opposite_style_detected
Expand All @@ -95,6 +95,10 @@ def on_send(node)

private

def allow_modifiers_on_symbols?(node)
cop_config['AllowModifiersOnSymbols'] && access_modifier_with_symbol?(node)
end

def offense?(node)
(group_style? && access_modifier_is_inlined?(node)) ||
(inline_style? && access_modifier_is_not_inlined?(node))
Expand Down
Expand Up @@ -322,6 +322,12 @@ def test; end\r
end\r
RUBY
end

it 'accepts only using access modifier' do
expect_no_offenses(<<~RUBY)
#{access_modifier}
RUBY
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/rubocop/cop/style/access_modifier_declarations_spec.rb
Expand Up @@ -64,6 +64,12 @@ class Test
RUBY
end

it 'accepts when using only #{access_modifier}' do
expect_no_offenses(<<~RUBY)
#{access_modifier}
RUBY
end

it "does not offend when #{access_modifier} is not inlined and " \
'has a comment' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 415d3e8

Please sign in to comment.