From 415d3e84955fd962ea9a1101992d7aa6dea2e1f8 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Tue, 6 Oct 2020 16:41:20 +0900 Subject: [PATCH] [Fix #8855] Fix an error when using only access modifier Fixes #8855. This PR fixes an error for `Layout/EmptyLinesAroundAccessModifier` and `Style/AccessModifierDeclarations` when using only access modifier. --- CHANGELOG.md | 1 + .../layout/empty_lines_around_access_modifier.rb | 13 ++++++------- .../cop/style/access_modifier_declarations.rb | 8 ++++++-- .../empty_lines_around_access_modifier_spec.rb | 6 ++++++ .../cop/style/access_modifier_declarations_spec.rb | 6 ++++++ 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18890f4c21b..62383270385 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb b/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb index b0f02e3c43e..ad3e9731ed1 100644 --- a/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +++ b/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb @@ -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| @@ -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) diff --git a/lib/rubocop/cop/style/access_modifier_declarations.rb b/lib/rubocop/cop/style/access_modifier_declarations.rb index bea5d065bf1..643a7851752 100644 --- a/lib/rubocop/cop/style/access_modifier_declarations.rb +++ b/lib/rubocop/cop/style/access_modifier_declarations.rb @@ -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 @@ -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)) diff --git a/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb b/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb index 74c092b77f4..967cf0333d3 100644 --- a/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb +++ b/spec/rubocop/cop/layout/empty_lines_around_access_modifier_spec.rb @@ -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 diff --git a/spec/rubocop/cop/style/access_modifier_declarations_spec.rb b/spec/rubocop/cop/style/access_modifier_declarations_spec.rb index b8573864cb6..b4e99e3cb34 100644 --- a/spec/rubocop/cop/style/access_modifier_declarations_spec.rb +++ b/spec/rubocop/cop/style/access_modifier_declarations_spec.rb @@ -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)