Skip to content

Commit

Permalink
[Fix rubocop#7144] Extend the namespaces definition for Style/Documen…
Browse files Browse the repository at this point in the history
…tation cop

Extend the definition of namespaces by adding the constants visibility
statement.
No longer report an error for classes and modules that contain constants'
visibility statements.
  • Loading branch information
AdrienSdy committed Jun 16, 2019
1 parent f4df0ca commit ea5c5a0
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
* [#7119](https://github.com/rubocop-hq/rubocop/pull/7119): Fix cache with non UTF-8 offense message. ([@pocke][])
* [#7118](https://github.com/rubocop-hq/rubocop/pull/7118): Fix `Style/WordArray` with `encoding: binary` magic comment and non-ASCII string. ([@pocke][])
* [#7113](https://github.com/rubocop-hq/rubocop/pull/7113): This PR renames `EnforcedStyle: rails` to `EnabledStyle: outdented_access_modifiers` for `Layout/IndentationConsistency`. ([@koic][])
* [#7144](https://github.com/rubocop-hq/rubocop/issues/7144): Fix `Style/Documentation` constant visibility declaration in namespace. ([@AdrienSldy][])

### Changes

Expand Down
18 changes: 13 additions & 5 deletions lib/rubocop/cop/style/documentation.rb
Expand Up @@ -3,10 +3,11 @@
module RuboCop
module Cop
module Style
# This cop checks for missing top-level documentation of
# classes and modules. Classes with no body are exempt from the
# check and so are namespace modules - modules that have nothing in
# their bodies except classes, other modules, or constant definitions.
# This cop checks for missing top-level documentation of classes and
# modules. Classes with no body are exempt from the check and so are
# namespace modules - modules that have nothing in their bodies except
# classes, other modules, constant definitions or constant visibility
# declarations.
#
# The documentation requirement is annulled if the class or module has
# a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the
Expand All @@ -31,6 +32,9 @@ class Documentation < Cop

def_node_matcher :constant_definition?, '{class module casgn}'
def_node_search :outer_module, '(const (const nil? _) _)'
def_node_matcher :constant_visibility_declaration?, <<-PATTERN
(send nil? {:public_constant :private_constant} ({sym str} _))
PATTERN

def on_class(node)
return unless node.body
Expand Down Expand Up @@ -59,12 +63,16 @@ def namespace?(node)
return false unless node

if node.begin_type?
node.children.all? { |child| constant_definition?(child) }
node.children.all?(&method(:constant_declaration?))
else
constant_definition?(node)
end
end

def constant_declaration?(node)
constant_definition?(node) || constant_visibility_declaration?(node)
end

def compact_namespace?(node)
node.loc.name.source =~ /::/
end
Expand Down
9 changes: 5 additions & 4 deletions manual/cops_style.md
Expand Up @@ -1347,10 +1347,11 @@ Enabled by default | Safe | Supports autocorrection | VersionAdded | VersionChan
--- | --- | --- | --- | ---
Enabled | Yes | No | 0.9 | -

This cop checks for missing top-level documentation of
classes and modules. Classes with no body are exempt from the
check and so are namespace modules - modules that have nothing in
their bodies except classes, other modules, or constant definitions.
This cop checks for missing top-level documentation of classes and
modules. Classes with no body are exempt from the check and so are
namespace modules - modules that have nothing in their bodies except
classes, other modules, constant definitions or constant visibility
declarations.

The documentation requirement is annulled if the class or module has
a "#:nodoc:" comment next to it. Likewise, "#:nodoc: all" does the
Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/style/documentation_spec.rb
Expand Up @@ -187,6 +187,38 @@ module Test
RUBY
end

context 'without documentation' do
context 'with non-empty module' do
context 'with constants visibility declaration content' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
module Namespace
class Private
end
private_constant :Private
end
RUBY
end
end
end

context 'with non-empty class' do
context 'with constants visibility declaration content' do
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
class Namespace
class Private
end
private_constant :Private
end
RUBY
end
end
end
end

it 'does not raise an error for an implicit match conditional' do
expect do
inspect_source(<<~RUBY)
Expand Down

0 comments on commit ea5c5a0

Please sign in to comment.