Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow excluding some constants from Style/Documentation #9219

Merged
merged 1 commit into from Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/new_allow_excluding_some_constants_from.md
@@ -0,0 +1 @@
* [#9219](https://github.com/rubocop-hq/rubocop/pull/9219): Allow excluding some constants from Style/Documentation. ([@fsateler][])
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -3174,6 +3174,7 @@ Style/Documentation:
Description: 'Document classes and non-namespace modules.'
Enabled: true
VersionAdded: '0.9'
AllowedConstants: []
Exclude:
- 'spec/**/*'
- 'test/**/*'
Expand Down
28 changes: 25 additions & 3 deletions lib/rubocop/cop/style/documentation.rb
Expand Up @@ -60,6 +60,15 @@ module Style
# extend Foo
# end
#
# @example AllowedConstants: ['ClassMethods']
#
fsateler marked this conversation as resolved.
Show resolved Hide resolved
# # good
# module A
# module ClassMethods
# # ...
# end
# end
#
class Documentation < Base
include DocumentationComment

Expand All @@ -85,14 +94,19 @@ def on_module(node)

def check(node, body, type)
return if namespace?(body)
return if documentation_comment?(node) || nodoc_comment?(node)
return if compact_namespace?(node) &&
nodoc_comment?(outer_module(node).first)
return if documentation_comment?(node)
return if constant_allowed?(node)
return if nodoc_self_or_outer_module?(node)
return if macro_only?(body)

add_offense(node.loc.keyword, message: format(MSG, type: type))
end

def nodoc_self_or_outer_module?(node)
nodoc_comment?(node) ||
compact_namespace?(node) && nodoc_comment?(outer_module(node).first)
end

def macro_only?(body)
body.respond_to?(:macro?) && body.macro? ||
body.respond_to?(:children) && body.children&.all? { |child| macro_only?(child) }
Expand All @@ -112,6 +126,10 @@ def constant_declaration?(node)
constant_definition?(node) || constant_visibility_declaration?(node)
end

def constant_allowed?(node)
allowed_constants.include?(node.identifier.short_name)
end

def compact_namespace?(node)
/::/.match?(node.loc.name.source)
end
Expand All @@ -138,6 +156,10 @@ def nodoc?(comment, require_all: false)
def nodoc(node)
processed_source.ast_with_comments[node.children.first].first
end

def allowed_constants
@allowed_constants ||= cop_config.fetch('AllowedConstants', []).map(&:intern)
end
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/rubocop/cli/auto_gen_config_spec.rb
Expand Up @@ -698,6 +698,7 @@ def a; end
" - 'example1.rb'",
'',
'# Offense count: 1',
'# Configuration parameters: AllowedConstants.',
'Style/Documentation:',
' Exclude:',
" - 'spec/**/*'", # Copied from default configuration
Expand Down Expand Up @@ -1067,6 +1068,7 @@ def a; end
' Exclude:',
" - 'example1.rb'",
'',
'# Configuration parameters: AllowedConstants.',
'Style/Documentation:',
' Exclude:',
" - 'spec/**/*'", # Copied from default configuration
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/documentation_spec.rb
Expand Up @@ -411,6 +411,21 @@ class Test < Parent
RUBY
end
end

describe 'when AllowedConstants is configured' do
before { config['Style/Documentation'] = { 'AllowedConstants' => ['ClassMethods'] } }

it 'ignores the constants in the config' do
expect_no_offenses(<<~RUBY)
module A
module ClassMethods
def do_something
end
end
end
RUBY
end
end
end
end
end