Skip to content

Commit

Permalink
[Fix #11504] Allow initialize method in Style/DocumentationMethod
Browse files Browse the repository at this point in the history
Fixes #11504.

This PR makes to allow `initialize` method in `Style/DocumentationMethod`
because `initialize` is a special method called from `new`.
In some languages they are called constructor to distinguish it from method.

So it would be different from the following intent that this cop wants to detect.

> Checks for missing documentation comment for public methods.
> It can optionally be configured to also require documentation for
> non-public methods.
  • Loading branch information
koic authored and bbatsov committed Jan 26, 2023
1 parent 345b54c commit 61a0da1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11504](https://github.com/rubocop/rubocop/issues/11504): Allow `initialize` method in `Style/DocumentationMethod`. ([@koic][])
6 changes: 6 additions & 0 deletions lib/rubocop/cop/style/documentation_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ module Style
# It can optionally be configured to also require documentation for
# non-public methods.
#
# NOTE: This cop allows `initialize` method because `initialize` is
# a special method called from `new`. In some programming languages
# they are called constructor to distinguish it from method.
#
# @example
#
# # bad
Expand Down Expand Up @@ -103,6 +107,8 @@ class DocumentationMethod < Base
PATTERN

def on_def(node)
return if node.method?(:initialize)

parent = node.parent
module_function_node?(parent) ? check(parent) : check(node)
end
Expand Down
9 changes: 9 additions & 0 deletions spec/rubocop/cop/style/documentation_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def foo
end
end

context 'when `initialize` method' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
def initialize
end
RUBY
end
end

context 'when method is private' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 61a0da1

Please sign in to comment.