Skip to content

Commit

Permalink
Merge pull request #10774 from rubocop/fix-documentation-comment-fals…
Browse files Browse the repository at this point in the history
…e-negatives

Fix false negatives when public method is defined after a private one
  • Loading branch information
koic committed Jun 30, 2022
2 parents db9105d + 8d02cda commit 5e82448
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
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

0 comments on commit 5e82448

Please sign in to comment.