Skip to content

Commit

Permalink
[Fix #6673] Fix Style/DocumentationMethod for inline module_function
Browse files Browse the repository at this point in the history
Cop now recognizes documentation comments for `def`
inline with `module_function`.
  • Loading branch information
tejasbubane authored and bbatsov committed Jan 16, 2019
1 parent e2f1153 commit c95b534
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [#6670](https://github.com/rubocop-hq/rubocop/issues/6670): Fix a false positive for `Style/SafeNavigation` when a method call safeguarded with a negative check for the object. ([@koic][])
* [#6633](https://github.com/rubocop-hq/rubocop/issues/6633): Fix `Lint/SafeNavigation` complaining about use of `to_d`. ([@tejasbubane][])
* [#6575](https://github.com/rubocop-hq/rubocop/issues/6575): Fix `Naming/PredicateName` suggesting invalid rename. ([@tejasbubane][])
* [#6673](https://github.com/rubocop-hq/rubocop/issues/6673): Fix `Style/DocumentationMethod` cop to recognize documentation comments for `def` inline with `module_function`. ([@tejasbubane][])

### Changes

Expand Down
7 changes: 6 additions & 1 deletion lib/rubocop/cop/style/documentation_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ class DocumentationMethod < Cop

MSG = 'Missing method documentation comment.'.freeze

def_node_matcher :module_function_node?, <<-PATTERN
(send nil? :module_function ...)
PATTERN

def on_def(node)
check(node)
parent = node.parent
module_function_node?(parent) ? check(parent) : check(node)
end
alias on_defs on_def

Expand Down
48 changes: 47 additions & 1 deletion spec/rubocop/cop/style/documentation_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ def baz

context 'when declaring methods in a module' do
context 'without documentation comment' do
context 'wheh method is public' do
context 'when method is public' do
it 'registers an offense' do
expect_offense(<<-CODE.strip_indent)
module Foo
Expand Down Expand Up @@ -570,6 +570,30 @@ module Foo
end
end
end

context 'when method is module_function' do
it 'registers an offense for inline def' do
expect_offense(<<-CODE.strip_indent)
module Foo
module_function def bar
^^^^^^^^^^^^^^^^^^^^^^^ Missing method documentation comment.
end
end
CODE
end

it 'registers an offense for separate def' do
expect_offense(<<-CODE.strip_indent)
module Foo
def bar
^^^^^^^ Missing method documentation comment.
end
module_function :bar
end
CODE
end
end
end

context 'with documentation comment' do
Expand All @@ -594,6 +618,28 @@ def bar; end
CODE
end
end

context 'when method is module_function' do
it 'does not register an offense for inline def' do
expect_no_offenses(<<-CODE.strip_indent)
module Foo
# Documentation
module_function def bar; end
end
CODE
end

it 'does not register an offense for separate def' do
expect_no_offenses(<<-CODE.strip_indent)
module Foo
# Documentation
def bar; end
module_function :bar
end
CODE
end
end
end

context 'with both public and private methods' do
Expand Down

0 comments on commit c95b534

Please sign in to comment.