From ea5c5a0ad48f9f274046f9242431528071c79d72 Mon Sep 17 00:00:00 2001 From: AdrienSldy Date: Sun, 16 Jun 2019 22:49:06 +0200 Subject: [PATCH] [Fix #7144] Extend the namespaces definition for Style/Documentation 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. --- CHANGELOG.md | 1 + lib/rubocop/cop/style/documentation.rb | 18 ++++++++--- manual/cops_style.md | 9 +++--- spec/rubocop/cop/style/documentation_spec.rb | 32 ++++++++++++++++++++ 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5111d09a2c0..c0f2e22f7b1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/rubocop/cop/style/documentation.rb b/lib/rubocop/cop/style/documentation.rb index e6afaa89c1c..d879997b472 100644 --- a/lib/rubocop/cop/style/documentation.rb +++ b/lib/rubocop/cop/style/documentation.rb @@ -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 @@ -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 @@ -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 diff --git a/manual/cops_style.md b/manual/cops_style.md index cbbefd26237..1733b3d5fad 100644 --- a/manual/cops_style.md +++ b/manual/cops_style.md @@ -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 diff --git a/spec/rubocop/cop/style/documentation_spec.rb b/spec/rubocop/cop/style/documentation_spec.rb index 40343b41467..5cd615a5cd3 100644 --- a/spec/rubocop/cop/style/documentation_spec.rb +++ b/spec/rubocop/cop/style/documentation_spec.rb @@ -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)