Skip to content

Commit

Permalink
Add IgnoreModules configuration to Style/ConstantVisibility
Browse files Browse the repository at this point in the history
To not register offense for module definitions.

Issue #8724
  • Loading branch information
tejasbubane authored and bbatsov committed Feb 14, 2021
1 parent 07cd64c commit 6cdce3b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#8724](https://github.com/rubocop-hq/rubocop/issues/8724): Add `IgnoreModules` configuration to `Style/ConstantVisibility` to not register offense for module definitions. ([@tejasbubane][])
2 changes: 2 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3102,6 +3102,8 @@ Style/ConstantVisibility:
visibility declarations.
Enabled: false
VersionAdded: '0.66'
VersionChanged: <<next>>
IgnoreModules: false

# Checks that you have put a copyright in a comment before any code.
#
Expand Down
27 changes: 27 additions & 0 deletions lib/rubocop/cop/style/constant_visibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,47 @@ module Style
# public_constant :BAZ
# end
#
# @example IgnoreModules: false (default)
# # bad
# class Foo
# MyClass = Struct.new()
# end
#
# # good
# class Foo
# MyClass = Struct.new()
# public_constant :MyClass
# end
#
# @example IgnoreModules: true
# # good
# class Foo
# MyClass = Struct.new()
# end
#
class ConstantVisibility < Base
MSG = 'Explicitly make `%<constant_name>s` public or private using ' \
'either `#public_constant` or `#private_constant`.'

def on_casgn(node)
return unless class_or_module_scope?(node)
return if visibility_declaration?(node)
return if ignore_modules? && module?(node)

message = message(node)
add_offense(node, message: message)
end

private

def ignore_modules?
cop_config.fetch('IgnoreModules', false)
end

def module?(node)
node.children.last.class_constructor?
end

def message(node)
_namespace, constant_name, _value = *node

Expand Down
32 changes: 32 additions & 0 deletions spec/rubocop/cop/style/constant_visibility_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,38 @@ class Foo
end
end

it 'registers an offense for module definitions' do
expect_offense(<<~RUBY)
module Foo
MyClass = Class.new()
^^^^^^^^^^^^^^^^^^^^^ Explicitly make `MyClass` public or private using either `#public_constant` or `#private_constant`.
end
RUBY
end

context 'IgnoreModules' do
let(:cop_config) { { 'IgnoreModules' => true } }

it 'does not register an offense for class definitions' do
expect_no_offenses(<<~RUBY)
class Foo
SomeClass = Class.new()
SomeModule = Module.new()
SomeStruct = Struct.new()
end
RUBY
end

it 'registers an offense for constants' do
expect_offense(<<~RUBY)
module Foo
BAR = 42
^^^^^^^^ Explicitly make `BAR` public or private using either `#public_constant` or `#private_constant`.
end
RUBY
end
end

it 'does not register an offense when passing a string to the ' \
'visibility declaration' do
expect_no_offenses(<<~RUBY)
Expand Down

0 comments on commit 6cdce3b

Please sign in to comment.