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 false negatives when public method is defined after a private one #10774

Merged
merged 1 commit into from Jun 30, 2022
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/fix_false_negatives_in_documentation_comment.md
@@ -0,0 +1 @@
* [#10774](https://github.com/rubocop/rubocop/pull/10774): Fix false negatives in `Style/DocumentationMethod` when a public method is defined after a private one. ([@Darhazer][])
2 changes: 1 addition & 1 deletion lib/rubocop.rb
Expand Up @@ -72,7 +72,6 @@
require_relative 'rubocop/cop/mixin/configurable_formatting'
require_relative 'rubocop/cop/mixin/configurable_naming'
require_relative 'rubocop/cop/mixin/configurable_numbering'
require_relative 'rubocop/cop/mixin/def_node'
require_relative 'rubocop/cop/mixin/documentation_comment'
require_relative 'rubocop/cop/mixin/duplication'
require_relative 'rubocop/cop/mixin/range_help'
Expand Down Expand Up @@ -130,6 +129,7 @@
require_relative 'rubocop/cop/mixin/unused_argument'
require_relative 'rubocop/cop/mixin/visibility_help'
require_relative 'rubocop/cop/mixin/comments_help' # relies on visibility_help
require_relative 'rubocop/cop/mixin/def_node' # relies on visibility_help

require_relative 'rubocop/cop/utils/format_string'

Expand Down
9 changes: 2 additions & 7 deletions lib/rubocop/cop/mixin/def_node.rb
Expand Up @@ -5,8 +5,7 @@ module Cop
# Common functionality for checking def nodes.
module DefNode
extend NodePattern::Macros

NON_PUBLIC_MODIFIERS = %w[private protected].freeze
include VisibilityHelp

private

Expand All @@ -15,11 +14,7 @@ def non_public?(node)
end

def preceding_non_public_modifier?(node)
stripped_source_upto(node.first_line).any? { |line| NON_PUBLIC_MODIFIERS.include?(line) }
end

def stripped_source_upto(index)
processed_source[0..index].map(&:strip)
node_visibility(node) != :public
end

# @!method non_public_modifier?(node)
Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/documentation_method_spec.rb
Expand Up @@ -32,6 +32,24 @@ def method; end
^^^^^^^^^^^^^^^ Missing method documentation comment.
CODE
end

it 'registers an offense when method is public, but there were private methods before' do
expect_offense(<<~CODE)
class Foo
private

def baz
end

public

def foo
^^^^^^^ Missing method documentation comment.
puts 'bar'
end
end
CODE
end
end

context 'when method is private' do
Expand Down