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 #8855] Fix an error when using only access modifier #8856

Merged
merged 1 commit into from Oct 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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