Class: RuboCop::Cop::Gusto::ToplevelConstants
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Gusto::ToplevelConstants
- Defined in:
- lib/rubocop/cop/gusto/toplevel_constants.rb
Overview
Checks that no top-level constants (excluding classes and modules) are defined. This rule exists to prevent accidental pollution of the global namespace as well as cases where application code has accidentally depended on test code.
By default, this check is limited to files in app/, lib/, and
spec/ directories, except in the root of lib/ and in support files
in spec/support/.
Constant Summary collapse
- MSG =
"Top-level constants should be defined in an initializer. See https://github.com/Gusto/rubocop-gusto/blob/main/lib/rubocop/cop/gusto/toplevel_constants.rb"
Instance Method Summary collapse
Instance Method Details
#on_casgn(node) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/rubocop/cop/gusto/toplevel_constants.rb', line 44 def on_casgn(node) # Allow nested constants return unless node.parent.nil? || node.ancestors.all?(&:begin_type?) # Allow one-liners like `MyClass::MY_CONSTANT = 10` return unless node.children.first.nil? add_offense(node) end |